Im trying to create my own annotation and parse it . But when i try to get the methods that has my annotation using reflection Im always getting false.
Googled custom annotations in web but still not able to find the exact issue .
Below is the annotation class and the class that uses it and parse it.
Annotation:
package AnnotationsImpl;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
int custom_int() default 11;
String custom_str();
}
Class where annotation is used :
package AnnotationsImpl;
public class AnnotationImpl extends AnnotationExample {
//@CustomAnnotation(custom_str="str1")
int a=2;
@SuppressWarnings("deprecation")
@CustomAnnotation(custom_str="str1")
public static void main(String[] args){
AnnotationExample ai = new AnnotationExample();
ai.overrideMethod();
AnnotationImpl aaa = new AnnotationImpl();
aaa.overrideMethod();
aaa.testingCA();
ai.myDeprecatedMethod();
}
@Override
public void overrideMethod(){
System.out.println("In child class");
}
@CustomAnnotation(custom_str="str1")
public static void testingCA(){
System.out.println("custom annotation");
}
}
Parser :
package AnnotationsImpl;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class AnnotationParser {
public static void main(String[] args)throws Exception{
for(//AnnotationParser.class.getClassLoader().loadClass("AnnotationsImpl.AnnotationImpl")//
Method method : Class.forName("AnnotationsImpl.AnnotationImpl").getMethods()
){
System.out.print("method name :"+method.getName());
System.out.print(" *** Custom annotation present :"+method.isAnnotationPresent(CustomAnnotation.class));
System.out.println();
if(method.isAnnotationPresent(AnnotationsImpl.CustomAnnotation.class)){
System.out.println("custom present");
for(Annotation ann : method.getDeclaredAnnotations()){
System.out.println("Annotation ann :"+ann +"=== method :::"+method);
CustomAnnotation cu = method.getAnnotation(CustomAnnotation.class);
}
}
}
}
}
output :
*run:
method name :main *** Custom annotation present :false
method name :check *** Custom annotation present :false
method name :overrideMethod *** Custom annotation present :false
method name :testingCA *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :wait *** Custom annotation present :false
method name :equals *** Custom annotation present :false
method name :toString *** Custom annotation present :false
method name :hashCode *** Custom annotation present :false
method name :getClass *** Custom annotation present :false
method name :notify *** Custom annotation present :false
method name :notifyAll *** Custom annotation present :false
BUILD SUCCESSFUL (total time: 0 seconds)*
Tried RUNTIME retention policy. Why am i getting annotation present false always ?What mistake have I done? Thanks in advance .
Aucun commentaire:
Enregistrer un commentaire