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 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, 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 { 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 { |