Reputation: 5735
I'm trying to spot, using reflection, the 'init' method that is annotaded with the @Override annotation (or with whatever annotation), but ok, heres my example, much simplified, ofcourse Base class:
public abstract class A
{
public void init()
{
}
}
Then here the subclass:
public class B extends A
{
String bla;
@Override
public void init()
{
}
public void init(String bla)
{
this.bla=bla;
}
}
So the code i run to get the annotated method is this:
public static void main(String[] args)
{
ClassLoader c = Main.class.getClassLoader();
try
{
Class<?> clazz = c.loadClass("correct.path.to.class.B");
for (Method method : clazz.getDeclaredMethods())
{
if (method.getName().equals("init"))
{
System.out.println(method.getDeclaredAnnotations().length);
}
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
Both methods are correctly found but surprisingly, i get '0' twice when reading the length of the arrays containing the annotations, any idea whats wrong here?
The method getAnnotation()
gives me the same results
Upvotes: 0
Views: 61
Reputation: 18170
Check the documentation for @Override
and RetentionPolicy
. Basically, the @Override
annotation is not available at runtime, it's a source only annotation.
http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html
Upvotes: 2