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