1 /* 2 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 8190886 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 annot/AnnotatedElementInfo.java annot/MarkerAnnot.java 34 * @compile TestElementsAnnotatedWith.java 35 * @compile InheritedAnnotation.java 36 * @compile TpAnno.java 37 * @compile Anno.java 38 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 39 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 40 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 41 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 42 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 43 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 44 * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java 45 * @compile -processor TestElementsAnnotatedWith -proc:only pkg/package-info.java 46 * @compile -processor TestElementsAnnotatedWith -proc:only mod/quux/package-info.java 47 * @compile -processor TestElementsAnnotatedWith -proc:only mod/quux/Quux.java 48 * @compile mod/quux/Quux.java mod/quux/package-info.java 49 * @compile -processor TestElementsAnnotatedWith -proc:only -AsingleModuleMode=true mod/module-info.java 50 * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 51 * @compile Foo.java 52 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 53 */ 54 55 // * @compile mod/quux/package-info.java mod/quux/Quux.java 56 // * @compile -processor TestElementsAnnotatedWith -proc:only mod/module-info.java 57 58 59 import annot.AnnotatedElementInfo; 60 import java.lang.annotation.Annotation; 61 import java.util.Collections; 62 import java.util.Set; 63 import java.util.HashSet; 64 import java.util.Arrays; 65 import java.util.Objects; 66 import javax.annotation.processing.*; 67 import javax.lang.model.element.*; 68 import static javax.lang.model.util.ElementFilter.*; 69 70 /** 71 * This processor verifies that the information returned by 72 * getElementsAnnotatedWith and getElementsAnnotatedWithAny is 73 * consistent with the expected results stored in an 74 * AnnotatedElementInfo annotation. 75 */ 76 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 77 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 78 79 public boolean process(Set<? extends TypeElement> annotations, 80 RoundEnvironment roundEnv) { 81 // First check sets of annotated elements using the round 82 // environment from the annotation processing tool framework. 83 checkSetOfAnnotatedElements(roundEnv); 84 85 // Next check sets of annotated elements using a round 86 // environment which uses the default implementations of the 87 // getElementsAnnotatedWithAny methods from the interface. 88 checkSetOfAnnotatedElements(new TestingRoundEnvironment(roundEnv)); 89 return true; 90 } 91 92 /** 93 * To allow testing of the executable code of the default methods 94 * for the two overloaded getElementsAnnotatedWithAny methods 95 * defined in the RoundEnvironment interface, this class delegates 96 * the non-default methods of RoundEnvironment to a given 97 * RoundEnvironment object and then explicitly calls the default 98 * methods of the interface instead of relying on the object's 99 * implementation of those methods. 100 */ 101 private class TestingRoundEnvironment implements RoundEnvironment { 102 private RoundEnvironment re; 103 104 public TestingRoundEnvironment(RoundEnvironment re) { 105 this.re = re; 106 } 107 108 @Override 109 public boolean errorRaised() { 110 return re.errorRaised(); 111 } 112 113 @Override 114 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { 115 return re.getElementsAnnotatedWith(a); 116 } 117 118 @Override 119 public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> a) { 120 // Default method defined in the interface 121 return RoundEnvironment.super.getElementsAnnotatedWithAny(a); 122 } 123 124 @Override 125 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 126 return re.getElementsAnnotatedWith(a); 127 } 128 129 @Override 130 public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... a) { 131 // Default method defined in the interface 132 return RoundEnvironment.super.getElementsAnnotatedWithAny(a); 133 } 134 135 @Override 136 public Set<? extends Element> getRootElements() { 137 return re.getRootElements(); 138 } 139 140 @Override 141 public boolean processingOver() { 142 return re.processingOver(); 143 } 144 145 } 146 147 /** 148 * The method checks the following conditions: 149 * 150 * 1) The sets of elements found are equal for the TypeElement and 151 * Class<? extends Annotation> methods on logically equivalent 152 * arguments. 153 * 154 * 2) getElementsAnnotatedWithAny(X) is equal to 155 * getElementsAnnotatedWith(X') where X is a set/var-args array 156 * with one element and X' is the element. 157 * 158 * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to 159 * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y). 160 */ 161 void checkSetOfAnnotatedElements(RoundEnvironment re) { 162 // For the "Any" methods, search for both the expected 163 // annotation and AnnotatedElementInfo and verify the return 164 // set is the union of searching for AnnotatedElementInfo and 165 // the other annotation 166 Set<? extends Element> resultsMeta = Collections.emptySet(); 167 Set<? extends Element> resultsMetaAny = Collections.emptySet(); 168 Set<Element> resultsMetaMulti = new HashSet<>(); 169 Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet(); 170 Set<? extends Element> resultsBase = Collections.emptySet(); 171 Set<? extends Element> resultsBaseAny = Collections.emptySet(); 172 Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet(); 173 174 175 boolean singleModuleMode = processingEnv.getOptions().get("singleModuleMode") != null; 176 177 TypeElement annotatedElemInfoElem = null; 178 179 if (!re.processingOver()) { 180 testNonAnnotations(re); 181 182 // Verify AnnotatedElementInfo is present on the first 183 // specified type. 184 185 Element firstElement = re.getRootElements().iterator().next(); 186 187 AnnotatedElementInfo annotatedElemInfo = 188 firstElement.getAnnotation(AnnotatedElementInfo.class); 189 190 ModuleElement moduleContext; 191 // DEBUGGING 192 if (firstElement.getKind() == ElementKind.MODULE) { 193 elements.printElements(new java.io.OutputStreamWriter(System.out) , firstElement); 194 System.out.println("AnnotatedElementInfo module: " + AnnotatedElementInfo.class.getModule()); 195 196 for (AnnotationMirror am : firstElement.getAnnotationMirrors()){ 197 System.out.print("\t" + am.toString()); 198 System.out.println("\t in module " + elements.getModuleOf(am.getAnnotationType().asElement()).toString()); 199 } 200 moduleContext = (ModuleElement)firstElement; 201 } else { 202 moduleContext = elements.getModuleElement(""); // unnamed module 203 } 204 205 206 annotatedElemInfoElem = 207 elements.getTypeElement(moduleContext, "annot.AnnotatedElementInfo"); 208 209 boolean failed = false; 210 211 Objects.requireNonNull(annotatedElemInfo, 212 "Missing AnnotatedElementInfo annotation on " + firstElement); 213 214 // Verify that the annotation information is as expected. 215 Set<String> expectedNames = 216 new HashSet<>(Arrays.asList(annotatedElemInfo.names())); 217 218 String annotationName = annotatedElemInfo.annotationName(); 219 TypeElement annotationTypeElem = elements.getTypeElement(moduleContext, 220 annotationName); 221 222 resultsMeta = re.getElementsAnnotatedWith(annotationTypeElem); 223 resultsMetaAny = re.getElementsAnnotatedWithAny(annotationTypeElem); 224 resultsMetaMulti.addAll(resultsMeta); 225 resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem)); 226 resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem); 227 228 if (!resultsMeta.isEmpty()) 229 System.err.println("Results: " + resultsMeta); 230 231 if (!resultsMeta.equals(resultsMetaAny)) { 232 failed = true; 233 System.err.printf("Inconsistent Meta with vs withAny results"); 234 } 235 236 if (resultsMeta.size() != annotatedElemInfo.expectedSize()) { 237 failed = true; 238 System.err.printf("Bad number of elements; expected %d, got %d%n", 239 annotatedElemInfo.expectedSize(), resultsMeta.size()); 240 } else { 241 for(Element element : resultsMeta) { 242 String simpleName = element.getSimpleName().toString(); 243 if (!expectedNames.contains(simpleName) ) { 244 failed = true; 245 System.err.println("Name ``" + simpleName + "'' not expected."); 246 } 247 } 248 } 249 250 resultsBase = computeResultsBase(re, annotationName); 251 resultsBaseAny = computeResultsBaseAny(re, annotationName); 252 try { 253 Set<Class<? extends Annotation>> tmp = new HashSet<>(); 254 tmp.add(AnnotatedElementInfo.class); 255 tmp.add(Class.forName(annotationName).asSubclass(Annotation.class)); 256 resultsBaseAnyMulti = re.getElementsAnnotatedWithAny(tmp); 257 } catch (ClassNotFoundException e) { 258 throw new RuntimeException(e); 259 } 260 261 if (!resultsBase.equals(resultsBaseAny)) { 262 failed = true; 263 System.err.printf("Inconsistent Base with vs withAny results"); 264 } 265 266 if (!singleModuleMode && !resultsMeta.equals(resultsBase)) { 267 failed = true; 268 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 269 "\nbase: " + resultsBase); 270 } 271 272 if (!resultsMetaAnyMulti.equals(resultsMetaMulti)) { 273 failed = true; 274 System.err.println("MetaMultAny and MetaMulti sets unequal;\n meta: " + resultsMeta + 275 "\nbase: " + resultsBase); 276 } 277 278 if (!singleModuleMode && !resultsBaseAnyMulti.equals(resultsMetaAnyMulti)) { 279 failed = true; 280 System.err.println("BaseMulti and MetaMulti sets unequal;\n meta: " + resultsMeta + 281 "\nbase: " + resultsBase); 282 } 283 284 if (failed) { 285 System.err.println("AnnotatedElementInfo: " + annotatedElemInfo); 286 throw new RuntimeException(); 287 } 288 } else { 289 // If processing is over without an error, the specified 290 // elements should be empty so an empty set should be 291 // returned. 292 if (!singleModuleMode) { 293 annotatedElemInfoElem = elements.getTypeElement("AnnotatedElementInfo"); 294 throwOnNonEmpty(re.getElementsAnnotatedWith(annotatedElemInfoElem), "resultsMeta"); 295 throwOnNonEmpty(re.getElementsAnnotatedWithAny(annotatedElemInfoElem), "resultsMetaAny"); 296 throwOnNonEmpty(re.getElementsAnnotatedWith(AnnotatedElementInfo.class), "resultsBase"); 297 throwOnNonEmpty(re.getElementsAnnotatedWithAny(Set.of(AnnotatedElementInfo.class)), "resultsBaseAny"); 298 } 299 } 300 } 301 302 private void throwOnNonEmpty(Set<? extends Element> results, String message) { 303 if (!results.isEmpty()) { 304 throw new RuntimeException("Nonempty " + message + "\t" + results); 305 } 306 } 307 308 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnv, String name) { 309 try { 310 return roundEnv. 311 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 312 } catch (ClassNotFoundException cnfe) { 313 throw new RuntimeException(cnfe); 314 } 315 } 316 317 private Set<? extends Element> computeResultsBaseAny(RoundEnvironment roundEnv, String name) { 318 try { 319 return roundEnv. 320 getElementsAnnotatedWithAny(Set.of(Class.forName(name).asSubclass(Annotation.class))); 321 } catch (ClassNotFoundException cnfe) { 322 throw new RuntimeException(cnfe); 323 } 324 } 325 326 /** 327 * Verify non-annotation types result in 328 * IllegalArgumentExceptions. 329 */ 330 private void testNonAnnotations(RoundEnvironment roundEnv) { 331 Class objectClass = (Class)Object.class; 332 Set<? extends Element> elements; 333 try { 334 elements = roundEnv.getElementsAnnotatedWith(objectClass); 335 throw new RuntimeException("Illegal argument exception not thrown"); 336 } catch (IllegalArgumentException iae) {} 337 338 try { 339 elements = roundEnv.getElementsAnnotatedWithAny(Set.of(objectClass)); 340 throw new RuntimeException("Illegal argument exception not thrown"); 341 } catch (IllegalArgumentException iae) {} 342 343 TypeElement objectElement = processingEnv.getElementUtils().getTypeElement("java.lang.Object"); 344 try { 345 elements = roundEnv.getElementsAnnotatedWith(objectElement); 346 throw new RuntimeException("Illegal argument exception not thrown"); 347 } catch (IllegalArgumentException iae) {} 348 349 try { 350 elements = roundEnv.getElementsAnnotatedWithAny(objectElement); 351 throw new RuntimeException("Illegal argument exception not thrown"); 352 } catch (IllegalArgumentException iae) {} 353 } 354 }