author | jfranck |
Wed, 16 Dec 2015 20:00:03 +0100 | |
changeset 34708 | 4a1e3728135c |
parent 31139 | d48c3edf6a55 |
permissions | -rw-r--r-- |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
1 |
/* |
31139
d48c3edf6a55
8054304: Clarify treatment of bounds in j.l.r.Annotated{WildcardType,TypeVariable}.
sadayapalam
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
4 |
* |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
10 |
* |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
15 |
* accompanied this code). |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
16 |
* |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
20 |
* |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
23 |
* questions. |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
24 |
*/ |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
25 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
26 |
package sun.reflect.annotation; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
27 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
28 |
import java.lang.annotation.*; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
29 |
import java.lang.reflect.*; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
30 |
import java.util.ArrayList; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
31 |
import java.util.Arrays; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
32 |
import java.util.List; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
33 |
import java.util.Map; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
34 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
35 |
import static sun.reflect.annotation.TypeAnnotation.*; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
36 |
|
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
37 |
public final class AnnotatedTypeFactory { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
38 |
/** |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
39 |
* Create an AnnotatedType. |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
40 |
* |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
41 |
* @param type the type this AnnotatedType corresponds to |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
42 |
* @param currentLoc the location this AnnotatedType corresponds to |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
43 |
* @param actualTypeAnnos the type annotations this AnnotatedType has |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
44 |
* @param allOnSameTarget all type annotation on the same TypeAnnotationTarget |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
45 |
* as the AnnotatedType being built |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
46 |
* @param decl the declaration having the type use this AnnotatedType |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
47 |
* corresponds to |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
48 |
*/ |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
49 |
public static AnnotatedType buildAnnotatedType(Type type, |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
50 |
LocationInfo currentLoc, |
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
51 |
TypeAnnotation[] actualTypeAnnos, |
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
52 |
TypeAnnotation[] allOnSameTarget, |
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
53 |
AnnotatedElement decl) { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
54 |
if (type == null) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
55 |
return EMPTY_ANNOTATED_TYPE; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
56 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
57 |
if (isArray(type)) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
58 |
return new AnnotatedArrayTypeImpl(type, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
59 |
currentLoc, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
60 |
actualTypeAnnos, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
61 |
allOnSameTarget, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
62 |
decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
63 |
if (type instanceof Class) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
64 |
return new AnnotatedTypeBaseImpl(type, |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
65 |
currentLoc, |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
66 |
actualTypeAnnos, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
67 |
allOnSameTarget, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
68 |
decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
69 |
} else if (type instanceof TypeVariable) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
70 |
return new AnnotatedTypeVariableImpl((TypeVariable)type, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
71 |
currentLoc, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
72 |
actualTypeAnnos, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
73 |
allOnSameTarget, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
74 |
decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
75 |
} else if (type instanceof ParameterizedType) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
76 |
return new AnnotatedParameterizedTypeImpl((ParameterizedType)type, |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
77 |
currentLoc, |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
78 |
actualTypeAnnos, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
79 |
allOnSameTarget, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
80 |
decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
81 |
} else if (type instanceof WildcardType) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
82 |
return new AnnotatedWildcardTypeImpl((WildcardType) type, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
83 |
currentLoc, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
84 |
actualTypeAnnos, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
85 |
allOnSameTarget, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
86 |
decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
87 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
88 |
throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen."); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
89 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
90 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
91 |
public static LocationInfo nestingForType(Type type, LocationInfo addTo) { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
92 |
if (isArray(type)) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
93 |
return addTo; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
94 |
if (type instanceof Class) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
95 |
Class<?> clz = (Class)type; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
96 |
if (clz.getEnclosingClass() == null) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
97 |
return addTo; |
21361
6e4ef4e0097f
8023651: j.l.r.Constructor.getAnnotatedReceiverType() and j.l.r.Constructor.getAnnotatedReturnType() for inner classes return incorrect result
jfranck
parents:
21316
diff
changeset
|
98 |
if (Modifier.isStatic(clz.getModifiers())) |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
99 |
return nestingForType(clz.getEnclosingClass(), addTo); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
100 |
return nestingForType(clz.getEnclosingClass(), addTo.pushInner()); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
101 |
} else if (type instanceof ParameterizedType) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
102 |
ParameterizedType t = (ParameterizedType)type; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
103 |
if (t.getOwnerType() == null) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
104 |
return addTo; |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
105 |
return nestingForType(t.getOwnerType(), addTo.pushInner()); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
106 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
107 |
return addTo; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
108 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
109 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
110 |
private static boolean isArray(Type t) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
111 |
if (t instanceof Class) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
112 |
Class<?> c = (Class)t; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
113 |
if (c.isArray()) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
114 |
return true; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
115 |
} else if (t instanceof GenericArrayType) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
116 |
return true; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
117 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
118 |
return false; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
119 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
120 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
121 |
static final TypeAnnotation[] EMPTY_TYPE_ANNOTATION_ARRAY = new TypeAnnotation[0]; |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
122 |
static final AnnotatedType EMPTY_ANNOTATED_TYPE = new AnnotatedTypeBaseImpl(null, LocationInfo.BASE_LOCATION, |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
123 |
EMPTY_TYPE_ANNOTATION_ARRAY, EMPTY_TYPE_ANNOTATION_ARRAY, null); |
20481
2735b307d256
8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents:
15659
diff
changeset
|
124 |
static final AnnotatedType[] EMPTY_ANNOTATED_TYPE_ARRAY = new AnnotatedType[0]; |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
125 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
126 |
private static class AnnotatedTypeBaseImpl implements AnnotatedType { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
127 |
private final Type type; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
128 |
private final AnnotatedElement decl; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
129 |
private final LocationInfo location; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
130 |
private final TypeAnnotation[] allOnSameTargetTypeAnnotations; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
131 |
private final Map<Class <? extends Annotation>, Annotation> annotations; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
132 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
133 |
AnnotatedTypeBaseImpl(Type type, LocationInfo location, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
134 |
TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
135 |
AnnotatedElement decl) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
136 |
this.type = type; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
137 |
this.decl = decl; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
138 |
this.location = location; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
139 |
this.allOnSameTargetTypeAnnotations = allOnSameTargetTypeAnnotations; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
140 |
this.annotations = TypeAnnotationParser.mapTypeAnnotations(location.filter(actualTypeAnnotations)); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
141 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
142 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
143 |
// AnnotatedElement |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
144 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
145 |
public final Annotation[] getAnnotations() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
146 |
return getDeclaredAnnotations(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
147 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
148 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
149 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
150 |
public final <T extends Annotation> T getAnnotation(Class<T> annotation) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
151 |
return getDeclaredAnnotation(annotation); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
152 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
153 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
154 |
@Override |
15659
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
155 |
public final <T extends Annotation> T[] getAnnotationsByType(Class<T> annotation) { |
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
156 |
return getDeclaredAnnotationsByType(annotation); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
157 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
158 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
159 |
@Override |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
160 |
public final Annotation[] getDeclaredAnnotations() { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
161 |
return annotations.values().toArray(new Annotation[0]); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
162 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
163 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
164 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
165 |
@SuppressWarnings("unchecked") |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
166 |
public final <T extends Annotation> T getDeclaredAnnotation(Class<T> annotation) { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
167 |
return (T)annotations.get(annotation); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
168 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
169 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
170 |
@Override |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
171 |
public final <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotation) { |
21316
ca0a7cd228c9
8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents:
20481
diff
changeset
|
172 |
return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotations, annotation); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
173 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
174 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
175 |
// AnnotatedType |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
176 |
@Override |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
177 |
public final Type getType() { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
178 |
return type; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
179 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
180 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
181 |
@Override |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
182 |
public AnnotatedType getAnnotatedOwnerType() { |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
183 |
if (!(type instanceof Class<?>)) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
184 |
throw new IllegalStateException("Can't compute owner"); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
185 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
186 |
Class<?> inner = (Class<?>)type; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
187 |
Class<?> owner = inner.getDeclaringClass(); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
188 |
if (owner == null) // top-level, local or anonymous |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
189 |
return null; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
190 |
if (inner.isPrimitive() || inner == Void.TYPE) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
191 |
return null; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
192 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
193 |
LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1)); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
194 |
TypeAnnotation[]all = getTypeAnnotations(); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
195 |
List<TypeAnnotation> l = new ArrayList<>(all.length); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
196 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
197 |
for (TypeAnnotation t : all) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
198 |
if (t.getLocationInfo().isSameLocationInfo(outerLoc)) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
199 |
l.add(t); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
200 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
201 |
return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl()); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
202 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
203 |
} |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
204 |
|
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
205 |
// Implementation details |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
206 |
final LocationInfo getLocation() { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
207 |
return location; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
208 |
} |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
209 |
final TypeAnnotation[] getTypeAnnotations() { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
210 |
return allOnSameTargetTypeAnnotations; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
211 |
} |
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
212 |
final AnnotatedElement getDecl() { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
213 |
return decl; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
214 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
215 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
216 |
|
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
217 |
private static final class AnnotatedArrayTypeImpl extends AnnotatedTypeBaseImpl implements AnnotatedArrayType { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
218 |
AnnotatedArrayTypeImpl(Type type, LocationInfo location, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
219 |
TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
220 |
AnnotatedElement decl) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
221 |
super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
222 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
223 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
224 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
225 |
public AnnotatedType getAnnotatedGenericComponentType() { |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
226 |
Type t = getComponentType(); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
227 |
return AnnotatedTypeFactory.buildAnnotatedType(t, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
228 |
nestingForType(t, getLocation().pushArray()), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
229 |
getTypeAnnotations(), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
230 |
getTypeAnnotations(), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
231 |
getDecl()); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
232 |
} |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
233 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
234 |
@Override |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
235 |
public AnnotatedType getAnnotatedOwnerType() { |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
236 |
return null; |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
237 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
238 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
239 |
private Type getComponentType() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
240 |
Type t = getType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
241 |
if (t instanceof Class) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
242 |
Class<?> c = (Class)t; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
243 |
return c.getComponentType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
244 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
245 |
return ((GenericArrayType)t).getGenericComponentType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
246 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
247 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
248 |
|
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
249 |
private static final class AnnotatedTypeVariableImpl extends AnnotatedTypeBaseImpl implements AnnotatedTypeVariable { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
250 |
AnnotatedTypeVariableImpl(TypeVariable<?> type, LocationInfo location, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
251 |
TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
252 |
AnnotatedElement decl) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
253 |
super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
254 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
255 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
256 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
257 |
public AnnotatedType[] getAnnotatedBounds() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
258 |
return getTypeVariable().getAnnotatedBounds(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
259 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
260 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
261 |
@Override |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
262 |
public AnnotatedType getAnnotatedOwnerType() { |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
263 |
return null; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
264 |
} |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
265 |
|
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
266 |
private TypeVariable<?> getTypeVariable() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
267 |
return (TypeVariable)getType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
268 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
269 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
270 |
|
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
271 |
private static final class AnnotatedParameterizedTypeImpl extends AnnotatedTypeBaseImpl |
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
272 |
implements AnnotatedParameterizedType { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
273 |
AnnotatedParameterizedTypeImpl(ParameterizedType type, LocationInfo location, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
274 |
TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
275 |
AnnotatedElement decl) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
276 |
super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
277 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
278 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
279 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
280 |
public AnnotatedType[] getAnnotatedActualTypeArguments() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
281 |
Type[] arguments = getParameterizedType().getActualTypeArguments(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
282 |
AnnotatedType[] res = new AnnotatedType[arguments.length]; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
283 |
Arrays.fill(res, EMPTY_ANNOTATED_TYPE); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
284 |
int initialCapacity = getTypeAnnotations().length; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
285 |
for (int i = 0; i < res.length; i++) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
286 |
List<TypeAnnotation> l = new ArrayList<>(initialCapacity); |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
287 |
LocationInfo newLoc = nestingForType(arguments[i], getLocation().pushTypeArg((byte)i)); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
288 |
for (TypeAnnotation t : getTypeAnnotations()) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
289 |
if (t.getLocationInfo().isSameLocationInfo(newLoc)) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
290 |
l.add(t); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
291 |
res[i] = buildAnnotatedType(arguments[i], |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
292 |
newLoc, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
293 |
l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
294 |
getTypeAnnotations(), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
295 |
getDecl()); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
296 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
297 |
return res; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
298 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
299 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
300 |
@Override |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
301 |
public AnnotatedType getAnnotatedOwnerType() { |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
302 |
Type owner = getParameterizedType().getOwnerType(); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
303 |
if (owner == null) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
304 |
return null; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
305 |
LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1)); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
306 |
TypeAnnotation[]all = getTypeAnnotations(); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
307 |
List<TypeAnnotation> l = new ArrayList<>(all.length); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
308 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
309 |
for (TypeAnnotation t : all) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
310 |
if (t.getLocationInfo().isSameLocationInfo(outerLoc)) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
311 |
l.add(t); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
312 |
|
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
313 |
return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl()); |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
314 |
} |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
315 |
|
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
316 |
private ParameterizedType getParameterizedType() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
317 |
return (ParameterizedType)getType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
318 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
319 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
320 |
|
21622
643cd3c6f72d
8027796: Refactor Core Reflection for Type Annotations
jfranck
parents:
21361
diff
changeset
|
321 |
private static final class AnnotatedWildcardTypeImpl extends AnnotatedTypeBaseImpl implements AnnotatedWildcardType { |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
322 |
private final boolean hasUpperBounds; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
323 |
AnnotatedWildcardTypeImpl(WildcardType type, LocationInfo location, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
324 |
TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
325 |
AnnotatedElement decl) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
326 |
super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
327 |
hasUpperBounds = (type.getLowerBounds().length == 0); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
328 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
329 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
330 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
331 |
public AnnotatedType[] getAnnotatedUpperBounds() { |
31139
d48c3edf6a55
8054304: Clarify treatment of bounds in j.l.r.Annotated{WildcardType,TypeVariable}.
sadayapalam
parents:
25859
diff
changeset
|
332 |
if (!hasUpperBounds()) { |
d48c3edf6a55
8054304: Clarify treatment of bounds in j.l.r.Annotated{WildcardType,TypeVariable}.
sadayapalam
parents:
25859
diff
changeset
|
333 |
return new AnnotatedType[] { buildAnnotatedType(Object.class, |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
334 |
LocationInfo.BASE_LOCATION, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
335 |
EMPTY_TYPE_ANNOTATION_ARRAY, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
336 |
EMPTY_TYPE_ANNOTATION_ARRAY, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
337 |
null) |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
338 |
}; |
31139
d48c3edf6a55
8054304: Clarify treatment of bounds in j.l.r.Annotated{WildcardType,TypeVariable}.
sadayapalam
parents:
25859
diff
changeset
|
339 |
} |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
340 |
return getAnnotatedBounds(getWildcardType().getUpperBounds()); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
341 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
342 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
343 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
344 |
public AnnotatedType[] getAnnotatedLowerBounds() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
345 |
if (hasUpperBounds) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
346 |
return new AnnotatedType[0]; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
347 |
return getAnnotatedBounds(getWildcardType().getLowerBounds()); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
348 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
349 |
|
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
350 |
@Override |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
351 |
public AnnotatedType getAnnotatedOwnerType() { |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
352 |
return null; |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
353 |
} |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
354 |
|
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
355 |
private AnnotatedType[] getAnnotatedBounds(Type[] bounds) { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
356 |
AnnotatedType[] res = new AnnotatedType[bounds.length]; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
357 |
Arrays.fill(res, EMPTY_ANNOTATED_TYPE); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
358 |
int initialCapacity = getTypeAnnotations().length; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
359 |
for (int i = 0; i < res.length; i++) { |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
360 |
LocationInfo newLoc = nestingForType(bounds[i], getLocation().pushWildcard()); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
361 |
List<TypeAnnotation> l = new ArrayList<>(initialCapacity); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
362 |
for (TypeAnnotation t : getTypeAnnotations()) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
363 |
if (t.getLocationInfo().isSameLocationInfo(newLoc)) |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
364 |
l.add(t); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
365 |
res[i] = buildAnnotatedType(bounds[i], |
34708
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
366 |
newLoc, |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
367 |
l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
368 |
getTypeAnnotations(), |
4a1e3728135c
8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents:
31139
diff
changeset
|
369 |
getDecl()); |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
370 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
371 |
return res; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
372 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
373 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
374 |
private WildcardType getWildcardType() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
375 |
return (WildcardType)getType(); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
376 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
377 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
378 |
private boolean hasUpperBounds() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
379 |
return hasUpperBounds; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
380 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
381 |
} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff
changeset
|
382 |
} |