|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8202473 |
| 27 | + * @summary Annotations on type variables with multiple bounds should be placed on their respective bound |
| 28 | + * @compile TypeVariableBoundParameterIndex.java |
| 29 | + * @run main TypeVariableBoundParameterIndex |
| 30 | + */ |
| 31 | + |
| 32 | +import java.lang.annotation.Annotation; |
| 33 | +import java.lang.annotation.ElementType; |
| 34 | +import java.lang.annotation.Retention; |
| 35 | +import java.lang.annotation.RetentionPolicy; |
| 36 | +import java.lang.annotation.Target; |
| 37 | +import java.lang.reflect.AnnotatedParameterizedType; |
| 38 | +import java.lang.reflect.AnnotatedType; |
| 39 | +import java.lang.reflect.TypeVariable; |
| 40 | +import java.util.concurrent.Callable; |
| 41 | + |
| 42 | +/* |
| 43 | + * A class might have multiple bounds as parameterized types with type annotations on these bounds. |
| 44 | + * This test assures that these bound annotations are resolved correctly. |
| 45 | + */ |
| 46 | +public class TypeVariableBoundParameterIndex { |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + TypeVariable<?>[] variables = Sample.class.getTypeParameters(); |
| 50 | + |
| 51 | + for (int i = 0; i < 2; i++) { |
| 52 | + TypeVariable<?> variable = variables[i]; |
| 53 | + AnnotatedType[] bounds = variable.getAnnotatedBounds(); |
| 54 | + AnnotatedType bound = bounds[0]; |
| 55 | + AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) bound; |
| 56 | + AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); |
| 57 | + Annotation[] annotations = actualTypeArguments[0].getAnnotations(); |
| 58 | + if (annotations.length != 1 || annotations[0].annotationType() != TypeAnnotation.class) { |
| 59 | + throw new AssertionError(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + TypeVariable<?> variable = variables[2]; |
| 64 | + AnnotatedType[] bounds = variable.getAnnotatedBounds(); |
| 65 | + AnnotatedType bound = bounds[0]; |
| 66 | + AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) bound; |
| 67 | + AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments(); |
| 68 | + Annotation[] annotations = actualTypeArguments[0].getAnnotations(); |
| 69 | + if (annotations.length != 0) { |
| 70 | + throw new AssertionError(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Retention(RetentionPolicy.RUNTIME) |
| 75 | + @Target(ElementType.TYPE_USE) |
| 76 | + @interface TypeAnnotation { } |
| 77 | + |
| 78 | + static class Sample<T extends Callable<@TypeAnnotation ?>, S extends Callable<@TypeAnnotation ?>, U extends Callable<?>> { } |
| 79 | +} |
0 commit comments