Answer the question
In order to leave comments, you need to log in
How to replace code in an existing class using an annotation processor?
There are 2 annotations:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Ann {
public String description() default "AAA";
public String template() default "BBB";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface AnotherAnn {
public String text() default "Text";
}
public class App {
@Ann(description = "Yo!", template = "Bitches.")
public void someMethod(){}
}
@SupportedAnnotationTypes("alexiuscrow.annotation.Ann")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Proc extends AbstractProcessor {
public Proc() {
super();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
/* TODO DIRTY WORK (ANNOTATION REPLACEMENT)*/
Class<App> appClass = App.class;
for (Method method: appClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(AnotherAnn.class)) {
AnotherAnn anotherAnn = method.getAnnotation(AnotherAnn.class);
System.out.println("Method: \t" + method.getName());
System.out.println("AnotherAnn: \t" + anotherAnn);
System.out.println("Text: \t" + anotherAnn.text());
}
}
return true;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question