comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/verify/VerifyTruffleProcessor.java @ 21480:c2b006c5e15f

Testing the annotation processor using the @ExpectedError annotation as suggested by Christian Humer
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 25 May 2015 10:36:30 +0200
parents fb17e716b03c
children bb51b9a142b3
comparison
equal deleted inserted replaced
21479:2405d3b983cc 21480:c2b006c5e15f
32 import javax.lang.model.*; 32 import javax.lang.model.*;
33 import javax.lang.model.element.*; 33 import javax.lang.model.element.*;
34 import javax.tools.Diagnostic.Kind; 34 import javax.tools.Diagnostic.Kind;
35 35
36 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 36 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
37 import com.oracle.truffle.api.dsl.ExpectError;
37 import com.oracle.truffle.api.nodes.Node.Child; 38 import com.oracle.truffle.api.nodes.Node.Child;
38 39
39 @SupportedAnnotationTypes({"com.oracle.truffle.api.CompilerDirectives.TruffleBoundary", "com.oracle.truffle.api.nodes.Node.Child"}) 40 @SupportedAnnotationTypes({"com.oracle.truffle.api.CompilerDirectives.TruffleBoundary", "com.oracle.truffle.api.nodes.Node.Child"})
40 public class VerifyTruffleProcessor extends AbstractProcessor { 41 public class VerifyTruffleProcessor extends AbstractProcessor {
41 @Override 42 @Override
114 } 115 }
115 } 116 }
116 117
117 for (Element e : roundEnv.getElementsAnnotatedWith(Child.class)) { 118 for (Element e : roundEnv.getElementsAnnotatedWith(Child.class)) {
118 if (e.getModifiers().contains(Modifier.FINAL)) { 119 if (e.getModifiers().contains(Modifier.FINAL)) {
119 errorMessage(e, "@Child field cannot be final"); 120 emitError("@Child field cannot be final", e);
121 continue;
122 }
123 assertNoErrorExpected(e);
124 }
125 return false;
126 }
127
128 void assertNoErrorExpected(Element e) {
129 TypeElement eee = processingEnv.getElementUtils().getTypeElement(ExpectError.class.getName());
130 for (AnnotationMirror am : e.getAnnotationMirrors()) {
131 if (am.getAnnotationType().asElement() == eee) {
132 processingEnv.getMessager().printMessage(Kind.ERROR, "Expected an error, but none found!", e);
120 } 133 }
121 } 134 }
122 return false; 135 }
136
137 void emitError(String msg, Element e) {
138 TypeElement eee = processingEnv.getElementUtils().getTypeElement(ExpectError.class.getName());
139 for (AnnotationMirror am : e.getAnnotationMirrors()) {
140 if (am.getAnnotationType().asElement() == eee) {
141 Map<? extends ExecutableElement, ? extends AnnotationValue> vals = am.getElementValues();
142 if (vals.size() == 1) {
143 AnnotationValue av = vals.values().iterator().next();
144 if (av.getValue() instanceof List) {
145 List<?> arr = (List<?>) av.getValue();
146 for (Object o : arr) {
147 if (o instanceof AnnotationValue) {
148 AnnotationValue ov = (AnnotationValue) o;
149 if (msg.equals(ov.getValue())) {
150 return;
151 }
152 }
153 }
154 }
155 }
156 }
157 }
158 processingEnv.getMessager().printMessage(Kind.ERROR, msg, e);
123 } 159 }
124 160
125 /** 161 /**
126 * Determines if a given exception is (most likely) caused by <a 162 * Determines if a given exception is (most likely) caused by <a
127 * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>. 163 * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.