author | mchung |
Tue, 16 Apr 2013 21:39:52 -0700 | |
changeset 16906 | 44dfee24cb71 |
parent 16743 | b0b34102bb4c |
child 20482 | 6dc1f81f522c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15294
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang.reflect; |
|
27 |
||
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16743
diff
changeset
|
28 |
import sun.reflect.CallerSensitive; |
2 | 29 |
import sun.reflect.ConstructorAccessor; |
30 |
import sun.reflect.Reflection; |
|
31 |
import sun.reflect.generics.repository.ConstructorRepository; |
|
32 |
import sun.reflect.generics.factory.CoreReflectionFactory; |
|
33 |
import sun.reflect.generics.factory.GenericsFactory; |
|
34 |
import sun.reflect.generics.scope.ConstructorScope; |
|
35 |
import java.lang.annotation.Annotation; |
|
36 |
import java.lang.annotation.AnnotationFormatError; |
|
37 |
||
38 |
/** |
|
39 |
* {@code Constructor} provides information about, and access to, a single |
|
40 |
* constructor for a class. |
|
41 |
* |
|
42 |
* <p>{@code Constructor} permits widening conversions to occur when matching the |
|
43 |
* actual parameters to newInstance() with the underlying |
|
44 |
* constructor's formal parameters, but throws an |
|
45 |
* {@code IllegalArgumentException} if a narrowing conversion would occur. |
|
46 |
* |
|
47 |
* @param <T> the class in which the constructor is declared |
|
48 |
* |
|
49 |
* @see Member |
|
50 |
* @see java.lang.Class |
|
51 |
* @see java.lang.Class#getConstructors() |
|
52 |
* @see java.lang.Class#getConstructor(Class[]) |
|
53 |
* @see java.lang.Class#getDeclaredConstructors() |
|
54 |
* |
|
55 |
* @author Kenneth Russell |
|
56 |
* @author Nakul Saraiya |
|
57 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
58 |
public final class Constructor<T> extends Executable { |
2 | 59 |
private Class<T> clazz; |
60 |
private int slot; |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
61 |
private Class<?>[] parameterTypes; |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
62 |
private Class<?>[] exceptionTypes; |
2 | 63 |
private int modifiers; |
64 |
// Generics and annotations support |
|
65 |
private transient String signature; |
|
66 |
// generic info repository; lazily initialized |
|
67 |
private transient ConstructorRepository genericInfo; |
|
68 |
private byte[] annotations; |
|
69 |
private byte[] parameterAnnotations; |
|
14905
ca1bf7f03ffb
8004699: Add type annotation storage to Constructor, Field and Method
darcy
parents:
14342
diff
changeset
|
70 |
// This is set by the vm at Constructor creation |
ca1bf7f03ffb
8004699: Add type annotation storage to Constructor, Field and Method
darcy
parents:
14342
diff
changeset
|
71 |
private byte[] typeAnnotations; |
2 | 72 |
|
73 |
// Generics infrastructure |
|
74 |
// Accessor for factory |
|
75 |
private GenericsFactory getFactory() { |
|
76 |
// create scope and factory |
|
77 |
return CoreReflectionFactory.make(this, ConstructorScope.make(this)); |
|
78 |
} |
|
79 |
||
80 |
// Accessor for generic info repository |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
81 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
82 |
ConstructorRepository getGenericInfo() { |
2 | 83 |
// lazily initialize repository if necessary |
84 |
if (genericInfo == null) { |
|
85 |
// create and cache generic info repository |
|
86 |
genericInfo = |
|
87 |
ConstructorRepository.make(getSignature(), |
|
88 |
getFactory()); |
|
89 |
} |
|
90 |
return genericInfo; //return cached repository |
|
91 |
} |
|
92 |
||
93 |
private volatile ConstructorAccessor constructorAccessor; |
|
94 |
// For sharing of ConstructorAccessors. This branching structure |
|
95 |
// is currently only two levels deep (i.e., one root Constructor |
|
96 |
// and potentially many Constructor objects pointing to it.) |
|
97 |
private Constructor<T> root; |
|
98 |
||
99 |
/** |
|
100 |
* Package-private constructor used by ReflectAccess to enable |
|
101 |
* instantiation of these objects in Java code from the java.lang |
|
102 |
* package via sun.reflect.LangReflectAccess. |
|
103 |
*/ |
|
104 |
Constructor(Class<T> declaringClass, |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
105 |
Class<?>[] parameterTypes, |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
106 |
Class<?>[] checkedExceptions, |
2 | 107 |
int modifiers, |
108 |
int slot, |
|
109 |
String signature, |
|
110 |
byte[] annotations, |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
111 |
byte[] parameterAnnotations) { |
2 | 112 |
this.clazz = declaringClass; |
113 |
this.parameterTypes = parameterTypes; |
|
114 |
this.exceptionTypes = checkedExceptions; |
|
115 |
this.modifiers = modifiers; |
|
116 |
this.slot = slot; |
|
117 |
this.signature = signature; |
|
118 |
this.annotations = annotations; |
|
119 |
this.parameterAnnotations = parameterAnnotations; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Package-private routine (exposed to java.lang.Class via |
|
124 |
* ReflectAccess) which returns a copy of this Constructor. The copy's |
|
125 |
* "root" field points to this Constructor. |
|
126 |
*/ |
|
127 |
Constructor<T> copy() { |
|
128 |
// This routine enables sharing of ConstructorAccessor objects |
|
129 |
// among Constructor objects which refer to the same underlying |
|
130 |
// method in the VM. (All of this contortion is only necessary |
|
131 |
// because of the "accessibility" bit in AccessibleObject, |
|
132 |
// which implicitly requires that new java.lang.reflect |
|
133 |
// objects be fabricated for each reflective call on Class |
|
134 |
// objects.) |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
6534
diff
changeset
|
135 |
Constructor<T> res = new Constructor<>(clazz, |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
136 |
parameterTypes, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
137 |
exceptionTypes, modifiers, slot, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
138 |
signature, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
139 |
annotations, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
140 |
parameterAnnotations); |
2 | 141 |
res.root = this; |
142 |
// Might as well eagerly propagate this if already present |
|
143 |
res.constructorAccessor = constructorAccessor; |
|
14905
ca1bf7f03ffb
8004699: Add type annotation storage to Constructor, Field and Method
darcy
parents:
14342
diff
changeset
|
144 |
|
ca1bf7f03ffb
8004699: Add type annotation storage to Constructor, Field and Method
darcy
parents:
14342
diff
changeset
|
145 |
res.typeAnnotations = typeAnnotations; |
2 | 146 |
return res; |
147 |
} |
|
148 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
149 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
150 |
boolean hasGenericInformation() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
151 |
return (getSignature() != null); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
152 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
153 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
154 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
155 |
byte[] getAnnotationBytes() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
156 |
return annotations; |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
157 |
} |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
158 |
@Override |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
159 |
byte[] getTypeAnnotationBytes() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
160 |
return typeAnnotations; |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
161 |
} |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
162 |
|
2 | 163 |
/** |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
164 |
* {@inheritDoc} |
2 | 165 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
166 |
@Override |
2 | 167 |
public Class<T> getDeclaringClass() { |
168 |
return clazz; |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Returns the name of this constructor, as a string. This is |
|
6534
ad71f5af4022
6294399: (reflect) Constructor.getName() returns fully qualified name of declaring class
darcy
parents:
5506
diff
changeset
|
173 |
* the binary name of the constructor's declaring class. |
2 | 174 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
175 |
@Override |
2 | 176 |
public String getName() { |
177 |
return getDeclaringClass().getName(); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
181 |
* {@inheritDoc} |
2 | 182 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
183 |
@Override |
2 | 184 |
public int getModifiers() { |
185 |
return modifiers; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
189 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
190 |
* @throws GenericSignatureFormatError {@inheritDoc} |
2 | 191 |
* @since 1.5 |
192 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
193 |
@Override |
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
10342
diff
changeset
|
194 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
2 | 195 |
public TypeVariable<Constructor<T>>[] getTypeParameters() { |
196 |
if (getSignature() != null) { |
|
197 |
return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters(); |
|
198 |
} else |
|
199 |
return (TypeVariable<Constructor<T>>[])new TypeVariable[0]; |
|
200 |
} |
|
201 |
||
202 |
||
203 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
204 |
* {@inheritDoc} |
2 | 205 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
206 |
@Override |
2 | 207 |
public Class<?>[] getParameterTypes() { |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
208 |
return parameterTypes.clone(); |
2 | 209 |
} |
210 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
211 |
/** |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
212 |
* {@inheritDoc} |
15294
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
213 |
*/ |
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
214 |
public int getParameterCount() { return parameterTypes.length; } |
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
215 |
|
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
216 |
/** |
df55735ea03c
8004729: Add java.lang.reflect.Parameter and related changes for parameter reflection
robm
parents:
14910
diff
changeset
|
217 |
* {@inheritDoc} |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
218 |
* @throws GenericSignatureFormatError {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
219 |
* @throws TypeNotPresentException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
220 |
* @throws MalformedParameterizedTypeException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
221 |
* @since 1.5 |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
222 |
*/ |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
223 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
224 |
public Type[] getGenericParameterTypes() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
225 |
return super.getGenericParameterTypes(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
226 |
} |
2 | 227 |
|
228 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
229 |
* {@inheritDoc} |
2 | 230 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
231 |
@Override |
2 | 232 |
public Class<?>[] getExceptionTypes() { |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
233 |
return exceptionTypes.clone(); |
2 | 234 |
} |
235 |
||
236 |
||
237 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
238 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
239 |
* @throws GenericSignatureFormatError {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
240 |
* @throws TypeNotPresentException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
241 |
* @throws MalformedParameterizedTypeException {@inheritDoc} |
2 | 242 |
* @since 1.5 |
243 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
244 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
245 |
public Type[] getGenericExceptionTypes() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
246 |
return super.getGenericExceptionTypes(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
247 |
} |
2 | 248 |
|
249 |
/** |
|
250 |
* Compares this {@code Constructor} against the specified object. |
|
251 |
* Returns true if the objects are the same. Two {@code Constructor} objects are |
|
252 |
* the same if they were declared by the same class and have the |
|
253 |
* same formal parameter types. |
|
254 |
*/ |
|
255 |
public boolean equals(Object obj) { |
|
256 |
if (obj != null && obj instanceof Constructor) { |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
257 |
Constructor<?> other = (Constructor<?>)obj; |
2 | 258 |
if (getDeclaringClass() == other.getDeclaringClass()) { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
259 |
return equalParamTypes(parameterTypes, other.parameterTypes); |
2 | 260 |
} |
261 |
} |
|
262 |
return false; |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Returns a hashcode for this {@code Constructor}. The hashcode is |
|
267 |
* the same as the hashcode for the underlying constructor's |
|
268 |
* declaring class name. |
|
269 |
*/ |
|
270 |
public int hashCode() { |
|
271 |
return getDeclaringClass().getName().hashCode(); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Returns a string describing this {@code Constructor}. The string is |
|
276 |
* formatted as the constructor access modifiers, if any, |
|
277 |
* followed by the fully-qualified name of the declaring class, |
|
278 |
* followed by a parenthesized, comma-separated list of the |
|
279 |
* constructor's formal parameter types. For example: |
|
280 |
* <pre> |
|
281 |
* public java.util.Hashtable(int,float) |
|
282 |
* </pre> |
|
283 |
* |
|
284 |
* <p>The only possible modifiers for constructors are the access |
|
285 |
* modifiers {@code public}, {@code protected} or |
|
286 |
* {@code private}. Only one of these may appear, or none if the |
|
287 |
* constructor has default (package) access. |
|
16729
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
288 |
* |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
289 |
* @return a string describing this {@code Constructor} |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
290 |
* @jls 8.8.3. Constructor Modifiers |
2 | 291 |
*/ |
292 |
public String toString() { |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
293 |
return sharedToString(Modifier.constructorModifiers(), |
16729
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
294 |
false, |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
295 |
parameterTypes, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
296 |
exceptionTypes); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
297 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
298 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
299 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
300 |
void specificToStringHeader(StringBuilder sb) { |
16743
b0b34102bb4c
6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents:
16729
diff
changeset
|
301 |
sb.append(getDeclaringClass().getTypeName()); |
2 | 302 |
} |
303 |
||
304 |
/** |
|
305 |
* Returns a string describing this {@code Constructor}, |
|
306 |
* including type parameters. The string is formatted as the |
|
307 |
* constructor access modifiers, if any, followed by an |
|
308 |
* angle-bracketed comma separated list of the constructor's type |
|
309 |
* parameters, if any, followed by the fully-qualified name of the |
|
310 |
* declaring class, followed by a parenthesized, comma-separated |
|
311 |
* list of the constructor's generic formal parameter types. |
|
312 |
* |
|
313 |
* If this constructor was declared to take a variable number of |
|
314 |
* arguments, instead of denoting the last parameter as |
|
315 |
* "<tt><i>Type</i>[]</tt>", it is denoted as |
|
316 |
* "<tt><i>Type</i>...</tt>". |
|
317 |
* |
|
318 |
* A space is used to separate access modifiers from one another |
|
319 |
* and from the type parameters or return type. If there are no |
|
320 |
* type parameters, the type parameter list is elided; if the type |
|
321 |
* parameter list is present, a space separates the list from the |
|
322 |
* class name. If the constructor is declared to throw |
|
323 |
* exceptions, the parameter list is followed by a space, followed |
|
324 |
* by the word "{@code throws}" followed by a |
|
325 |
* comma-separated list of the thrown exception types. |
|
326 |
* |
|
327 |
* <p>The only possible modifiers for constructors are the access |
|
328 |
* modifiers {@code public}, {@code protected} or |
|
329 |
* {@code private}. Only one of these may appear, or none if the |
|
330 |
* constructor has default (package) access. |
|
331 |
* |
|
332 |
* @return a string describing this {@code Constructor}, |
|
333 |
* include type parameters |
|
334 |
* |
|
335 |
* @since 1.5 |
|
16729
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
336 |
* @jls 8.8.3. Constructor Modifiers |
2 | 337 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
338 |
@Override |
2 | 339 |
public String toGenericString() { |
16729
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
16058
diff
changeset
|
340 |
return sharedToGenericString(Modifier.constructorModifiers(), false); |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
341 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
342 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
343 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
344 |
void specificToGenericStringHeader(StringBuilder sb) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
345 |
specificToStringHeader(sb); |
2 | 346 |
} |
347 |
||
348 |
/** |
|
349 |
* Uses the constructor represented by this {@code Constructor} object to |
|
350 |
* create and initialize a new instance of the constructor's |
|
351 |
* declaring class, with the specified initialization parameters. |
|
352 |
* Individual parameters are automatically unwrapped to match |
|
353 |
* primitive formal parameters, and both primitive and reference |
|
354 |
* parameters are subject to method invocation conversions as necessary. |
|
355 |
* |
|
356 |
* <p>If the number of formal parameters required by the underlying constructor |
|
357 |
* is 0, the supplied {@code initargs} array may be of length 0 or null. |
|
358 |
* |
|
359 |
* <p>If the constructor's declaring class is an inner class in a |
|
360 |
* non-static context, the first argument to the constructor needs |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
9029
diff
changeset
|
361 |
* to be the enclosing instance; see section 15.9.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
9029
diff
changeset
|
362 |
* <cite>The Java™ Language Specification</cite>. |
2 | 363 |
* |
364 |
* <p>If the required access and argument checks succeed and the |
|
365 |
* instantiation will proceed, the constructor's declaring class |
|
366 |
* is initialized if it has not already been initialized. |
|
367 |
* |
|
368 |
* <p>If the constructor completes normally, returns the newly |
|
369 |
* created and initialized instance. |
|
370 |
* |
|
371 |
* @param initargs array of objects to be passed as arguments to |
|
372 |
* the constructor call; values of primitive types are wrapped in |
|
373 |
* a wrapper object of the appropriate type (e.g. a {@code float} |
|
374 |
* in a {@link java.lang.Float Float}) |
|
375 |
* |
|
376 |
* @return a new object created by calling the constructor |
|
377 |
* this object represents |
|
378 |
* |
|
379 |
* @exception IllegalAccessException if this {@code Constructor} object |
|
9022
b2e8758b10fd
6543593: (reflect) Clarify private final field mutability
darcy
parents:
7816
diff
changeset
|
380 |
* is enforcing Java language access control and the underlying |
2 | 381 |
* constructor is inaccessible. |
382 |
* @exception IllegalArgumentException if the number of actual |
|
383 |
* and formal parameters differ; if an unwrapping |
|
384 |
* conversion for primitive arguments fails; or if, |
|
385 |
* after possible unwrapping, a parameter value |
|
386 |
* cannot be converted to the corresponding formal |
|
387 |
* parameter type by a method invocation conversion; if |
|
388 |
* this constructor pertains to an enum type. |
|
389 |
* @exception InstantiationException if the class that declares the |
|
390 |
* underlying constructor represents an abstract class. |
|
391 |
* @exception InvocationTargetException if the underlying constructor |
|
392 |
* throws an exception. |
|
393 |
* @exception ExceptionInInitializerError if the initialization provoked |
|
394 |
* by this method fails. |
|
395 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16743
diff
changeset
|
396 |
@CallerSensitive |
2 | 397 |
public T newInstance(Object ... initargs) |
398 |
throws InstantiationException, IllegalAccessException, |
|
399 |
IllegalArgumentException, InvocationTargetException |
|
400 |
{ |
|
401 |
if (!override) { |
|
402 |
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16743
diff
changeset
|
403 |
Class<?> caller = Reflection.getCallerClass(); |
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
404 |
checkAccess(caller, clazz, null, modifiers); |
2 | 405 |
} |
406 |
} |
|
407 |
if ((clazz.getModifiers() & Modifier.ENUM) != 0) |
|
408 |
throw new IllegalArgumentException("Cannot reflectively create enum objects"); |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
409 |
ConstructorAccessor ca = constructorAccessor; // read volatile |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
410 |
if (ca == null) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
411 |
ca = acquireConstructorAccessor(); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
412 |
} |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
413 |
@SuppressWarnings("unchecked") |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
414 |
T inst = (T) ca.newInstance(initargs); |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
415 |
return inst; |
2 | 416 |
} |
417 |
||
418 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
419 |
* {@inheritDoc} |
2 | 420 |
* @since 1.5 |
421 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
422 |
@Override |
2 | 423 |
public boolean isVarArgs() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
424 |
return super.isVarArgs(); |
2 | 425 |
} |
426 |
||
427 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
428 |
* {@inheritDoc} |
14910 | 429 |
* @jls 13.1 The Form of a Binary |
2 | 430 |
* @since 1.5 |
431 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
432 |
@Override |
2 | 433 |
public boolean isSynthetic() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
434 |
return super.isSynthetic(); |
2 | 435 |
} |
436 |
||
437 |
// NOTE that there is no synchronization used here. It is correct |
|
438 |
// (though not efficient) to generate more than one |
|
439 |
// ConstructorAccessor for a given Constructor. However, avoiding |
|
440 |
// synchronization will probably make the implementation more |
|
441 |
// scalable. |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
442 |
private ConstructorAccessor acquireConstructorAccessor() { |
2 | 443 |
// First check to see if one has been created yet, and take it |
444 |
// if so. |
|
445 |
ConstructorAccessor tmp = null; |
|
446 |
if (root != null) tmp = root.getConstructorAccessor(); |
|
447 |
if (tmp != null) { |
|
448 |
constructorAccessor = tmp; |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
449 |
} else { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
450 |
// Otherwise fabricate one and propagate it up to the root |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
451 |
tmp = reflectionFactory.newConstructorAccessor(this); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
452 |
setConstructorAccessor(tmp); |
2 | 453 |
} |
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
454 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
455 |
return tmp; |
2 | 456 |
} |
457 |
||
458 |
// Returns ConstructorAccessor for this Constructor object, not |
|
459 |
// looking up the chain to the root |
|
460 |
ConstructorAccessor getConstructorAccessor() { |
|
461 |
return constructorAccessor; |
|
462 |
} |
|
463 |
||
464 |
// Sets the ConstructorAccessor for this Constructor object and |
|
465 |
// (recursively) its root |
|
466 |
void setConstructorAccessor(ConstructorAccessor accessor) { |
|
467 |
constructorAccessor = accessor; |
|
468 |
// Propagate up |
|
469 |
if (root != null) { |
|
470 |
root.setConstructorAccessor(accessor); |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
int getSlot() { |
|
475 |
return slot; |
|
476 |
} |
|
477 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
478 |
String getSignature() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
479 |
return signature; |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
480 |
} |
2 | 481 |
|
482 |
byte[] getRawAnnotations() { |
|
483 |
return annotations; |
|
484 |
} |
|
485 |
||
486 |
byte[] getRawParameterAnnotations() { |
|
487 |
return parameterAnnotations; |
|
488 |
} |
|
489 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
490 |
|
2 | 491 |
/** |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
492 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
493 |
* @throws NullPointerException {@inheritDoc} |
2 | 494 |
* @since 1.5 |
495 |
*/ |
|
496 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
497 |
return super.getAnnotation(annotationClass); |
2 | 498 |
} |
499 |
||
500 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
501 |
* {@inheritDoc} |
2 | 502 |
* @since 1.5 |
503 |
*/ |
|
504 |
public Annotation[] getDeclaredAnnotations() { |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
505 |
return super.getDeclaredAnnotations(); |
2 | 506 |
} |
507 |
||
508 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
509 |
* {@inheritDoc} |
2 | 510 |
* @since 1.5 |
511 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
512 |
@Override |
2 | 513 |
public Annotation[][] getParameterAnnotations() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
514 |
return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
515 |
} |
2 | 516 |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
517 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
518 |
void handleParameterNumberMismatch(int resultLength, int numParameters) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
519 |
Class<?> declaringClass = getDeclaringClass(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
520 |
if (declaringClass.isEnum() || |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
521 |
declaringClass.isAnonymousClass() || |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
522 |
declaringClass.isLocalClass() ) |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
523 |
return ; // Can't do reliable parameter counting |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
524 |
else { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
525 |
if (!declaringClass.isMemberClass() || // top-level |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
526 |
// Check for the enclosing instance parameter for |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
527 |
// non-static member classes |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
528 |
(declaringClass.isMemberClass() && |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
529 |
((declaringClass.getModifiers() & Modifier.STATIC) == 0) && |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
530 |
resultLength + 1 != numParameters) ) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
531 |
throw new AnnotationFormatError( |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
532 |
"Parameter annotations don't match number of parameters"); |
2 | 533 |
} |
534 |
} |
|
535 |
} |
|
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
536 |
|
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
537 |
/** |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
538 |
* {@inheritDoc} |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
539 |
* @since 1.8 |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
540 |
*/ |
16058
5b8d8cec6280
8007808: Missing method: Executable.getAnnotatedReturnType()
jfranck
parents:
15510
diff
changeset
|
541 |
@Override |
15510
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
542 |
public AnnotatedType getAnnotatedReturnType() { |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
543 |
return getAnnotatedReturnType0(getDeclaringClass()); |
898d924a7efd
8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
15294
diff
changeset
|
544 |
} |
2 | 545 |
} |