import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface ExtractInterface {public String value();
}
@ExtractInterface("IMultiplier")
public class Multiplier {public int multiplay(int x ,int y) {int total = 0;for (int i = 0; i < x ; i++) {total = add(total,y);}return total;}private int add(int x,int y) {return x + y;}public static void main(String[] args) {Multiplier multiplier = new Multiplier();System.out.println("11*16 = " +multiplier.multiplay(11,16));}
}
import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.Modifier;
import com.sun.mirror.declaration.ParameterDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class InterfaceExtractorProcessor implements AnnotationProcessor { private final AnnotationProcessorEnvironment annotationProcessorEnvironment; private ArrayList<MethodDeclaration> interfaceMethods = new ArrayList<MethodDeclaration>(); public InterfaceExtractorProcessor(AnnotationProcessorEnvironment annotationProcessorEnvironment) {this.annotationProcessorEnvironment = annotationProcessorEnvironment;}@Overridepublic void process() {for (TypeDeclaration specifiedTypeDeclaration : annotationProcessorEnvironment.getSpecifiedTypeDeclarations()) {ExtractInterface annotation = specifiedTypeDeclaration.getAnnotation(ExtractInterface.class); if (annotation == null) {break;}for (MethodDeclaration method : specifiedTypeDeclaration.getMethods()) { if (method.getModifiers().contains(Modifier.PUBLIC) && !(method.getModifiers().contains(Modifier.STATIC))) { interfaceMethods.add(method);}}if (interfaceMethods.size() > 0) {try {PrintWriter sourceFile = annotationProcessorEnvironment.getFiler().createSourceFile(annotation.value());sourceFile.println("package"+specifiedTypeDeclaration.getPackage().getQualifiedName()+";"); sourceFile.println("public interface"+annotation.value()+" {");for (MethodDeclaration interfaceMethod : interfaceMethods) {sourceFile.println(" public");sourceFile.println(interfaceMethod.getReturnType() +" "); sourceFile.println(interfaceMethod.getSimpleName()+" ("); int i = 0;for (ParameterDeclaration parameter : interfaceMethod.getParameters()) { sourceFile.print(parameter.getType() + " " + parameter.getSimpleName());if (++i < interfaceMethod.getParameters().size()) {sourceFile.print(",");}}sourceFile.println(")");}sourceFile.println("}");sourceFile.close();}catch (IOException ioException) {throw new RuntimeException(ioException);}}}}
}
import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;import java.util.Collection;
import java.util.Collections;
import java.util.Set;
public class InterfaceExtractorProcessorFactory implements AnnotationProcessorFactory {@Overridepublic Collection<String> supportedOptions() {return Collections.emptySet();}@Overridepublic Collection<String> supportedAnnotationTypes() {return Collections.singleton("annotations.ExtractInterface");}@Overridepublic AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) {return new InterfaceExtractorProcessor(env);}
}