Strange: compiles as Annotation but not as a method
Hello,
I have an annotation like this:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ContextualPath
{
public Class<? extends MyInterface>[] nodes();
}
Now I define this annotation for a class:
@ContextualPath(nodes = { MyObject.class })
public class Foo
{
}
where MyObject implements MyInterface.
This compiles fine without problem. However it does not compile if
expressed within a method. Compiler errors for method
Bar.getContextualPath1:
* Type mismatch: cannot convert from Class<MyObject> to
Class<MyInterface>
* Cannot create a generic array of Class<MyInterface>
On the contrary, Bar.getContextualPath2 compiles fine.
class Bar
{
public Class<? extends IModel>[] getContextualPath1()
{
new Class<IModel>[]
{ MyObject.class };
}
public Class<? extends IModel>[] getContextualPath2()
{
// compiles without problem
return
getClass().getAnnotation(ContextualPathNodes.class).nodes();
}
}
There seems to be something special with annotations that I don't get.
Most of all I don't understand why it is allowed for an annotation but
not within a class. Would be gratefull if some knowledgeable person
out there could shed some light on this for me.
Thanks, Oliver