Created
December 6, 2015 15:18
-
-
Save freestrings/e733342c3b4e78a050dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mvn clean package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Dummy { | |
private String name; | |
@HelloWorld | |
public String getName() { | |
return this.name; | |
} | |
public static void main(String... args) { | |
new Dummy().getName(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Set; | |
import javax.annotation.processing.AbstractProcessor; | |
import javax.annotation.processing.RoundEnvironment; | |
import javax.annotation.processing.SupportedAnnotationTypes; | |
import javax.annotation.processing.SupportedSourceVersion; | |
import javax.lang.model.SourceVersion; | |
import javax.lang.model.element.Element; | |
import javax.lang.model.element.TypeElement; | |
import com.sun.tools.javac.model.JavacElements; | |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; | |
import com.sun.tools.javac.tree.JCTree; | |
import com.sun.tools.javac.tree.JCTree.JCExpression; | |
import com.sun.tools.javac.tree.JCTree.JCLiteral; | |
import com.sun.tools.javac.tree.TreeMaker; | |
import com.sun.tools.javac.util.Context; | |
import com.sun.tools.javac.util.List; | |
import com.sun.tools.javac.util.Name; | |
@SupportedAnnotationTypes({ "HelloWorld" }) | |
@SupportedSourceVersion(SourceVersion.RELEASE_8) | |
public class HelloProcessor extends AbstractProcessor { | |
@Override | |
public boolean process( | |
Set<? extends TypeElement> annotations, | |
RoundEnvironment roundEnv) { | |
if (roundEnv.processingOver()) { | |
return false; | |
} | |
JavacProcessingEnvironment environment | |
= (JavacProcessingEnvironment) processingEnv; | |
Context context | |
= environment.getContext(); | |
TreeMaker maker | |
= TreeMaker.instance(context); | |
JavacElements elemUtils | |
= (JavacElements) processingEnv.getElementUtils(); | |
Class<HelloWorld> clazz = HelloWorld.class; | |
Set<? extends Element> elements | |
= roundEnv.getElementsAnnotatedWith(clazz); | |
for (Element element : elements) { | |
JCTree tree = elemUtils.getTree(element); | |
JCTree.JCMethodDecl mDecl | |
= (JCTree.JCMethodDecl) tree; | |
injectHelloWorld(maker, elemUtils, mDecl); | |
} | |
return false; | |
} | |
private void injectHelloWorld( | |
TreeMaker maker, | |
JavacElements elemUtils, | |
JCTree.JCMethodDecl mDecl) { | |
maker.pos = mDecl.pos; | |
List<JCExpression> nil = List.<JCTree.JCExpression> nil(); | |
Name system = elemUtils.getName("System"); | |
Name out = elemUtils.getName("out"); | |
Name _println = elemUtils.getName("println"); | |
JCLiteral helloworld = maker.Literal("Hello world"); | |
mDecl.body = maker.Block(0,List.of( | |
maker.Exec(maker.Apply(nil, | |
maker.Select(maker.Select( | |
maker.Ident( | |
system), | |
out), | |
_println), | |
List.<JCTree.JCExpression> of(helloworld))), | |
mDecl.body) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public @interface { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>freestrings.apt</groupId> | |
<artifactId>apt-playground</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
<!-- Disable annotation processing for ourselves. --> | |
<compilerArgument>-proc:none</compilerArgument> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>com.sun</groupId> | |
<artifactId>tools</artifactId> | |
<version>1.8.0</version> | |
<scope>system</scope> | |
<systemPath>/usr/lib/jvm/java-8-openjdk-amd64/lib/tools.jar</systemPath> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac -cp target/apt-playground-1.0-SNAPSHOT.jar -processor HelloProcessor Dummy.java |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment