0

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

0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

so there is no way for me to get to that information at runtime...?
From the documentation: 'SOURCE Annotations are to be discarded by the compiler.'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.