File: AnnoVal.java

package info (click to toggle)
libnb-javaparser-java 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 26,688 kB
  • sloc: java: 189,590; sh: 3,082; xml: 2,085; makefile: 379
file content (200 lines) | stat: -rw-r--r-- 6,091 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/*
 * @test
 * @bug 4853450 5014539 5034991
 * @summary Tests AnnotationValue methods.
 * @library ../../lib
 * @compile -source 1.5 AnnoVal.java
 * @run main/othervm AnnoVal
 */


import java.util.*;
import com.sun.mirror.declaration.*;
import com.sun.mirror.type.*;


public class AnnoVal extends Tester {

    public static void main(String[] args) {
        (new AnnoVal()).run();
    }

    @Test(result={
        "i Integer 2",
        "l Long 4294967296",
        "d Double 3.14",
        "b Boolean true",
        "c Character @",
        "s String sigh",
        // The following results reflect some implementation details.
        "k ClassTypeImpl java.lang.Boolean",
        "kb PrimitiveTypeImpl boolean",
        "ka ArrayTypeImpl java.lang.Boolean[]",
        "kab ArrayTypeImpl int[][]",
        "w ClassTypeImpl java.lang.Long",
        "e EnumConstantDeclarationImpl TYPE",
        "sa ArrayList [\"up\", \"down\"]",
        "a AnnotationMirrorImpl @AT1"})
    @AT2(i = 1 + 1,
         l = 1024 * 1024 * 1024 * 4L,
         d = 3.14,
         b = true,
         c = '@',
         s = "sigh",
         k = Boolean.class,
         kb = boolean.class,
         ka = Boolean[].class,          // bugid 5020899
         kab = int[][].class,           //      "
         w = Long.class,
         e = java.lang.annotation.ElementType.TYPE,
         sa = {"up", "down"},
         a = @AT1)
    Collection<String> getValue() {
        Collection<String> res = new ArrayList<String>();
        AnnotationMirror anno = getAnno("getValue", "AT2");

        for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> e :
                 anno.getElementValues().entrySet()) {
            Object val = e.getValue().getValue();
            res.add(String.format("%s %s %s",
                                  e.getKey().getSimpleName(),
                                  simpleClassName(val),
                                  val));
        }
        return res;
    }

    @Test(result={
        "int i 2",
        "long l 4294967296L",
        "double d 3.14",
        "boolean b true",
        "char c '@'",
        "java.lang.String s \"sigh\"",
        "java.lang.Class k java.lang.Boolean.class",
        "java.lang.Class kb boolean.class",
        "java.lang.Class ka java.lang.Boolean[].class",
        "java.lang.Class kab int[][].class",
        "java.lang.Class<? extends java.lang.Number> w java.lang.Long.class",
        "java.lang.annotation.ElementType e java.lang.annotation.ElementType.TYPE",
        "java.lang.String[] sa {\"up\", \"down\"}",
        "AT1 a @AT1"})
    Collection<String> toStringTests() {
        Collection<String> res = new ArrayList<String>();
        AnnotationMirror anno = getAnno("getValue", "AT2");

        for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
                 anno.getElementValues().entrySet()) {
            res.add(String.format("%s %s %s",
                                  e.getKey().getReturnType(),
                                  e.getKey().getSimpleName(),
                                  e.getValue().toString()));
        }
        return res;
    }

    @Test(result={
        "byte b 0x0b",
        "float f 3.0f",
        "double nan 0.0/0.0",
        "double hi 1.0/0.0",
        "float lo -1.0f/0.0f",
        "char newline '\\n'",
        "char ff '\\u00ff'",
        "java.lang.String s \"\\\"high\\tlow\\\"\"",
        "java.lang.String smiley \"\\u263a\""})
    @AT3(b = 11,
         f = 3,
         nan = 0.0/0.0,
         hi = 1.0/0.0,
         lo = -1.0f/0.0f,
         newline = '\n',
         ff = '\u00FF',
         s = "\"high\tlow\"",
         smiley = "\u263A")
    Collection<String> toStringFancy() {
        Collection<String> res = new ArrayList<String>();
        AnnotationMirror anno = getAnno("toStringFancy", "AT3");

        for (Map.Entry<AnnotationTypeElementDeclaration,AnnotationValue> e :
                 anno.getElementValues().entrySet()) {
            res.add(String.format("%s %s %s",
                                  e.getKey().getReturnType(),
                                  e.getKey().getSimpleName(),
                                  e.getValue().toString()));
        }
        return res;
    }


    /**
     * Returns the simple name of an object's class.
     */
    private String simpleClassName(Object o) {
        return (o == null)
            ? "null"
            : o.getClass().getName().replaceFirst(".*\\.", "");
    }
}


/*
 * Annotations used for testing.
 */

@interface AT1 {
    String value() default "";
}

@interface AT2 {
    int i();
    long l();
    double d();
    boolean b();
    char c();
    String s();
    Class k();
    Class kb();
    Class ka();
    Class kab();
    Class<? extends Number> w();
    java.lang.annotation.ElementType e();
    String[] sa();
    AT1 a();
}

@interface AT3 {
    byte b();
    float f();
    double nan();
    double hi();
    float lo();
    char newline();
    char ff();
    String s();
    String smiley();
}