Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
duke@1 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 21 | * have any questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | |
duke@1 | 25 | /* |
duke@1 | 26 | * @test |
duke@1 | 27 | * @bug 4989091 5050782 5051962 |
duke@1 | 28 | * @summary Tests Declaration.getAnnotation method |
duke@1 | 29 | * @library ../../lib |
duke@1 | 30 | * @compile -source 1.5 GetAnno.java |
duke@1 | 31 | * @run main GetAnno |
duke@1 | 32 | */ |
duke@1 | 33 | |
duke@1 | 34 | |
duke@1 | 35 | import java.lang.annotation.*; |
duke@1 | 36 | import java.util.*; |
duke@1 | 37 | |
duke@1 | 38 | import com.sun.mirror.declaration.*; |
duke@1 | 39 | import com.sun.mirror.type.*; |
duke@1 | 40 | |
duke@1 | 41 | import static java.lang.annotation.RetentionPolicy.*; |
duke@1 | 42 | |
duke@1 | 43 | |
duke@1 | 44 | public class GetAnno extends Tester { |
duke@1 | 45 | |
duke@1 | 46 | public static void main(String[] args) { |
duke@1 | 47 | (new GetAnno()).run(); |
duke@1 | 48 | } |
duke@1 | 49 | |
duke@1 | 50 | |
duke@1 | 51 | // Annotations used by tests |
duke@1 | 52 | |
duke@1 | 53 | @Retention(RUNTIME) |
duke@1 | 54 | @interface AT1 { |
duke@1 | 55 | long l(); |
duke@1 | 56 | String s(); |
duke@1 | 57 | RetentionPolicy e(); |
duke@1 | 58 | String[] sa(); |
duke@1 | 59 | AT2 a(); |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | @Inherited |
duke@1 | 63 | @interface AT2 { |
duke@1 | 64 | } |
duke@1 | 65 | |
duke@1 | 66 | @interface AT3 { |
duke@1 | 67 | Class value() default String.class; |
duke@1 | 68 | } |
duke@1 | 69 | |
duke@1 | 70 | // Array-valued elements of various kinds. |
duke@1 | 71 | @interface AT4 { |
duke@1 | 72 | boolean[] bs(); |
duke@1 | 73 | long[] ls(); |
duke@1 | 74 | String[] ss(); |
duke@1 | 75 | RetentionPolicy[] es(); |
duke@1 | 76 | AT2[] as(); |
duke@1 | 77 | } |
duke@1 | 78 | |
duke@1 | 79 | |
duke@1 | 80 | @Test(result="@GetAnno$AT1(l=7, s=sigh, e=CLASS, sa=[in, out], " + |
duke@1 | 81 | "a=@GetAnno$AT2())") |
duke@1 | 82 | @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2) |
duke@1 | 83 | public Annotation getAnnotation() { |
duke@1 | 84 | MethodDeclaration m = getMethod("getAnnotation"); |
duke@1 | 85 | AT1 a = m.getAnnotation(AT1.class); |
duke@1 | 86 | if (a.l() != 7 || !a.s().equals("sigh") || a.e() != CLASS) |
duke@1 | 87 | throw new AssertionError(); |
duke@1 | 88 | return a; |
duke@1 | 89 | } |
duke@1 | 90 | |
duke@1 | 91 | @Test(result="null") |
duke@1 | 92 | public Annotation getAnnotationNotThere() { |
duke@1 | 93 | return thisClassDecl.getAnnotation(Deprecated.class); |
duke@1 | 94 | } |
duke@1 | 95 | |
duke@1 | 96 | @Test(result="@GetAnno$AT4(bs=[true, false], " + |
duke@1 | 97 | "ls=[9, 8], " + |
duke@1 | 98 | "ss=[black, white], " + |
duke@1 | 99 | "es=[CLASS, SOURCE], " + |
duke@1 | 100 | "as=[@GetAnno$AT2(), @GetAnno$AT2()])") |
duke@1 | 101 | @AT4(bs={true, false}, |
duke@1 | 102 | ls={9, 8}, |
duke@1 | 103 | ss={"black", "white"}, |
duke@1 | 104 | es={CLASS, SOURCE}, |
duke@1 | 105 | as={@AT2, @AT2}) |
duke@1 | 106 | public AT4 getAnnotationArrayValues() { |
duke@1 | 107 | MethodDeclaration m = getMethod("getAnnotationArrayValues"); |
duke@1 | 108 | return m.getAnnotation(AT4.class); |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | @Test(result="@GetAnno$AT3(value=java.lang.String)") |
duke@1 | 112 | @AT3(String.class) |
duke@1 | 113 | public AT3 getAnnotationWithClass1() { |
duke@1 | 114 | MethodDeclaration m = getMethod("getAnnotationWithClass1"); |
duke@1 | 115 | return m.getAnnotation(AT3.class); |
duke@1 | 116 | } |
duke@1 | 117 | |
duke@1 | 118 | @Test(result="java.lang.String") |
duke@1 | 119 | public TypeMirror getAnnotationWithClass2() { |
duke@1 | 120 | AT3 a = getAnnotationWithClass1(); |
duke@1 | 121 | try { |
duke@1 | 122 | Class c = a.value(); |
duke@1 | 123 | throw new AssertionError(); |
duke@1 | 124 | } catch (MirroredTypeException e) { |
duke@1 | 125 | return e.getTypeMirror(); |
duke@1 | 126 | } |
duke@1 | 127 | } |
duke@1 | 128 | |
duke@1 | 129 | @Test(result="boolean") |
duke@1 | 130 | @AT3(boolean.class) |
duke@1 | 131 | public TypeMirror getAnnotationWithPrim() { |
duke@1 | 132 | MethodDeclaration m = getMethod("getAnnotationWithPrim"); |
duke@1 | 133 | AT3 a = m.getAnnotation(AT3.class); |
duke@1 | 134 | try { |
duke@1 | 135 | Class c = a.value(); |
duke@1 | 136 | throw new AssertionError(); |
duke@1 | 137 | } catch (MirroredTypeException e) { |
duke@1 | 138 | return e.getTypeMirror(); |
duke@1 | 139 | } |
duke@1 | 140 | } |
duke@1 | 141 | |
duke@1 | 142 | // 5050782 |
duke@1 | 143 | @Test(result="null") |
duke@1 | 144 | public AT2 getInheritedAnnotation() { |
duke@1 | 145 | return thisClassDecl.getAnnotation(AT2.class); |
duke@1 | 146 | } |
duke@1 | 147 | |
duke@1 | 148 | /** |
duke@1 | 149 | * Verify that an annotation created by Declaration.getAnnotation() |
duke@1 | 150 | * has the same hash code as a like annotation created by core |
duke@1 | 151 | * reflection. |
duke@1 | 152 | */ |
duke@1 | 153 | @Test(result="true") |
duke@1 | 154 | @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2) |
duke@1 | 155 | public boolean getAnnotationHashCode() { |
duke@1 | 156 | MethodDeclaration m1 = getMethod("getAnnotationHashCode"); |
duke@1 | 157 | AT1 a1 = m1.getAnnotation(AT1.class); |
duke@1 | 158 | java.lang.reflect.Method m2 = null; |
duke@1 | 159 | try { |
duke@1 | 160 | m2 = this.getClass().getMethod("getAnnotationHashCode"); |
duke@1 | 161 | } catch (NoSuchMethodException e) { |
duke@1 | 162 | assert false; |
duke@1 | 163 | } |
duke@1 | 164 | AT1 a2 = m2.getAnnotation(AT1.class); |
duke@1 | 165 | return a1.hashCode() == a2.hashCode(); |
duke@1 | 166 | } |
duke@1 | 167 | } |