1 /* 2 * Copyright (c) 2004, 2008, 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 /* 26 * @test 27 * @bug 4989091 5050782 5051962 28 * @summary Tests Declaration.getAnnotation method 29 * @library ../../lib 30 * @compile -source 1.5 GetAnno.java 31 * @run main/othervm GetAnno 32 */ 33 34 35 import java.lang.annotation.*; 36 import java.util.*; 37 38 import com.sun.mirror.declaration.*; 39 import com.sun.mirror.type.*; 40 41 import static java.lang.annotation.RetentionPolicy.*; 42 43 44 public class GetAnno extends Tester { 45 46 public static void main(String[] args) { 47 (new GetAnno()).run(); 48 } 49 50 51 // Annotations used by tests 52 53 @Retention(RUNTIME) 54 @interface AT1 { 55 long l(); 56 String s(); 57 RetentionPolicy e(); 58 String[] sa(); 59 AT2 a(); 60 } 61 62 @Inherited 63 @interface AT2 { 64 } 65 66 @interface AT3 { 67 Class value() default String.class; 68 } 69 70 // Array-valued elements of various kinds. 71 @interface AT4 { 72 boolean[] bs(); 73 long[] ls(); 74 String[] ss(); 75 RetentionPolicy[] es(); 76 AT2[] as(); 77 } 78 79 80 @Test(result="@GetAnno$AT1(l=7, s=sigh, e=CLASS, sa=[in, out], " + 81 "a=@GetAnno$AT2())") 82 @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2) 83 public Annotation getAnnotation() { 84 MethodDeclaration m = getMethod("getAnnotation"); 85 AT1 a = m.getAnnotation(AT1.class); 86 if (a.l() != 7 || !a.s().equals("sigh") || a.e() != CLASS) 87 throw new AssertionError(); 88 return a; 89 } 90 91 @Test(result="null") 92 public Annotation getAnnotationNotThere() { 93 return thisClassDecl.getAnnotation(Deprecated.class); 94 } 95 96 @Test(result="@GetAnno$AT4(bs=[true, false], " + 97 "ls=[9, 8], " + 98 "ss=[black, white], " + 99 "es=[CLASS, SOURCE], " + 100 "as=[@GetAnno$AT2(), @GetAnno$AT2()])") 101 @AT4(bs={true, false}, 102 ls={9, 8}, 103 ss={"black", "white"}, 104 es={CLASS, SOURCE}, 105 as={@AT2, @AT2}) 106 public AT4 getAnnotationArrayValues() { 107 MethodDeclaration m = getMethod("getAnnotationArrayValues"); 108 return m.getAnnotation(AT4.class); 109 } 110 111 @Test(result="@GetAnno$AT3(value=java.lang.String)") 112 @AT3(String.class) 113 public AT3 getAnnotationWithClass1() { 114 MethodDeclaration m = getMethod("getAnnotationWithClass1"); 115 return m.getAnnotation(AT3.class); 116 } 117 118 @Test(result="java.lang.String") 119 public TypeMirror getAnnotationWithClass2() { 120 AT3 a = getAnnotationWithClass1(); 121 try { 122 Class c = a.value(); 123 throw new AssertionError(); 124 } catch (MirroredTypeException e) { 125 return e.getTypeMirror(); 126 } 127 } 128 129 @Test(result="boolean") 130 @AT3(boolean.class) 131 public TypeMirror getAnnotationWithPrim() { 132 MethodDeclaration m = getMethod("getAnnotationWithPrim"); 133 AT3 a = m.getAnnotation(AT3.class); 134 try { 135 Class c = a.value(); 136 throw new AssertionError(); 137 } catch (MirroredTypeException e) { 138 return e.getTypeMirror(); 139 } 140 } 141 142 // 5050782 143 @Test(result="null") 144 public AT2 getInheritedAnnotation() { 145 return thisClassDecl.getAnnotation(AT2.class); 146 } 147 148 /** 149 * Verify that an annotation created by Declaration.getAnnotation() 150 * has the same hash code as a like annotation created by core 151 * reflection. 152 */ 153 @Test(result="true") 154 @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2) 155 public boolean getAnnotationHashCode() { 156 MethodDeclaration m1 = getMethod("getAnnotationHashCode"); 157 AT1 a1 = m1.getAnnotation(AT1.class); 158 java.lang.reflect.Method m2 = null; 159 try { 160 m2 = this.getClass().getMethod("getAnnotationHashCode"); 161 } catch (NoSuchMethodException e) { 162 assert false; 163 } 164 AT1 a2 = m2.getAnnotation(AT1.class); 165 return a1.hashCode() == a2.hashCode(); 166 } 167 }