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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Use and Distribution is subject to the Java Research License available
 * at <http://wwws.sun.com/software/communitysource/jrl.html>.
 */

package com.sun.tools.javac.code;

import java.util.LinkedHashMap;
import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.util.*;

import static com.sun.tools.javac.code.TypeTags.*;

/** An annotation value.
 *
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
 *  you write code that depends on this, you do so at your own risk.
 *  This code and its internal interfaces are subject to change or
 *  deletion without notice.</b>
 */
@Version("%W% %E%")
public abstract class Attribute implements AnnotationValue {

    /** The type of the annotation element. */
    public Type type;

    public Attribute(Type type) {
    this.type = type;
    }

    public abstract void accept(Visitor v);

    public Object getValue() {
    throw new UnsupportedOperationException();
    }

    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
    throw new UnsupportedOperationException();
    }


    /** The value for an annotation element of primitive type or String. */
    public static class Constant extends Attribute {
    public final Object value;
    public void accept(Visitor v) { v.visitConstant(this); }
    public Constant(Type type, Object value) {
        super(type);
        this.value = value;
    }
    public String toString() {
        return Constants.format(value, type);
    }
    public Object getValue() {
        return Constants.decode(value, type);
    }
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        if (value instanceof String)
        return v.visitString((String) value, p);
        if (value instanceof Integer) {
        int i = (Integer) value;
        switch (type.tag) {
        case BOOLEAN:   return v.visitBoolean(i != 0, p);
        case CHAR:  return v.visitChar((char) i, p);
        case BYTE:  return v.visitByte((byte) i, p);
        case SHORT: return v.visitShort((short) i, p);
        case INT:   return v.visitInt(i, p);
        }
        }
        switch (type.tag) {
        case LONG:      return v.visitLong((Long) value, p);
        case FLOAT:     return v.visitFloat((Float) value, p);
        case DOUBLE:    return v.visitDouble((Double) value, p);
        }
        throw new AssertionError("Bad annotation element value: " + value);
    }
    }

    /** The value for an annotation element of type java.lang.Class,
     *  represented as a ClassSymbol.
     */
    public static class Class extends Attribute {
    public final Type type;
    public void accept(Visitor v) { v.visitClass(this); }
    public Class(Types types, Type type) {
        super(makeClassType(types, type));
        this.type = type;
    }
        static Type makeClassType(Types types, Type type) {
            Type arg = type.isPrimitive()
                ? types.boxedClass(type).type
                : types.erasure(type);
            return new Type.ClassType(types.syms.classType.getEnclosingType(),
                                      List.of(arg),
                                      types.syms.classType.tsym);
        }
    public String toString() {
        return type + ".class";
    }
    public Type getValue() {
        return type;
    }
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        return v.visitType(type, p);
    }
    }

    /** A compound annotation element value, the type of which is an
     *  attribute interface.
     */
    public static class Compound extends Attribute implements AnnotationMirror {
    /** The attributes values, as pairs.  Each pair contains a
     *  reference to the accessing method in the attribute interface
     *  and the value to be returned when that method is called to
     *  access this attribute.
     */
    public final List<Pair<MethodSymbol,Attribute>> values;
    public Compound(Type type,
            List<Pair<MethodSymbol,Attribute>> values) {
        super(type);
        this.values = values;
    }
    public void accept(Visitor v) { v.visitCompound(this); }

    /**
     * Returns a string representation of this annotation.
     * String is of one of the forms:
     *     @com.example.foo(name1=val1, name2=val2)
     *     @com.example.foo(val)
     *     @com.example.foo
     * Omit parens for marker annotations, and omit "value=" when allowed.
     */
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("@");
        buf.append(type);
        int len = values.length();
        if (len > 0) {
        buf.append('(');
        boolean first = true;
        for (Pair<MethodSymbol, Attribute> value : values) {
            if (!first) buf.append(", ");
            first = false;

            Name name = value.fst.name;
            if (len > 1 || name != name.table.value) {
            buf.append(name);
            buf.append('=');
            }
            buf.append(value.snd);
        }
        buf.append(')');
        }
        return buf.toString();
    }

    public Attribute member(Name member) {
        for (Pair<MethodSymbol,Attribute> pair : values)
        if (pair.fst.name == member) return pair.snd;
        return null;
    }

    public Attribute.Compound getValue() {
        return this;
    }

    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        return v.visitAnnotation(this, p);
    }

    public DeclaredType getAnnotationType() {
        return (DeclaredType) type;
    }

    public Map<MethodSymbol, Attribute> getElementValues() {
        Map<MethodSymbol, Attribute> valmap =
        new LinkedHashMap<MethodSymbol, Attribute>();
        for (Pair<MethodSymbol, Attribute> value : values)
        valmap.put(value.fst, value.snd);
        return valmap;
    }
    }

    /** The value for an annotation element of an array type.
     */
    public static class Array extends Attribute {
    public final Attribute[] values;
    public Array(Type type, Attribute[] values) {
        super(type);
        this.values = values;
    }
    public void accept(Visitor v) { v.visitArray(this); }
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append('{');
        boolean first = true;
        for (Attribute value : values) {
        if (!first)
            buf.append(", ");
        first = false;
        buf.append(value);
        }
        buf.append('}');
        return buf.toString();
    }
    public List<Attribute> getValue() {
        return List.from(values);
    }
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        return v.visitArray(getValue(), p);
    }
    }

    /** The value for an annotation element of an enum type.
     */
    public static class Enum extends Attribute {
    public VarSymbol value;
    public Enum(Type type, VarSymbol value) {
        super(type);
        assert value != null;
        this.value = value;
    }
    public void accept(Visitor v) { v.visitEnum(this); }
    public String toString() {
        return value.enclClass() + "." + value; // qualified name
    }
    public VarSymbol getValue() {
        return value;
    }
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        return v.visitEnumConstant(value, p);
    }
    }

    public static class Error extends Attribute {
    public Error(Type type) {
        super(type);
    }
    public void accept(Visitor v) { v.visitError(this); }
    public String toString() {
        return "<error>";
    }
    public String getValue() {
        return toString();
    }
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
        return v.visitString(toString(), p);
    }
    }

    /** A visitor type for dynamic dispatch on the kind of attribute value. */
    public static interface Visitor {
    void visitConstant(Attribute.Constant value);
    void visitClass(Attribute.Class clazz);
    void visitCompound(Attribute.Compound compound);
    void visitArray(Attribute.Array array);
    void visitEnum(Attribute.Enum e);
    void visitError(Attribute.Error e);
    }
}
			
			

Browsed Source: [clear]