1 /* 2 * Copyright (c) 2013, 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 8004698 27 * @summary Unit test for annotations on TypeVariables 28 */ 29 30 import java.util.*; 31 import java.lang.annotation.*; 32 import java.lang.reflect.*; 33 import java.io.Serializable; 34 35 public class TypeParamAnnotation { 36 public static void main(String[] args) throws Exception { 37 testOnClass(); 38 testOnMethod(); 39 testGetAnno(); 40 testGetAnnos(); 41 } 42 43 private static void check(boolean b) { 44 if (!b) 45 throw new RuntimeException(); 46 } 47 48 private static void testOnClass() { 49 TypeVariable<?>[] ts = TypeParam.class.getTypeParameters(); 50 check(ts.length == 3); 51 52 Annotation[] as; 53 54 as = ts[0].getAnnotations(); 55 check(as.length == 2); 56 check(((ParamAnno)as[0]).value().equals("t")); 57 check(((ParamAnno2)as[1]).value() == 1); 58 59 as = ts[1].getAnnotations(); 60 check(as.length == 0); 61 62 as = ts[2].getAnnotations(); 63 check(as.length == 2); 64 check(((ParamAnno)as[0]).value().equals("v")); 65 check(((ParamAnno2)as[1]).value() == 2); 66 } 67 private static void testOnMethod() throws Exception { 68 TypeVariable<?>[] ts = TypeParam.class.getDeclaredMethod("foo").getTypeParameters(); 69 check(ts.length == 3); 70 71 Annotation[] as; 72 73 as = ts[0].getAnnotations(); 74 check(as.length == 2); 75 check(((ParamAnno)as[0]).value().equals("x")); 76 check(((ParamAnno2)as[1]).value() == 3); 77 78 as = ts[1].getAnnotations(); 79 check(as.length == 0); 80 81 as = ts[2].getAnnotations(); 82 check(as.length == 2); 83 check(((ParamAnno)as[0]).value().equals("z")); 84 check(((ParamAnno2)as[1]).value() == 4); 85 } 86 87 private static void testGetAnno() { 88 TypeVariable<?>[] ts = TypeParam.class.getTypeParameters(); 89 ParamAnno a; 90 a = ts[0].getAnnotation(ParamAnno.class); 91 check(a.value().equals("t")); 92 } 93 private static void testGetAnnos() throws Exception { 94 TypeVariable<?>[] ts = TypeParam.class.getDeclaredMethod("foo").getTypeParameters(); 95 ParamAnno2[] as; 96 as = ts[0].getAnnotations(ParamAnno2.class); 97 check(as.length == 1); 98 check(as[0].value() == 3); 99 } 100 } 101 102 class TypeParam <@ParamAnno("t") @ParamAnno2(1) T, 103 U, 104 @ParamAnno("v") @ParamAnno2(2) V extends Runnable> { 105 public <@ParamAnno("x") @ParamAnno2(3) X, 106 Y, 107 @ParamAnno("z") @ParamAnno2(4) Z extends Cloneable> void foo() {} 108 } 109 110 @Target(ElementType.TYPE_PARAMETER) 111 @Retention(RetentionPolicy.RUNTIME) 112 @interface ParamAnno { 113 String value(); 114 } 115 116 @Target(ElementType.TYPE_PARAMETER) 117 @Retention(RetentionPolicy.RUNTIME) 118 @interface ParamAnno2 { 119 int value(); 120 }