Created
February 25, 2019 02:15
-
-
Save mageddo/069420b654b6f20b8a5b5478ce44b1f6 to your computer and use it in GitHub Desktop.
JavaParserSamples
This file contains 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
// java parser 3.13 java 8+ | |
package com.mageddo.rawstringliterals; | |
import com.github.javaparser.JavaParser; | |
import com.github.javaparser.JavaParserBuild; | |
import com.github.javaparser.ParserConfiguration; | |
import com.github.javaparser.StaticJavaParser; | |
import com.github.javaparser.ast.CompilationUnit; | |
import com.github.javaparser.ast.Node; | |
import com.github.javaparser.ast.NodeList; | |
import com.github.javaparser.ast.body.MethodDeclaration; | |
import com.github.javaparser.ast.body.TypeDeclaration; | |
import com.github.javaparser.ast.stmt.BlockStmt; | |
import com.github.javaparser.ast.stmt.ExpressionStmt; | |
import com.github.javaparser.ast.stmt.Statement; | |
import com.sun.source.tree.AnnotationTree; | |
import com.sun.source.tree.ClassTree; | |
import com.sun.source.tree.CompilationUnitTree; | |
import com.sun.source.tree.Tree; | |
import com.sun.source.util.SimpleTreeVisitor; | |
import com.sun.source.util.TreePath; | |
import com.sun.source.util.TreePathScanner; | |
import com.sun.source.util.Trees; | |
import com.sun.tools.javac.api.ClientCodeWrapper; | |
import com.sun.tools.javac.code.Flags; | |
import com.sun.tools.javac.code.Symbol; | |
import com.sun.tools.javac.code.Symbol.ClassSymbol; | |
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.JCCompilationUnit; | |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; | |
import com.sun.tools.javac.tree.TreeMaker; | |
import com.sun.tools.javac.tree.TreeTranslator; | |
import jdk.nashorn.internal.ir.ExpressionStatement; | |
import javax.annotation.processing.AbstractProcessor; | |
import javax.annotation.processing.ProcessingEnvironment; | |
import javax.annotation.processing.RoundEnvironment; | |
import javax.annotation.processing.SupportedAnnotationTypes; | |
import javax.lang.model.SourceVersion; | |
import javax.lang.model.element.Element; | |
import javax.lang.model.element.TypeElement; | |
import javax.tools.FileObject; | |
import javax.tools.JavaFileObject; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.Reader; | |
import java.nio.file.Files; | |
import java.util.Arrays; | |
import java.util.Set; | |
import static sun.misc.Version.println; | |
@SupportedAnnotationTypes(References.RAW_STRING_REF) | |
public final class JavacMultilineProcessor extends AbstractProcessor { | |
private JavacElements elementUtils; | |
private TreeMaker maker; | |
private Trees trees; | |
@Override | |
public void init(final ProcessingEnvironment procEnv) { | |
super.init(procEnv); | |
JavacProcessingEnvironment javacProcessingEnv = (JavacProcessingEnvironment) procEnv; | |
this.elementUtils = javacProcessingEnv.getElementUtils(); | |
this.maker = TreeMaker.instance(javacProcessingEnv.getContext()); | |
trees = Trees.instance(processingEnv); | |
} | |
@Override | |
public SourceVersion getSupportedSourceVersion() { | |
return SourceVersion.latest(); | |
} | |
@Override | |
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) { | |
for (final Element element : roundEnv.getElementsAnnotatedWith(getAnnotation())) { | |
if (!(element instanceof ClassSymbol)){ | |
continue; | |
} | |
final ClassSymbol classSymbol = (ClassSymbol) element; | |
if (classSymbol.sourcefile.getKind() == JavaFileObject.Kind.SOURCE) { | |
try { | |
final Reader r = classSymbol.sourcefile.openReader(true); | |
try { | |
final CompilationUnit cu = StaticJavaParser.parse(r); | |
cu.getComments(); | |
for (final TypeDeclaration<?> type : cu.getTypes()) { | |
for (final MethodDeclaration method : type.getMethods()) { | |
for (Node childNode : method.getChildNodes()) { | |
if(childNode instanceof BlockStmt){ | |
final BlockStmt methodBody = (BlockStmt) childNode; | |
for (Statement stm : methodBody.getStatements()) { | |
if(stm instanceof ExpressionStmt){ | |
final ExpressionStmt expressionStmt = (ExpressionStmt) stm; | |
expressionStmt.getcom | |
} | |
} | |
} | |
} | |
} | |
} | |
// .get(0).getBody().get().getStatements().get(0).getComment(); | |
// cu.getTypes().get(0).getMethods().get(0).getBody().get().getStatements().get(0).getChildNodes().get(0); | |
} finally { | |
r.close(); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
// new JavaParser(new ParserConfiguration().) | |
// JavaParserBuild. | |
// roundEnv.getElementsAnnotatedWith(null).iterator().next().get | |
final TreePathScanner<Object, CompilationUnitTree> scanner = new TreePathScanner<Object, CompilationUnitTree>() { | |
@Override | |
public Trees visitClass(final ClassTree classTree, final CompilationUnitTree unitTree) { | |
// StaticJavaParser.parse(new FileInputStream()) | |
for (Tree member : classTree.getMembers()) { | |
if(member.getKind() == Tree.Kind.METHOD){ | |
// member.accept(new SimpleTreeVisitor<String, String>(){ | |
// | |
// }); | |
} | |
} | |
if (unitTree instanceof JCCompilationUnit) { | |
final JCCompilationUnit compilationUnit = (JCCompilationUnit) unitTree; | |
if (compilationUnit.sourcefile.getKind() != JavaFileObject.Kind.SOURCE) { | |
return trees; | |
} | |
compilationUnit.accept(new TreeTranslator(){ | |
@Override | |
public void visitAnnotation(JCTree.JCAnnotation jcAnnotation) { | |
System.out.println(jcAnnotation.getAnnotationType()); | |
super.visitAnnotation(jcAnnotation); | |
} | |
@Override | |
public void visitMethodDef(JCTree.JCMethodDecl jcMethodDecl) { | |
super.visitMethodDef(jcMethodDecl); | |
// jcMethodDecl.getBody().getStatements().get(0).mods.annotations | |
JCVariableDecl jcStatement = (JCVariableDecl) jcMethodDecl.getBody().getStatements().get(0); | |
// jcMethodDecl. | |
// elementUtils.getDocComment(jcStatement) | |
// jcMethodDecl.mods.annotations | |
} | |
}); | |
// compilationUnit.accept(new TreeTranslator() { | |
// public void visitVarDef(final JCVariableDecl tree) { | |
// super.visitVarDef(tree); | |
// | |
// if ((tree.mods.flags & Flags.FINAL) == 0) { | |
// tree.mods.flags |= Flags.FINAL; | |
// } | |
// } | |
// }); | |
// if (unitTree instanceof JCCompilationUnit) { | |
// final JCCompilationUnit compilationUnit = (JCCompilationUnit) unitTree; | |
// | |
// // Only process on files which have been compiled from source | |
// if (compilationUnit.sourcefile.getKind() == JavaFileObject.Kind.SOURCE) { | |
// compilationUnit.accept(new TreeTranslator() { | |
// public void visitVarDef(final JCVariableDecl tree) { | |
// super.visitVarDef(tree); | |
// | |
// if ((tree.mods.flags & Flags.FINAL) == 0) { | |
// tree.mods.flags |= Flags.FINAL; | |
// } | |
// } | |
// }); | |
// } | |
} | |
return trees; | |
} | |
@Override | |
public Object visitAnnotation(AnnotationTree annotationTree, CompilationUnitTree compilationUnitTree) { | |
System.out.println("visitou annotation"); | |
return super.visitAnnotation(annotationTree, compilationUnitTree); | |
} | |
}; | |
Set<? extends Element> fields = roundEnv.getElementsAnnotatedWith(getAnnotation()); | |
for (Element field : fields) { | |
String docComment = elementUtils.getDocComment(field); | |
if (docComment != null) { | |
JCVariableDecl fieldNode = (JCVariableDecl) elementUtils.getTree(field); | |
fieldNode.init = maker.Literal(StringProcessor.toString(docComment, field.getAnnotation(getAnnotation()))); | |
} | |
} | |
if (!roundEnv.getElementsAnnotatedWith(RawString.class).isEmpty()) { | |
// final TreePath path = trees.getPath((Element) roundEnv.getRootElements().toArray()[0]); | |
TreePath path = trees.getPath(roundEnv.getRootElements().iterator().next().getEnclosedElements().get(2)); | |
scanner.scan(path, path.getCompilationUnit()); | |
// scanner.scan(path, path.getCompilationUnit()); | |
} | |
return true; | |
} | |
private Class<RawString> getAnnotation() { | |
return RawString.class; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment