annotations' inheritance, polymorphism, 'cycle detected' limitations are...
... giving me a headache.
Hi all,
What I would like to do is specify access rights on some methods, like so:
@Or({@Role("Administrator"),
@Role("Engineer")
})
publicvoid someMethod();
This reads like "all persons having the administrator role or the Engineer role have access to the someMethod() method".
The annotations would then look like this:
@interface Role{
String value();
}
@interface Or{
Role[] values();
}
This works fine. However, when I want to add some more complexity, things fall apart. The following for example does not work:
@Or({@Role("Administrator"),
@And({@Role("Engineer"),
@Role("Tester")
})
})
publicvoid someMethod();
The reason this cannot work is that annotations don't support inheritance - so you cannot create a common super type for both the @Role and the @And annotation, and so the values() method of the @Or annotation cannot return a array that can contain both @Role and @And annotations.
So I tried something different. The idea was to do something like this:
@Check( name="Or"
children={@Check(name="Administrator"),
@Check(name="And"
children{ @Check(name="Engineer"),
@Check(name="Tester")
})
})
publicvoid someMethod();
But this too is not possible. Not only is there the problem about how to specify a default value for the children(), the compiler is also giving me the following exception: "Cycle detected: the annotation type Check cannot contain attributes of the annotation type itself". Why is that?
Here's that annotation definition:
@interface Check{
String name()default"";
Check[] children() default?;
}
Anybody has an idea? Or should I leave this track altogether, and try to solve it without annotations? Thanks!

