|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2024, 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 8042981 |
| 27 | + * @summary Test if annotations are stripped from the results of Types' methods |
| 28 | + * @library /tools/javac/lib |
| 29 | + * @modules java.compiler |
| 30 | + * jdk.compiler |
| 31 | + * @build JavacTestingAbstractProcessor TestAnnotationStripping |
| 32 | + * @compile -processor TestAnnotationStripping -proc:only TestAnnotationStripping.java |
| 33 | + */ |
| 34 | + |
| 35 | +import java.lang.annotation.*; |
| 36 | +import java.util.*; |
| 37 | +import static java.util.Objects.*; |
| 38 | +import javax.annotation.processing.*; |
| 39 | +import javax.lang.model.SourceVersion; |
| 40 | +import static javax.lang.model.SourceVersion.*; |
| 41 | +import javax.lang.model.*; |
| 42 | +import javax.lang.model.element.*; |
| 43 | +import javax.lang.model.type.*; |
| 44 | +import javax.lang.model.util.*; |
| 45 | +import static javax.lang.model.util.ElementFilter.*; |
| 46 | +import static javax.tools.Diagnostic.Kind.*; |
| 47 | +import static javax.tools.StandardLocation.*; |
| 48 | + |
| 49 | +/** |
| 50 | + * Test if annotations are stripped from the results of Types' methods |
| 51 | + */ |
| 52 | +public class TestAnnotationStripping extends JavacTestingAbstractProcessor { |
| 53 | + private Types vacuousTypes = new VacuousTypes(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Check expected behavior on classes and packages. |
| 57 | + */ |
| 58 | + public boolean process(Set<? extends TypeElement> annotations, |
| 59 | + RoundEnvironment roundEnv) { |
| 60 | + if (!roundEnv.processingOver()) { |
| 61 | + TypeElement juSetElt = eltUtils.getTypeElement("java.util.Set"); |
| 62 | + TypeElement testElt = elements.getTypeElement("TestAnnotationStripping"); |
| 63 | + TypeElement boxElt = elements.getTypeElement("TestAnnotationStripping.Box"); |
| 64 | + |
| 65 | + TypeMirror expectedAnnotation = eltUtils.getTypeElement("TestTypeAnnotation").asType(); |
| 66 | + |
| 67 | + for (ExecutableElement m : |
| 68 | + methodsIn(eltUtils.getTypeElement("HostClass").getEnclosedElements())) { |
| 69 | + /* |
| 70 | + * The kinds of types include: |
| 71 | + * |
| 72 | + * arrays |
| 73 | + * declared types (classes, interfaces, etc.) |
| 74 | + * error types |
| 75 | + * executable types |
| 76 | + * intersection types |
| 77 | + * no-type |
| 78 | + * null type |
| 79 | + * primitive types |
| 80 | + * type variable |
| 81 | + * union type |
| 82 | + * wildcards |
| 83 | + * |
| 84 | + * A subset of these can appear at the return type of |
| 85 | + * a method. The general methodology is to verify that |
| 86 | + * types that can appear as return types when |
| 87 | + * annotated with type annotations appear as specified |
| 88 | + * as the result of type operations or when new types |
| 89 | + * are constructed. |
| 90 | + */ |
| 91 | + |
| 92 | + TypeMirror returnType = m.getReturnType(); |
| 93 | + |
| 94 | + System.err.println("Checking " + returnType); |
| 95 | + |
| 96 | + testVacuous(returnType); |
| 97 | + checkDeepEmptyAnnotations(typeUtils.stripAnnotations(returnType)); |
| 98 | + |
| 99 | + checkExpectedTypeAnnotations(returnType, expectedAnnotation); |
| 100 | + |
| 101 | + // Note: the result of Types.asElement is *not* |
| 102 | + // checked for its annotations since the return value |
| 103 | + // is an Element and not a TypeMirror. |
| 104 | + |
| 105 | + System.err.print("\tcapture()"); |
| 106 | + checkDeepEmptyAnnotations(typeUtils.capture(returnType)); |
| 107 | + |
| 108 | + System.err.print("\terasure()"); |
| 109 | + checkDeepEmptyAnnotations(typeUtils.erasure(returnType)); |
| 110 | + |
| 111 | + System.err.print("\tgetArrayType()"); |
| 112 | + ArrayType arrayType = typeUtils.getArrayType(returnType); |
| 113 | + checkEmptyAnnotations(arrayType); |
| 114 | + /* |
| 115 | + * "Annotations on the component type are preserved." |
| 116 | + */ |
| 117 | + checkEqualTypeAndAnnotations(returnType, arrayType.getComponentType()); |
| 118 | + |
| 119 | + if (!returnType.getKind().isPrimitive()) { |
| 120 | + /* |
| 121 | + * For getWildcardType() |
| 122 | + * "Annotations on the bounds are preserved." |
| 123 | + */ |
| 124 | + WildcardType wcType; |
| 125 | + checkEmptyAnnotations(wcType = typeUtils.getWildcardType(returnType, null)); |
| 126 | + checkEqualTypeAndAnnotations(returnType, wcType.getExtendsBound()); |
| 127 | + |
| 128 | + checkEmptyAnnotations(wcType = typeUtils.getWildcardType(null, returnType)); |
| 129 | + checkEqualTypeAndAnnotations(returnType, wcType.getSuperBound()); |
| 130 | + |
| 131 | + /* |
| 132 | + * For getDeclaredType() |
| 133 | + * "Annotations on the type arguments are preserved." |
| 134 | + */ |
| 135 | + DeclaredType declaredType = typeUtils.getDeclaredType(juSetElt, returnType); |
| 136 | + checkEqualTypeAndAnnotations(returnType, declaredType.getTypeArguments().get(0)); |
| 137 | + |
| 138 | + // Check both overloads |
| 139 | + declaredType = typeUtils.getDeclaredType(typeUtils.getDeclaredType(testElt), // outer type |
| 140 | + boxElt, |
| 141 | + returnType); |
| 142 | + checkEqualTypeAndAnnotations(returnType, declaredType.getTypeArguments().get(0)); |
| 143 | + } |
| 144 | + |
| 145 | + System.out.println(returnType.getAnnotation(TestTypeAnnotation.class)); |
| 146 | + System.out.println(returnType.getAnnotationsByType(TestTypeAnnotation.class).length); |
| 147 | + TestTypeAnnotation ta = requireNonNull(returnType.getAnnotation(TestTypeAnnotation.class), |
| 148 | + returnType.toString()); |
| 149 | + |
| 150 | + System.err.println(); |
| 151 | + System.err.println(); |
| 152 | + } |
| 153 | + |
| 154 | + if (failures > 0) |
| 155 | + throw new RuntimeException(failures + " failures occured."); |
| 156 | + } |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + void testVacuous(TypeMirror tm ) { |
| 161 | + try { |
| 162 | + var result = vacuousTypes.stripAnnotations(tm); |
| 163 | + messager.printError("Unexpected non-exceptional result returned " + result); |
| 164 | + } catch(UnsupportedOperationException uoe) { |
| 165 | + ; // Expected |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private int failures = 0; |
| 170 | + |
| 171 | + void checkExpectedTypeAnnotations(AnnotatedConstruct ac, TypeMirror expectedAnnotation) { |
| 172 | + List<? extends AnnotationMirror> annotations = ac.getAnnotationMirrors(); |
| 173 | + if (annotations.size() != 1) { |
| 174 | + failures++; |
| 175 | + System.err.println("\t\t\tUnexpected annotations size: " + annotations.size()); |
| 176 | + } else if (!typeUtils.isSameType(annotations.get(0).getAnnotationType(), expectedAnnotation)) { |
| 177 | + failures++; |
| 178 | + System.err.println("\t\t\tUnexpected annotations type: " + annotations); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + void checkEmptyAnnotations(AnnotatedConstruct ac) { |
| 183 | + System.err.println("\t" + ac); |
| 184 | + if (ac == null) |
| 185 | + return; |
| 186 | + else { |
| 187 | + List<? extends AnnotationMirror> annotations = ac.getAnnotationMirrors(); |
| 188 | + int count = annotations.size(); |
| 189 | + if (count != 0) { |
| 190 | + failures++; |
| 191 | + System.err.println(ac.getClass()); |
| 192 | + System.err.println("\t\t\tUnexpected nonzero annotations size: " + annotations); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + void checkDeepEmptyAnnotations(TypeMirror ac) { |
| 198 | + System.err.println("\t" + ac); |
| 199 | + if (ac == null) { |
| 200 | + return; |
| 201 | + } |
| 202 | + new SimpleTypeVisitor14<Void, Void>() { |
| 203 | + @Override |
| 204 | + protected Void defaultAction(TypeMirror t, Void o) { |
| 205 | + checkEmptyAnnotations(t); |
| 206 | + return null; |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public Void visitArray(ArrayType t, Void o) { |
| 211 | + scan(t.getComponentType()); |
| 212 | + return super.visitArray(t, o); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public Void visitDeclared(DeclaredType t, Void o) { |
| 217 | + scan(t.getEnclosingType()); |
| 218 | + t.getTypeArguments().stream().forEach(this::scan); |
| 219 | + return super.visitDeclared(t, o); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public Void visitTypeVariable(TypeVariable t, Void o) { |
| 224 | + // the bounds correspond to the type variable declaration, not its use |
| 225 | + // scan(t.getUpperBound()); |
| 226 | + // scan(t.getLowerBound()); |
| 227 | + return super.visitTypeVariable(t, o); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public Void visitWildcard(WildcardType t, Void o) { |
| 232 | + scan(t.getExtendsBound()); |
| 233 | + scan(t.getSuperBound()); |
| 234 | + return super.visitWildcard(t, o); |
| 235 | + } |
| 236 | + |
| 237 | + private void scan(TypeMirror t) { |
| 238 | + if (t != null) { |
| 239 | + visit(t); |
| 240 | + } |
| 241 | + } |
| 242 | + }.visit(ac); |
| 243 | + } |
| 244 | + |
| 245 | + void checkEqualTypeAndAnnotations(TypeMirror tm1, TypeMirror tm2) { |
| 246 | + if (!typeUtils.isSameType(tm1, tm2)) { |
| 247 | + failures++; |
| 248 | + System.err.printf("Unequal types %s and %s.%n", tm1, tm2); |
| 249 | + } |
| 250 | + |
| 251 | + if (!Objects.equals(tm1.getAnnotationMirrors(), tm1.getAnnotationMirrors())) { |
| 252 | + failures++; |
| 253 | + System.err.printf("Unequal annotations on and %s.%n", tm1, tm2); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + // Nested class to test getDeclaredType overload. |
| 258 | + class Box<T> { |
| 259 | + private T contents; |
| 260 | + |
| 261 | + public Box(T t){ |
| 262 | + contents = t; |
| 263 | + } |
| 264 | + |
| 265 | + T value() { return contents;}; |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +/* |
| 270 | + * Class to host annotations for testing |
| 271 | + */ |
| 272 | +class HostClass { |
| 273 | + // Declared type Integer |
| 274 | + public static @TestTypeAnnotation("foo") Integer foo() {return null;} |
| 275 | + |
| 276 | + // Primitive type int |
| 277 | + public static @TestTypeAnnotation("foo2") int foo2() {return 0;} |
| 278 | + |
| 279 | + public static @TestTypeAnnotation("foo3") String foo3() {return null;} |
| 280 | + |
| 281 | + // Declared raw type Set |
| 282 | + public static java.util.@TestTypeAnnotation("foo4")Set foo4() {return null;} |
| 283 | + |
| 284 | + // Array type |
| 285 | + public static String @TestTypeAnnotation("foo5")[] foo5() {return null;} |
| 286 | + |
| 287 | + // Declared type Set with instantiated type parameter |
| 288 | + public static java.util. @TestTypeAnnotation("foo6") Set < @TestTypeAnnotation("foo7") String> foo6() {return null;} |
| 289 | + |
| 290 | + // Type variable |
| 291 | + public static <@TestTypeAnnotation("foo8") T extends @TestTypeAnnotation("foo9") String> @TestTypeAnnotation("foo10") T foo7() {return null;} |
| 292 | + |
| 293 | + // Declared type including wildcard |
| 294 | + public static java.util. @TestTypeAnnotation("foo11") Set < @TestTypeAnnotation("foo12") ? extends @TestTypeAnnotation("foo13") Number> foo8() {return null;} |
| 295 | + |
| 296 | + // Type variable with intersection type |
| 297 | + public static <@TestTypeAnnotation("foo14") S extends Number & Runnable> @TestTypeAnnotation("foo15") S foo9() {return null;} |
| 298 | + |
| 299 | +} |
| 300 | + |
| 301 | +@Retention(RetentionPolicy.RUNTIME) |
| 302 | +@Target(ElementType.TYPE_USE) |
| 303 | +@interface TestTypeAnnotation { |
| 304 | + String value() default ""; |
| 305 | +} |
0 commit comments