langtools/test/tools/apt/mirror/declaration/AnnoVal.java
author lana
Tue, 01 Jun 2010 14:17:38 -0700
changeset 5597 ab490f66d2cf
parent 5520 86e4b9a9da40
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     2
 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    21
 * questions.
10
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 4853450 5014539 5034991
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @summary Tests AnnotationValue methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @library ../../lib
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 * @compile -source 1.5 AnnoVal.java
1467
290837884931 6749967: regression tests for apt should be same-vm friendly
jjg
parents: 10
diff changeset
    31
 * @run main/othervm AnnoVal
10
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.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.mirror.declaration.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.mirror.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class AnnoVal extends Tester {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        (new AnnoVal()).run();
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    @Test(result={
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        "i Integer 2",
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        "l Long 4294967296",
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        "d Double 3.14",
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        "b Boolean true",
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        "c Character @",
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        "s String sigh",
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        // The following results reflect some implementation details.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        "k ClassTypeImpl java.lang.Boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        "kb PrimitiveTypeImpl boolean",
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        "ka ArrayTypeImpl java.lang.Boolean[]",
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        "kab ArrayTypeImpl int[][]",
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        "w ClassTypeImpl java.lang.Long",
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        "e EnumConstantDeclarationImpl TYPE",
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        "sa ArrayList [\"up\", \"down\"]",
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        "a AnnotationMirrorImpl @AT1"})
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    @AT2(i = 1 + 1,
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
         l = 1024 * 1024 * 1024 * 4L,
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
         d = 3.14,
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
         b = true,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
         c = '@',
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
         s = "sigh",
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
         k = Boolean.class,
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
         kb = boolean.class,
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
         ka = Boolean[].class,          // bugid 5020899
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
         kab = int[][].class,           //      "
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
         w = Long.class,
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
         e = java.lang.annotation.ElementType.TYPE,
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
         sa = {"up", "down"},
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
         a = @AT1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    Collection<String> getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        Collection<String> res = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        AnnotationMirror anno = getAnno("getValue", "AT2");
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> e :
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                 anno.getElementValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            Object val = e.getValue().getValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            res.add(String.format("%s %s %s",
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                                  e.getKey().getSimpleName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                                  simpleClassName(val),
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                                  val));
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return res;
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={
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        "int i 2",
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        "long l 4294967296L",
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        "double d 3.14",
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        "boolean b true",
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        "char c '@'",
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        "java.lang.String s \"sigh\"",
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        "java.lang.Class k java.lang.Boolean.class",
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        "java.lang.Class kb boolean.class",
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        "java.lang.Class ka java.lang.Boolean[].class",
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        "java.lang.Class kab int[][].class",
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        "java.lang.Class<? extends java.lang.Number> w java.lang.Long.class",
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        "java.lang.annotation.ElementType e java.lang.annotation.ElementType.TYPE",
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        "java.lang.String[] sa {\"up\", \"down\"}",
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        "AT1 a @AT1"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    Collection<String> toStringTests() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        Collection<String> res = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        AnnotationMirror anno = getAnno("getValue", "AT2");
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                 anno.getElementValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            res.add(String.format("%s %s %s",
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                                  e.getKey().getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                                  e.getKey().getSimpleName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                                  e.getValue().toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    @Test(result={
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        "byte b 0x0b",
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        "float f 3.0f",
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        "double nan 0.0/0.0",
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        "double hi 1.0/0.0",
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        "float lo -1.0f/0.0f",
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        "char newline '\\n'",
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        "char ff '\\u00ff'",
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        "java.lang.String s \"\\\"high\\tlow\\\"\"",
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        "java.lang.String smiley \"\\u263a\""})
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    @AT3(b = 11,
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
         f = 3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
         nan = 0.0/0.0,
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
         hi = 1.0/0.0,
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
         lo = -1.0f/0.0f,
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
         newline = '\n',
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
         ff = '\u00FF',
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
         s = "\"high\tlow\"",
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
         smiley = "\u263A")
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    Collection<String> toStringFancy() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        Collection<String> res = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        AnnotationMirror anno = getAnno("toStringFancy", "AT3");
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                 anno.getElementValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            res.add(String.format("%s %s %s",
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                                  e.getKey().getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                                  e.getKey().getSimpleName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                                  e.getValue().toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * Returns the simple name of an object's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    private String simpleClassName(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        return (o == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            ? "null"
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            : o.getClass().getName().replaceFirst(".*\\.", "");
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
 * Annotations used for testing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
@interface AT1 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    String value() default "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
@interface AT2 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    int i();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    long l();
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    double d();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    boolean b();
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    char c();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    String s();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    Class k();
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    Class kb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    Class ka();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    Class kab();
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    Class<? extends Number> w();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    java.lang.annotation.ElementType e();
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    String[] sa();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    AT1 a();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
@interface AT3 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    byte b();
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    float f();
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    double nan();
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    double hi();
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    float lo();
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    char newline();
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    char ff();
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    String s();
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    String smiley();
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
}