Debian Codegraph @

runtime-testsuite/processors/src/org/antlr/v4/test/runtime/CommentHasStringValueProcessor.java

raw
1 /*
2 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3 * Use of this file is governed by the BSD 3-clause license that
4 * can be found in the LICENSE.txt file in the project root.
5 */
6
7 package org.antlr.v4.test.runtime;
8
9 import com.sun.tools.javac.main.JavaCompiler;
10 import com.sun.tools.javac.model.JavacElements;
11 import com.sun.tools.javac.tree.JCTree;
12 import com.sun.tools.javac.tree.TreeMaker;
13 import com.sun.tools.javac.util.Context;
14 import com.sun.tools.javac.util.List;
15
16 import javax.annotation.processing.AbstractProcessor;
17 import javax.annotation.processing.ProcessingEnvironment;
18 import javax.annotation.processing.RoundEnvironment;
19 import javax.annotation.processing.SupportedAnnotationTypes;
20 import javax.annotation.processing.SupportedSourceVersion;
21 import javax.lang.model.SourceVersion;
22 import javax.lang.model.element.Element;
23 import javax.lang.model.element.TypeElement;
24 import java.lang.reflect.Field;
25 import java.util.Set;
26
27 /**
28 I think I figured out how to use annotation processors in maven. It's
29 more or less automatic and you don't even need to tell maven, with one minor
30 exception. The idea is to create a project for the annotation and another
31 for the annotation processor. Then, a project that uses the annotation
32 can simply set up the dependency on the other projects. You have to turn
33 off processing, -proc:none on the processor project itself but other than
34 that, java 6+ more or less tries to apply any processors it finds during
35 compilation. maven just works.
36
37 Also you need a META-INF/services/javax.annotation.processing.Processor file
38 with "org.antlr.v4.test.runtime.CommentHasStringValueProcessor" in it.
39 */
40 @SupportedAnnotationTypes({"org.antlr.v4.test.runtime.CommentHasStringValue"})
41 @SupportedSourceVersion(SourceVersion.RELEASE_7)
42 public class CommentHasStringValueProcessor extends AbstractProcessor {
43
44 protected JavacElements utilities;
45 protected TreeMaker treeMaker;
46
47 @Override
48 public synchronized void init(ProcessingEnvironment processingEnv) {
49 super.init(processingEnv);
50 // Messager messager = processingEnv.getMessager();
51 // messager.printMessage(Diagnostic.Kind.NOTE, "WOW INIT--------------------");
52 utilities = (JavacElements)processingEnv.getElementUtils();
53 treeMaker = TreeMaker.instance(extractContext(utilities));
54 }
55
56 private static Context extractContext(JavacElements utilities) {
57 try {
58 Field compilerField = JavacElements.class.getDeclaredField("javaCompiler");
59 compilerField.setAccessible(true);
60 JavaCompiler compiler = (JavaCompiler)compilerField.get(utilities);
61 Field contextField = JavaCompiler.class.getDeclaredField("context");
62 contextField.setAccessible(true);
63 return (Context)contextField.get(compiler);
64 } catch (NoSuchFieldException | IllegalAccessException e) {
65 throw new IllegalStateException(e);
66 }
67 }
68
69 @Override
70 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
71 // Messager messager = processingEnv.getMessager();
72 // messager.printMessage(Diagnostic.Kind.NOTE, "PROCESS--------------------");
73 Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(CommentHasStringValue.class);
74 for (Element annotatedElement : annotatedElements) {
75 String docComment = utilities.getDocComment(annotatedElement);
76 JCTree.JCLiteral literal = treeMaker.Literal(docComment!=null ? docComment : "");
77 JCTree elementTree = utilities.getTree(annotatedElement);
78 if ( elementTree instanceof JCTree.JCVariableDecl ) {
79 ((JCTree.JCVariableDecl)elementTree).init = literal;
80 }
81 else if ( elementTree instanceof JCTree.JCMethodDecl ) {
82 JCTree.JCStatement[] statements = new JCTree.JCStatement[1];
83 statements[0] = treeMaker.Return(literal);
84 ((JCTree.JCMethodDecl)elementTree).body = treeMaker.Block(0, List.from(statements));
85 }
86 }
87 return true;
88 }
89
90 @Override
91 public SourceVersion getSupportedSourceVersion() {
92 return SourceVersion.latestSupported();
93 }
94 }