test/tools/apt/mirror/declaration/GetAnno.java

changeset 1
9a66ca7c79fa
child 132
a54ef8459576
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/apt/mirror/declaration/GetAnno.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*
     1.5 + * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +/*
    1.29 + * @test
    1.30 + * @bug 4989091 5050782 5051962
    1.31 + * @summary Tests Declaration.getAnnotation method
    1.32 + * @library ../../lib
    1.33 + * @compile -source 1.5 GetAnno.java
    1.34 + * @run main GetAnno
    1.35 + */
    1.36 +
    1.37 +
    1.38 +import java.lang.annotation.*;
    1.39 +import java.util.*;
    1.40 +
    1.41 +import com.sun.mirror.declaration.*;
    1.42 +import com.sun.mirror.type.*;
    1.43 +
    1.44 +import static java.lang.annotation.RetentionPolicy.*;
    1.45 +
    1.46 +
    1.47 +public class GetAnno extends Tester {
    1.48 +
    1.49 +    public static void main(String[] args) {
    1.50 +        (new GetAnno()).run();
    1.51 +    }
    1.52 +
    1.53 +
    1.54 +    // Annotations used by tests
    1.55 +
    1.56 +    @Retention(RUNTIME)
    1.57 +    @interface AT1 {
    1.58 +        long l();
    1.59 +        String s();
    1.60 +        RetentionPolicy e();
    1.61 +        String[] sa();
    1.62 +        AT2 a();
    1.63 +    }
    1.64 +
    1.65 +    @Inherited
    1.66 +    @interface AT2 {
    1.67 +    }
    1.68 +
    1.69 +    @interface AT3 {
    1.70 +        Class value() default String.class;
    1.71 +    }
    1.72 +
    1.73 +    // Array-valued elements of various kinds.
    1.74 +    @interface AT4 {
    1.75 +        boolean[] bs();
    1.76 +        long[] ls();
    1.77 +        String[] ss();
    1.78 +        RetentionPolicy[] es();
    1.79 +        AT2[] as();
    1.80 +    }
    1.81 +
    1.82 +
    1.83 +    @Test(result="@GetAnno$AT1(l=7, s=sigh, e=CLASS, sa=[in, out], " +
    1.84 +                              "a=@GetAnno$AT2())")
    1.85 +    @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2)
    1.86 +    public Annotation getAnnotation() {
    1.87 +        MethodDeclaration m = getMethod("getAnnotation");
    1.88 +        AT1 a = m.getAnnotation(AT1.class);
    1.89 +        if (a.l() != 7 || !a.s().equals("sigh") || a.e() != CLASS)
    1.90 +            throw new AssertionError();
    1.91 +        return a;
    1.92 +    }
    1.93 +
    1.94 +    @Test(result="null")
    1.95 +    public Annotation getAnnotationNotThere() {
    1.96 +        return thisClassDecl.getAnnotation(Deprecated.class);
    1.97 +    }
    1.98 +
    1.99 +    @Test(result="@GetAnno$AT4(bs=[true, false], " +
   1.100 +                              "ls=[9, 8], " +
   1.101 +                              "ss=[black, white], " +
   1.102 +                              "es=[CLASS, SOURCE], " +
   1.103 +                              "as=[@GetAnno$AT2(), @GetAnno$AT2()])")
   1.104 +    @AT4(bs={true, false},
   1.105 +         ls={9, 8},
   1.106 +         ss={"black", "white"},
   1.107 +         es={CLASS, SOURCE},
   1.108 +         as={@AT2, @AT2})
   1.109 +    public AT4 getAnnotationArrayValues() {
   1.110 +        MethodDeclaration m = getMethod("getAnnotationArrayValues");
   1.111 +        return m.getAnnotation(AT4.class);
   1.112 +    }
   1.113 +
   1.114 +    @Test(result="@GetAnno$AT3(value=java.lang.String)")
   1.115 +    @AT3(String.class)
   1.116 +    public AT3 getAnnotationWithClass1() {
   1.117 +        MethodDeclaration m = getMethod("getAnnotationWithClass1");
   1.118 +        return m.getAnnotation(AT3.class);
   1.119 +    }
   1.120 +
   1.121 +    @Test(result="java.lang.String")
   1.122 +    public TypeMirror getAnnotationWithClass2() {
   1.123 +        AT3 a = getAnnotationWithClass1();
   1.124 +        try {
   1.125 +            Class c = a.value();
   1.126 +            throw new AssertionError();
   1.127 +        } catch (MirroredTypeException e) {
   1.128 +            return e.getTypeMirror();
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    @Test(result="boolean")
   1.133 +    @AT3(boolean.class)
   1.134 +    public TypeMirror getAnnotationWithPrim() {
   1.135 +        MethodDeclaration m = getMethod("getAnnotationWithPrim");
   1.136 +        AT3 a = m.getAnnotation(AT3.class);
   1.137 +        try {
   1.138 +            Class c = a.value();
   1.139 +            throw new AssertionError();
   1.140 +        } catch (MirroredTypeException e) {
   1.141 +            return e.getTypeMirror();
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    // 5050782
   1.146 +    @Test(result="null")
   1.147 +    public AT2 getInheritedAnnotation() {
   1.148 +        return thisClassDecl.getAnnotation(AT2.class);
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Verify that an annotation created by Declaration.getAnnotation()
   1.153 +     * has the same hash code as a like annotation created by core
   1.154 +     * reflection.
   1.155 +     */
   1.156 +    @Test(result="true")
   1.157 +    @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2)
   1.158 +    public boolean getAnnotationHashCode() {
   1.159 +        MethodDeclaration m1 = getMethod("getAnnotationHashCode");
   1.160 +        AT1 a1 = m1.getAnnotation(AT1.class);
   1.161 +        java.lang.reflect.Method m2 = null;
   1.162 +        try {
   1.163 +            m2 = this.getClass().getMethod("getAnnotationHashCode");
   1.164 +        } catch (NoSuchMethodException e) {
   1.165 +            assert false;
   1.166 +        }
   1.167 +        AT1 a2 = m2.getAnnotation(AT1.class);
   1.168 +        return a1.hashCode() == a2.hashCode();
   1.169 +    }
   1.170 +}

mercurial