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
Daniel Pitts - 06 Dec 2007 20:25 GMT
> Hello,
>
[quoted text clipped - 7 lines]
> public Class<? extends MyInterface>[] nodes();
> }
You're mixing generics with arrays... Can't do that unfortunately. I
don't really see a way around that in this case either.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Olli Plough - 07 Dec 2007 09:40 GMT
> You're mixing generics with arrays... Can't do that unfortunately. I
> don't really see a way around that in this case either.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Hi Daniel,
thanks for that one. Really didn't know this.
Cheers, Olli