6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 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 * @test 26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080 27 * @summary Tests that getElementsAnnotatedWith works properly. 28 * @author Joseph D. Darcy 29 * @library /tools/javac/lib 30 * @modules java.compiler 31 * jdk.compiler 32 * @build JavacTestingAbstractProcessor 33 * @compile TestElementsAnnotatedWith.java 34 * @compile InheritedAnnotation.java 35 * @compile TpAnno.java 36 * @compile Anno.java 37 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 38 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 39 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 40 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 41 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 42 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 43 * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java 44 * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 45 * @compile Foo.java 46 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 47 */ 48 49 import java.lang.annotation.Annotation; 50 import java.util.Collections; 51 import java.util.Set; 52 import java.util.HashSet; 53 import java.util.Arrays; 54 import javax.annotation.processing.*; 55 import javax.lang.model.element.*; 56 import static javax.lang.model.util.ElementFilter.*; 57 58 /** 59 * This processor verifies that the information returned by 60 * getElementsAnnotatedWith is consistent with the expected results 61 * stored in an AnnotatedElementInfo annotation. 62 */ 63 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 64 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 65 66 public boolean process(Set<? extends TypeElement> annotations, 67 RoundEnvironment roundEnvironment) { 68 TypeElement annotatedElementInfoElement = 69 elements.getTypeElement("AnnotatedElementInfo"); 70 Set<? extends Element> resultsMeta = Collections.emptySet(); 71 Set<? extends Element> resultsBase = Collections.emptySet(); 72 73 if (!roundEnvironment.processingOver()) { 74 testNonAnnotations(roundEnvironment); 75 76 // Verify AnnotatedElementInfo is present on the first 77 // specified type. 78 79 TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); 80 81 AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); 82 83 boolean failed = false; 84 85 if (annotatedElementInfo == null) 86 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + 87 firstType); 88 else { 89 // Verify that the annotation information is as 90 // expected. 91 92 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); 93 94 resultsMeta = 95 roundEnvironment. 96 getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName())); 97 98 if (!resultsMeta.isEmpty()) 99 System.err.println("Results: " + resultsMeta); 100 101 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { 102 failed = true; 103 System.err.printf("Bad number of elements; expected %d, got %d%n", 104 annotatedElementInfo.expectedSize(), resultsMeta.size()); 105 } else { 106 for(Element element : resultsMeta) { 107 String simpleName = element.getSimpleName().toString(); 108 if (!expectedNames.contains(simpleName) ) { 109 failed = true; 110 System.err.println("Name ``" + simpleName + "'' not expected."); 111 } 112 } 113 } 114 } 115 116 resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); 117 118 if (!resultsMeta.equals(resultsBase)) { 119 failed = true; 120 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 121 "\nbase: " + resultsBase); 122 } 123 124 if (failed) { 125 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 126 throw new RuntimeException(); 127 } 128 } else { 129 // If processing is over without an error, the specified 130 // elements should be empty so an empty set should be returned. 131 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); 132 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); 133 if (!resultsMeta.isEmpty()) 134 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); 135 if (!resultsBase.isEmpty()) 136 throw new RuntimeException("Nonempty resultsBase: " + resultsBase); 137 138 } 139 return true; 140 } 141 142 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { 143 try { 144 return roundEnvironment. 145 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 146 } catch(ClassNotFoundException cnfe) { 147 throw new RuntimeException(cnfe); 148 } 149 } 150 151 /** 152 * Verify non-annotation types result in 153 * IllegalArgumentExceptions. 154 */ 155 private void testNonAnnotations(RoundEnvironment roundEnvironment) { 156 try { 157 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); 158 throw new RuntimeException("Illegal argument exception not thrown"); 159 } catch(IllegalArgumentException iae) {} 160 161 try { 162 Set<? extends Element> elements = 163 roundEnvironment.getElementsAnnotatedWith(processingEnv. 164 getElementUtils(). 165 getTypeElement("java.lang.Object") ); 166 throw new RuntimeException("Illegal argument exception not thrown"); 167 } catch(IllegalArgumentException iae) {} 168 } 169 } | 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 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 * @test 26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080 8032230 27 * @summary Tests that getElementsAnnotatedWith works properly. 28 * @author Joseph D. Darcy 29 * @library /tools/javac/lib 30 * @modules java.compiler 31 * jdk.compiler 32 * @build JavacTestingAbstractProcessor 33 * @compile TestElementsAnnotatedWith.java 34 * @compile InheritedAnnotation.java 35 * @compile TpAnno.java 36 * @compile Anno.java 37 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 38 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 39 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 40 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 41 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 42 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 43 * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java 44 * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 45 * @compile Foo.java 46 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 47 */ 48 49 import java.lang.annotation.Annotation; 50 import java.util.Collections; 51 import java.util.Set; 52 import java.util.HashSet; 53 import java.util.Arrays; 54 import java.util.Objects; 55 import javax.annotation.processing.*; 56 import javax.lang.model.element.*; 57 import static javax.lang.model.util.ElementFilter.*; 58 59 /** 60 * This processor verifies that the information returned by 61 * getElementsAnnotatedWith and getElementsAnnotatedWithAny is 62 * consistent with the expected results stored in an 63 * AnnotatedElementInfo annotation. 64 */ 65 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 66 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 67 68 public boolean process(Set<? extends TypeElement> annotations, 69 RoundEnvironment roundEnv) { 70 // First check sets of annotated elements using the round 71 // environment from the annotation processing tool framework. 72 checkSetOfAnnotatedElements(roundEnv); 73 74 // Next check sets of annotated elements using a round 75 // environment which uses the default implementations of the 76 // getElementsAnnotatedWithAny methods from the interface. 77 checkSetOfAnnotatedElements(new TestingRoundEnvironment(roundEnv)); 78 return true; 79 } 80 81 private class TestingRoundEnvironment implements RoundEnvironment { 82 private RoundEnvironment re; 83 84 public TestingRoundEnvironment(RoundEnvironment re) { 85 this.re = re; 86 } 87 88 @Override 89 public boolean errorRaised() { 90 return re.errorRaised(); 91 } 92 93 @Override 94 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { 95 return re.getElementsAnnotatedWith(a); 96 } 97 98 @Override 99 public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> a) { 100 // Default method defined in the interface 101 return RoundEnvironment.super.getElementsAnnotatedWithAny(a); 102 } 103 104 @Override 105 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 106 return re.getElementsAnnotatedWith(a); 107 } 108 109 @Override 110 public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... a) { 111 // Default method defined in the interface 112 return RoundEnvironment.super.getElementsAnnotatedWithAny(a); 113 } 114 115 @Override 116 public Set<? extends Element> getRootElements() { 117 return re.getRootElements(); 118 } 119 120 @Override 121 public boolean processingOver() { 122 return re.processingOver(); 123 } 124 125 } 126 127 /** 128 * The method checks the following conditions: 129 * 130 * 1) The sets of elements found are equal for the TypeElement and 131 * Class<? extends Annotation> methods on logically equivalent 132 * arguments. 133 * 134 * 2) getElementsAnnotatedWithAny(X) is equal to 135 * getElementsAnnotatedWith(X') where X is a set/var-args array 136 * with one element and X' is the element. 137 * 138 * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to 139 * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y). 140 */ 141 void checkSetOfAnnotatedElements(RoundEnvironment re) { 142 TypeElement annotatedElemInfoElem = elements.getTypeElement("AnnotatedElementInfo"); 143 144 // For the "Any" methods, search for both the expected 145 // annotation and AnnotatedElementInfo and verify the return 146 // set is the union of searching for AnnotatedElementInfo and 147 // the other annotation 148 Set<? extends Element> resultsMeta = Collections.emptySet(); 149 Set<? extends Element> resultsMetaAny = Collections.emptySet(); 150 Set<Element> resultsMetaMulti = new HashSet<>(); 151 Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet(); 152 Set<? extends Element> resultsBase = Collections.emptySet(); 153 Set<? extends Element> resultsBaseAny = Collections.emptySet(); 154 Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet(); 155 156 if (!re.processingOver()) { 157 testNonAnnotations(re); 158 159 // Verify AnnotatedElementInfo is present on the first 160 // specified type. 161 162 TypeElement firstType = typesIn(re.getRootElements()).iterator().next(); 163 164 AnnotatedElementInfo annotatedElemInfo = 165 firstType.getAnnotation(AnnotatedElementInfo.class); 166 167 boolean failed = false; 168 169 Objects.requireNonNull(annotatedElemInfo, 170 "Missing AnnotatedElementInfo annotation on " + firstType); 171 172 // Verify that the annotation information is as expected. 173 Set<String> expectedNames = 174 new HashSet<>(Arrays.asList(annotatedElemInfo.names())); 175 176 String annotationName = annotatedElemInfo.annotationName(); 177 TypeElement annotationTypeElem = elements.getTypeElement(annotationName); 178 179 resultsMeta = re.getElementsAnnotatedWith(annotationTypeElem); 180 resultsMetaAny = re.getElementsAnnotatedWithAny(annotationTypeElem); 181 resultsMetaMulti.addAll(resultsMeta); 182 resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem)); 183 resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem); 184 185 if (!resultsMeta.isEmpty()) 186 System.err.println("Results: " + resultsMeta); 187 188 if (!resultsMeta.equals(resultsMetaAny)) { 189 failed = true; 190 System.err.printf("Inconsistent Meta with vs withAny results"); 191 } 192 193 if (resultsMeta.size() != annotatedElemInfo.expectedSize()) { 194 failed = true; 195 System.err.printf("Bad number of elements; expected %d, got %d%n", 196 annotatedElemInfo.expectedSize(), resultsMeta.size()); 197 } else { 198 for(Element element : resultsMeta) { 199 String simpleName = element.getSimpleName().toString(); 200 if (!expectedNames.contains(simpleName) ) { 201 failed = true; 202 System.err.println("Name ``" + simpleName + "'' not expected."); 203 } 204 } 205 } 206 207 resultsBase = computeResultsBase(re, annotationName); 208 resultsBaseAny = computeResultsBaseAny(re, annotationName); 209 try { 210 Set<Class<? extends Annotation>> tmp = new HashSet<>(); 211 tmp.add(AnnotatedElementInfo.class); 212 tmp.add(Class.forName(annotationName).asSubclass(Annotation.class)); 213 resultsBaseAnyMulti = re.getElementsAnnotatedWithAny(tmp); 214 } catch (ClassNotFoundException e) { 215 throw new RuntimeException(e); 216 } 217 218 if (!resultsBase.equals(resultsBaseAny)) { 219 failed = true; 220 System.err.printf("Inconsistent Base with vs withAny results"); 221 } 222 223 if (!resultsMeta.equals(resultsBase)) { 224 failed = true; 225 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 226 "\nbase: " + resultsBase); 227 } 228 229 if (!resultsMetaAnyMulti.equals(resultsMetaMulti)) { 230 failed = true; 231 System.err.println("MetaMultAny and MetaMulti sets unequal;\n meta: " + resultsMeta + 232 "\nbase: " + resultsBase); 233 } 234 235 if (!resultsBaseAnyMulti.equals(resultsMetaAnyMulti)) { 236 failed = true; 237 System.err.println("BaseMulti and MetaMulti sets unequal;\n meta: " + resultsMeta + 238 "\nbase: " + resultsBase); 239 } 240 241 if (failed) { 242 System.err.println("AnnotatedElementInfo: " + annotatedElemInfo); 243 throw new RuntimeException(); 244 } 245 } else { 246 // If processing is over without an error, the specified 247 // elements should be empty so an empty set should be 248 // returned. 249 throwOnNonEmpty(re.getElementsAnnotatedWith(annotatedElemInfoElem), "resultsMeta"); 250 throwOnNonEmpty(re.getElementsAnnotatedWithAny(annotatedElemInfoElem), "resultsMetaAny"); 251 throwOnNonEmpty(re.getElementsAnnotatedWith(AnnotatedElementInfo.class), "resultsBase"); 252 throwOnNonEmpty(re.getElementsAnnotatedWithAny(Set.of(AnnotatedElementInfo.class)), "resultsBaseAny"); 253 } 254 } 255 256 private void throwOnNonEmpty(Set<? extends Element> results, String message) { 257 if (!results.isEmpty()) { 258 throw new RuntimeException("Nonempty " + message + "\t" + results); 259 } 260 } 261 262 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnv, String name) { 263 try { 264 return roundEnv. 265 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 266 } catch (ClassNotFoundException cnfe) { 267 throw new RuntimeException(cnfe); 268 } 269 } 270 271 private Set<? extends Element> computeResultsBaseAny(RoundEnvironment roundEnv, String name) { 272 try { 273 return roundEnv. 274 getElementsAnnotatedWithAny(Set.of(Class.forName(name).asSubclass(Annotation.class))); 275 } catch (ClassNotFoundException cnfe) { 276 throw new RuntimeException(cnfe); 277 } 278 } 279 280 /** 281 * Verify non-annotation types result in 282 * IllegalArgumentExceptions. 283 */ 284 private void testNonAnnotations(RoundEnvironment roundEnv) { 285 Class objectClass = (Class)Object.class; 286 Set<? extends Element> elements; 287 try { 288 elements = roundEnv.getElementsAnnotatedWith(objectClass); 289 throw new RuntimeException("Illegal argument exception not thrown"); 290 } catch (IllegalArgumentException iae) {} 291 292 try { 293 elements = roundEnv.getElementsAnnotatedWithAny(Set.of(objectClass)); 294 throw new RuntimeException("Illegal argument exception not thrown"); 295 } catch (IllegalArgumentException iae) {} 296 297 TypeElement objectElement = processingEnv.getElementUtils().getTypeElement("java.lang.Object"); 298 try { 299 elements = roundEnv.getElementsAnnotatedWith(objectElement); 300 throw new RuntimeException("Illegal argument exception not thrown"); 301 } catch (IllegalArgumentException iae) {} 302 303 try { 304 elements = roundEnv.getElementsAnnotatedWithAny(objectElement); 305 throw new RuntimeException("Illegal argument exception not thrown"); 306 } catch (IllegalArgumentException iae) {} 307 } 308 } |