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"); | 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 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/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 47 * @compile Foo.java 48 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 49 */ 50 51 import annot.AnnotatedElementInfo; 52 import java.lang.annotation.Annotation; 53 import java.util.Collections; 54 import java.util.Set; 55 import java.util.HashSet; 56 import java.util.Arrays; 57 import java.util.Objects; 58 import javax.annotation.processing.*; 59 import javax.lang.model.element.*; 60 import static javax.lang.model.util.ElementFilter.*; 61 62 /** 63 * This processor verifies that the information returned by 64 * getElementsAnnotatedWith and getElementsAnnotatedWithAny is 65 * consistent with the expected results stored in an 66 * AnnotatedElementInfo annotation. 67 */ 68 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 69 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 70 71 public boolean process(Set<? extends TypeElement> annotations, 134 return re.processingOver(); 135 } 136 137 } 138 139 /** 140 * The method checks the following conditions: 141 * 142 * 1) The sets of elements found are equal for the TypeElement and 143 * Class<? extends Annotation> methods on logically equivalent 144 * arguments. 145 * 146 * 2) getElementsAnnotatedWithAny(X) is equal to 147 * getElementsAnnotatedWith(X') where X is a set/var-args array 148 * with one element and X' is the element. 149 * 150 * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to 151 * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y). 152 */ 153 void checkSetOfAnnotatedElements(RoundEnvironment re) { 154 TypeElement annotatedElemInfoElem = elements.getTypeElement("annot.AnnotatedElementInfo"); 155 156 // For the "Any" methods, search for both the expected 157 // annotation and AnnotatedElementInfo and verify the return 158 // set is the union of searching for AnnotatedElementInfo and 159 // the other annotation 160 Set<? extends Element> resultsMeta = Collections.emptySet(); 161 Set<? extends Element> resultsMetaAny = Collections.emptySet(); 162 Set<Element> resultsMetaMulti = new HashSet<>(); 163 Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet(); 164 Set<? extends Element> resultsBase = Collections.emptySet(); 165 Set<? extends Element> resultsBaseAny = Collections.emptySet(); 166 Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet(); 167 168 if (!re.processingOver()) { 169 testNonAnnotations(re); 170 171 // Verify AnnotatedElementInfo is present on the first 172 // specified type. 173 174 Element firstElement = re.getRootElements().iterator().next(); 175 176 AnnotatedElementInfo annotatedElemInfo = 177 firstElement.getAnnotation(AnnotatedElementInfo.class); 178 179 boolean failed = false; 180 181 Objects.requireNonNull(annotatedElemInfo, 182 "Missing AnnotatedElementInfo annotation on " + firstElement); 183 184 // Verify that the annotation information is as expected. 185 Set<String> expectedNames = 186 new HashSet<>(Arrays.asList(annotatedElemInfo.names())); 187 188 String annotationName = annotatedElemInfo.annotationName(); 189 TypeElement annotationTypeElem = elements.getTypeElement(annotationName); 190 191 resultsMeta = re.getElementsAnnotatedWith(annotationTypeElem); 192 resultsMetaAny = re.getElementsAnnotatedWithAny(annotationTypeElem); 193 resultsMetaMulti.addAll(resultsMeta); 194 resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem)); 195 resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem); 196 197 if (!resultsMeta.isEmpty()) 198 System.err.println("Results: " + resultsMeta); 199 200 if (!resultsMeta.equals(resultsMetaAny)) { 201 failed = true; 202 System.err.printf("Inconsistent Meta with vs withAny results"); |