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