29 import java.lang.reflect.AnnotatedType;
30 import java.lang.reflect.Array;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.GenericDeclaration;
33 import java.lang.reflect.Member;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Type;
36 import java.lang.reflect.TypeVariable;
37 import java.util.LinkedHashMap;
38 import java.util.Map;
39 import java.util.Objects;
40 import sun.reflect.annotation.AnnotationSupport;
41 import sun.reflect.annotation.TypeAnnotationParser;
42 import sun.reflect.annotation.AnnotationType;
43 import sun.reflect.generics.factory.GenericsFactory;
44 import sun.reflect.generics.tree.FieldTypeSignature;
45 import sun.reflect.generics.visitor.Reifier;
46 import sun.reflect.misc.ReflectUtil;
47
48 /**
49 * Implementation of <tt>java.lang.reflect.TypeVariable</tt> interface
50 * for core reflection.
51 */
52 public class TypeVariableImpl<D extends GenericDeclaration>
53 extends LazyReflectiveObjectGenerator implements TypeVariable<D> {
54 private final D genericDeclaration;
55 private final String name;
56
57 /**
58 * The upper bounds. Lazily converted from FieldTypeSignature[] to Type[].
59 * We are required to evaluate the bounds lazily, so we store them as ASTs
60 * until we are first asked for them. This also neatly solves the problem
61 * with F-bounds - you can't reify them before the formal is defined.
62 */
63 private volatile Object[] bounds;
64
65 // constructor is private to enforce access through static factory
66 private TypeVariableImpl(D decl, String n, FieldTypeSignature[] bs,
67 GenericsFactory f) {
68 super(f);
69 genericDeclaration = decl;
82 * objects that represent the bounds of this type variable
83 * @return A type variable with name, bounds, declaration and factory
84 * specified
85 */
86 public static <T extends GenericDeclaration>
87 TypeVariableImpl<T> make(T decl, String name,
88 FieldTypeSignature[] bs,
89 GenericsFactory f) {
90
91 if (!((decl instanceof Class) ||
92 (decl instanceof Method) ||
93 (decl instanceof Constructor))) {
94 throw new AssertionError("Unexpected kind of GenericDeclaration" +
95 decl.getClass().toString());
96 }
97 return new TypeVariableImpl<T>(decl, name, bs, f);
98 }
99
100
101 /**
102 * Returns an array of <tt>Type</tt> objects representing the
103 * upper bound(s) of this type variable. Note that if no upper bound is
104 * explicitly declared, the upper bound is <tt>Object</tt>.
105 *
106 * <p>For each upper bound B:
107 * <ul>
108 * <li>if B is a parameterized type or a type variable, it is created,
109 * (see {@link #ParameterizedType} for the details of the creation
110 * process for parameterized types).
111 * <li>Otherwise, B is resolved.
112 * </ul>
113 *
114 * @throws <tt>TypeNotPresentException</tt> if any of the
115 * bounds refers to a non-existent type declaration
116 * @throws <tt>MalformedParameterizedTypeException</tt> if any of the
117 * bounds refer to a parameterized type that cannot be instantiated
118 * for any reason
119 * @return an array of Types representing the upper bound(s) of this
120 * type variable
121 */
122 public Type[] getBounds() {
123 Object[] value = bounds;
124 if (value instanceof FieldTypeSignature[]) {
125 value = reifyBounds((FieldTypeSignature[])value);
126 bounds = value;
127 }
128 return (Type[])value.clone();
129 }
130
131 /**
132 * Returns the <tt>GenericDeclaration</tt> object representing the
133 * generic declaration that declared this type variable.
134 *
135 * @return the generic declaration that declared this type variable.
136 *
137 * @since 1.5
138 */
139 public D getGenericDeclaration() {
140 if (genericDeclaration instanceof Class)
141 ReflectUtil.checkPackageAccess((Class)genericDeclaration);
142 else if ((genericDeclaration instanceof Method) ||
143 (genericDeclaration instanceof Constructor))
144 ReflectUtil.conservativeCheckMemberAccess((Member)genericDeclaration);
145 else
146 throw new AssertionError("Unexpected kind of GenericDeclaration");
147 return genericDeclaration;
148 }
149
150
151 /**
152 * Returns the name of this type variable, as it occurs in the source code.
|
29 import java.lang.reflect.AnnotatedType;
30 import java.lang.reflect.Array;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.GenericDeclaration;
33 import java.lang.reflect.Member;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Type;
36 import java.lang.reflect.TypeVariable;
37 import java.util.LinkedHashMap;
38 import java.util.Map;
39 import java.util.Objects;
40 import sun.reflect.annotation.AnnotationSupport;
41 import sun.reflect.annotation.TypeAnnotationParser;
42 import sun.reflect.annotation.AnnotationType;
43 import sun.reflect.generics.factory.GenericsFactory;
44 import sun.reflect.generics.tree.FieldTypeSignature;
45 import sun.reflect.generics.visitor.Reifier;
46 import sun.reflect.misc.ReflectUtil;
47
48 /**
49 * Implementation of {@code java.lang.reflect.TypeVariable} interface
50 * for core reflection.
51 */
52 public class TypeVariableImpl<D extends GenericDeclaration>
53 extends LazyReflectiveObjectGenerator implements TypeVariable<D> {
54 private final D genericDeclaration;
55 private final String name;
56
57 /**
58 * The upper bounds. Lazily converted from FieldTypeSignature[] to Type[].
59 * We are required to evaluate the bounds lazily, so we store them as ASTs
60 * until we are first asked for them. This also neatly solves the problem
61 * with F-bounds - you can't reify them before the formal is defined.
62 */
63 private volatile Object[] bounds;
64
65 // constructor is private to enforce access through static factory
66 private TypeVariableImpl(D decl, String n, FieldTypeSignature[] bs,
67 GenericsFactory f) {
68 super(f);
69 genericDeclaration = decl;
82 * objects that represent the bounds of this type variable
83 * @return A type variable with name, bounds, declaration and factory
84 * specified
85 */
86 public static <T extends GenericDeclaration>
87 TypeVariableImpl<T> make(T decl, String name,
88 FieldTypeSignature[] bs,
89 GenericsFactory f) {
90
91 if (!((decl instanceof Class) ||
92 (decl instanceof Method) ||
93 (decl instanceof Constructor))) {
94 throw new AssertionError("Unexpected kind of GenericDeclaration" +
95 decl.getClass().toString());
96 }
97 return new TypeVariableImpl<T>(decl, name, bs, f);
98 }
99
100
101 /**
102 * Returns an array of {@code Type} objects representing the
103 * upper bound(s) of this type variable. Note that if no upper bound is
104 * explicitly declared, the upper bound is {@code Object}.
105 *
106 * <p>For each upper bound B:
107 * <ul>
108 * <li>if B is a parameterized type or a type variable, it is created,
109 * (see {@link #ParameterizedType} for the details of the creation
110 * process for parameterized types).
111 * <li>Otherwise, B is resolved.
112 * </ul>
113 *
114 * @throws {@code TypeNotPresentException} if any of the
115 * bounds refers to a non-existent type declaration
116 * @throws {@code MalformedParameterizedTypeException} if any of the
117 * bounds refer to a parameterized type that cannot be instantiated
118 * for any reason
119 * @return an array of Types representing the upper bound(s) of this
120 * type variable
121 */
122 public Type[] getBounds() {
123 Object[] value = bounds;
124 if (value instanceof FieldTypeSignature[]) {
125 value = reifyBounds((FieldTypeSignature[])value);
126 bounds = value;
127 }
128 return (Type[])value.clone();
129 }
130
131 /**
132 * Returns the {@code GenericDeclaration} object representing the
133 * generic declaration that declared this type variable.
134 *
135 * @return the generic declaration that declared this type variable.
136 *
137 * @since 1.5
138 */
139 public D getGenericDeclaration() {
140 if (genericDeclaration instanceof Class)
141 ReflectUtil.checkPackageAccess((Class)genericDeclaration);
142 else if ((genericDeclaration instanceof Method) ||
143 (genericDeclaration instanceof Constructor))
144 ReflectUtil.conservativeCheckMemberAccess((Member)genericDeclaration);
145 else
146 throw new AssertionError("Unexpected kind of GenericDeclaration");
147 return genericDeclaration;
148 }
149
150
151 /**
152 * Returns the name of this type variable, as it occurs in the source code.
|