1 /* 2 * Copyright (c) 2006, 2015, 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 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, 131 return re.processingOver(); 132 } 133 134 } 135 136 /** 137 * The method checks the following conditions: 138 * 139 * 1) The sets of elements found are equal for the TypeElement and 140 * Class<? extends Annotation> methods on logically equivalent 141 * arguments. 142 * 143 * 2) getElementsAnnotatedWithAny(X) is equal to 144 * getElementsAnnotatedWith(X') where X is a set/var-args array 145 * with one element and X' is the element. 146 * 147 * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to 148 * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y). 149 */ 150 void checkSetOfAnnotatedElements(RoundEnvironment re) { 151 TypeElement annotatedElemInfoElem = elements.getTypeElement("AnnotatedElementInfo"); 152 153 // For the "Any" methods, search for both the expected 154 // annotation and AnnotatedElementInfo and verify the return 155 // set is the union of searching for AnnotatedElementInfo and 156 // the other annotation 157 Set<? extends Element> resultsMeta = Collections.emptySet(); 158 Set<? extends Element> resultsMetaAny = Collections.emptySet(); 159 Set<Element> resultsMetaMulti = new HashSet<>(); 160 Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet(); 161 Set<? extends Element> resultsBase = Collections.emptySet(); 162 Set<? extends Element> resultsBaseAny = Collections.emptySet(); 163 Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet(); 164 165 if (!re.processingOver()) { 166 testNonAnnotations(re); 167 168 // Verify AnnotatedElementInfo is present on the first 169 // specified type. 170 171 TypeElement firstType = typesIn(re.getRootElements()).iterator().next(); 172 173 AnnotatedElementInfo annotatedElemInfo = 174 firstType.getAnnotation(AnnotatedElementInfo.class); 175 176 boolean failed = false; 177 178 Objects.requireNonNull(annotatedElemInfo, 179 "Missing AnnotatedElementInfo annotation on " + firstType); 180 181 // Verify that the annotation information is as expected. 182 Set<String> expectedNames = 183 new HashSet<>(Arrays.asList(annotatedElemInfo.names())); 184 185 String annotationName = annotatedElemInfo.annotationName(); 186 TypeElement annotationTypeElem = elements.getTypeElement(annotationName); 187 188 resultsMeta = re.getElementsAnnotatedWith(annotationTypeElem); 189 resultsMetaAny = re.getElementsAnnotatedWithAny(annotationTypeElem); 190 resultsMetaMulti.addAll(resultsMeta); 191 resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem)); 192 resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem); 193 194 if (!resultsMeta.isEmpty()) 195 System.err.println("Results: " + resultsMeta); 196 197 if (!resultsMeta.equals(resultsMetaAny)) { 198 failed = true; 199 System.err.printf("Inconsistent Meta with vs withAny results"); 200 } 201 202 if (resultsMeta.size() != annotatedElemInfo.expectedSize()) { 203 failed = true; 204 System.err.printf("Bad number of elements; expected %d, got %d%n", 205 annotatedElemInfo.expectedSize(), resultsMeta.size()); 206 } else { 212 } 213 } 214 } 215 216 resultsBase = computeResultsBase(re, annotationName); 217 resultsBaseAny = computeResultsBaseAny(re, annotationName); 218 try { 219 Set<Class<? extends Annotation>> tmp = new HashSet<>(); 220 tmp.add(AnnotatedElementInfo.class); 221 tmp.add(Class.forName(annotationName).asSubclass(Annotation.class)); 222 resultsBaseAnyMulti = re.getElementsAnnotatedWithAny(tmp); 223 } catch (ClassNotFoundException e) { 224 throw new RuntimeException(e); 225 } 226 227 if (!resultsBase.equals(resultsBaseAny)) { 228 failed = true; 229 System.err.printf("Inconsistent Base with vs withAny results"); 230 } 231 232 if (!resultsMeta.equals(resultsBase)) { 233 failed = true; 234 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 235 "\nbase: " + resultsBase); 236 } 237 238 if (!resultsMetaAnyMulti.equals(resultsMetaMulti)) { 239 failed = true; 240 System.err.println("MetaMultAny and MetaMulti sets unequal;\n meta: " + resultsMeta + 241 "\nbase: " + resultsBase); 242 } 243 244 if (!resultsBaseAnyMulti.equals(resultsMetaAnyMulti)) { 245 failed = true; 246 System.err.println("BaseMulti and MetaMulti sets unequal;\n meta: " + resultsMeta + 247 "\nbase: " + resultsBase); 248 } 249 250 if (failed) { 251 System.err.println("AnnotatedElementInfo: " + annotatedElemInfo); 252 throw new RuntimeException(); 253 } 254 } else { 255 // If processing is over without an error, the specified 256 // elements should be empty so an empty set should be 257 // returned. 258 throwOnNonEmpty(re.getElementsAnnotatedWith(annotatedElemInfoElem), "resultsMeta"); 259 throwOnNonEmpty(re.getElementsAnnotatedWithAny(annotatedElemInfoElem), "resultsMetaAny"); 260 throwOnNonEmpty(re.getElementsAnnotatedWith(AnnotatedElementInfo.class), "resultsBase"); 261 throwOnNonEmpty(re.getElementsAnnotatedWithAny(Set.of(AnnotatedElementInfo.class)), "resultsBaseAny"); 262 } 263 } 264 265 private void throwOnNonEmpty(Set<? extends Element> results, String message) { 266 if (!results.isEmpty()) { 267 throw new RuntimeException("Nonempty " + message + "\t" + results); 268 } 269 } 270 271 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnv, String name) { 272 try { 273 return roundEnv. 274 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 275 } catch (ClassNotFoundException cnfe) { 276 throw new RuntimeException(cnfe); 277 } 278 } 279 280 private Set<? extends Element> computeResultsBaseAny(RoundEnvironment roundEnv, String name) { 281 try { | 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, 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 { 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 { |