javac -cp $JAVA_HOME/lib/tools.jar MisoProcessor.java
javac -processor MisoProcessor Miso.java
java Miso 0
# --> true
Last active
January 24, 2018 05:13
-
-
Save wreulicke/2717c9e9e49ade151e0e755799bd7680 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
public class Miso { | |
public static void main(String... args){ | |
int n = Integer.parseInt(args[0]); | |
if (n == 1 && n == 2 && n == 3) { | |
System.out.println("true"); | |
} else { | |
System.out.println("false"); | |
} | |
} | |
} |
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.*; | |
import javax.annotation.processing.AbstractProcessor; | |
import javax.annotation.processing.ProcessingEnvironment; | |
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.TypeElement; | |
import com.sun.source.tree.*; | |
import com.sun.source.util.TreeScanner; | |
import com.sun.source.util.Trees; | |
import com.sun.tools.javac.model.JavacElements; | |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; | |
import com.sun.tools.javac.tree.*; | |
import com.sun.tools.javac.tree.JCTree.JCIf; | |
import com.sun.tools.javac.tree.TreeMaker; | |
@SupportedSourceVersion(SourceVersion.RELEASE_8) | |
@SupportedAnnotationTypes("*") | |
public class MisoProcessor extends AbstractProcessor{ | |
@Override | |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | |
JavacProcessingEnvironment env = (JavacProcessingEnvironment)processingEnv; | |
roundEnv.getRootElements().stream() | |
.forEach(e -> { | |
System.err.println(e); | |
Trees.instance(env) | |
.getPath(e) | |
.getCompilationUnit() | |
.accept(new Visitor(processingEnv, env), null); | |
}); | |
return false; | |
} | |
private class Visitor extends TreeScanner<Void, Void> { | |
JavacElements elements; | |
TreeMaker maker; | |
Visitor(ProcessingEnvironment processingEnv, JavacProcessingEnvironment env) { | |
elements = JavacElements.instance(env.getContext()); | |
maker = TreeMaker.instance(env.getContext()); | |
} | |
@Override | |
public Void visitIf(IfTree arg0, Void ignore) { | |
JCIf jcif = (JCIf) arg0; | |
jcif.cond = maker.Literal(true); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment