15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.processor;
26
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.NoSuchElementException;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38 import javax.annotation.processing.FilerException;
39 import javax.annotation.processing.ProcessingEnvironment;
40 import javax.lang.model.element.AnnotationMirror;
41 import javax.lang.model.element.AnnotationValue;
42 import javax.lang.model.element.Element;
43 import javax.lang.model.element.ExecutableElement;
44 import javax.lang.model.element.TypeElement;
45 import javax.lang.model.type.TypeMirror;
46 import javax.lang.model.util.ElementFilter;
47 import javax.tools.Diagnostic.Kind;
48 import javax.tools.FileObject;
49 import javax.tools.StandardLocation;
50
51 /**
52 * {@link javax.annotation.processing.AbstractProcessor} subclass that provides extra functionality.
53 */
54 @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", //
55 reason = "We want this type to be found when someone is writing a new Graal annotation processor")
56 public abstract class AbstractProcessor extends javax.annotation.processing.AbstractProcessor {
57
58 /**
59 * Gets the processing environment available to this processor.
60 */
61 public ProcessingEnvironment env() {
62 return processingEnv;
63 }
64
65 private final Map<String, TypeElement> types = new HashMap<>();
66
67 /**
68 * Gets the {@link TypeMirror} for a given class name.
69 *
70 * @throws NoClassDefFoundError if the class cannot be resolved
71 */
72 public TypeMirror getType(String className) {
73 return getTypeElement(className).asType();
74 }
75
76 /**
77 * Gets the {@link TypeMirror} for a given class name.
78 *
79 * @return {@code null} if the class cannot be resolved
80 */
81 public TypeMirror getTypeOrNull(String className) {
82 TypeElement element = getTypeElementOrNull(className);
83 if (element == null) {
|
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.processor;
26
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.NoSuchElementException;
35 import java.util.Set;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 import javax.annotation.processing.FilerException;
40 import javax.annotation.processing.ProcessingEnvironment;
41 import javax.annotation.processing.RoundEnvironment;
42 import javax.lang.model.element.AnnotationMirror;
43 import javax.lang.model.element.AnnotationValue;
44 import javax.lang.model.element.Element;
45 import javax.lang.model.element.ExecutableElement;
46 import javax.lang.model.element.TypeElement;
47 import javax.lang.model.type.TypeMirror;
48 import javax.lang.model.util.ElementFilter;
49 import javax.tools.Diagnostic.Kind;
50 import javax.tools.FileObject;
51 import javax.tools.StandardLocation;
52
53 /**
54 * {@link javax.annotation.processing.AbstractProcessor} subclass that provides extra functionality.
55 */
56 @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", //
57 reason = "We want this type to be found when someone is writing a new Graal annotation processor")
58 public abstract class AbstractProcessor extends javax.annotation.processing.AbstractProcessor {
59
60 /**
61 * Gets the processing environment available to this processor.
62 */
63 public ProcessingEnvironment env() {
64 return processingEnv;
65 }
66
67 @Override
68 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
69 // In JDK 8, each annotation processing round has its own Elements object
70 // so this cache must be cleared at the start of each round. As of JDK9,
71 // a single Elements is preserved across all annotation processing rounds.
72 // However, since both behaviors are compliant with the annotation processing
73 // specification, we unconditionally clear the cache to be safe.
74 types.clear();
75
76 return doProcess(annotations, roundEnv);
77 }
78
79 protected abstract boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv);
80
81 private final Map<String, TypeElement> types = new HashMap<>();
82
83 /**
84 * Gets the {@link TypeMirror} for a given class name.
85 *
86 * @throws NoClassDefFoundError if the class cannot be resolved
87 */
88 public TypeMirror getType(String className) {
89 return getTypeElement(className).asType();
90 }
91
92 /**
93 * Gets the {@link TypeMirror} for a given class name.
94 *
95 * @return {@code null} if the class cannot be resolved
96 */
97 public TypeMirror getTypeOrNull(String className) {
98 TypeElement element = getTypeElementOrNull(className);
99 if (element == null) {
|