excilys/androidannotations — Issue #10: Executing methods only if runtime conditions are met
Author: pyricau
Created: 2012-01-06 10:34:16+00:00
State: open

This is more of a draft idea that needs to be discussed. Suggested by @matboniface, I write it here after a few beers so the idea might need improvements ;-)

Create a new annotation, @RunWhen or @RunIf, which would be provided with conditions. Those conditions would be evaluated at runtime. It's basically brings the same things as if you were just adding an "if" at the beginning of your method, except that you provide the conditions through metadatas, and your code is therefor more readable, since it only deals with what's important.

It would be nice to be able to do things such as :

```
@RunIf(@Device(constructor="HTC"))
public void myMethod() {
// Runs if device has an HTC constructor
}
```

```
@RunIf( { @AndroidVersion(greaterThan=8), @MyCondition(myParam="SomeValue") })
public void myMethod() {
// Runs if android version > 8 and a custom condition is met.
}
```

The @MyCondition annotation would be created by the user, which would bind it to a condition class that would have a validation method (returns boolean) and parameters that matches the annotation parameter names and types. This condition class should also be able to receive a context (activity?).

It could also be used to implement roles :

```
@RunIf(@Role("admin"))
public void myMethod() {
// Runs if user has admin rôle.
}
```

@RunIf method must return void