jaroslav@601
|
1 |
/*
|
jaroslav@601
|
2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@601
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@601
|
4 |
*
|
jaroslav@601
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@601
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@601
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@601
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@601
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@601
|
10 |
*
|
jaroslav@601
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@601
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@601
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@601
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@601
|
15 |
* accompanied this code).
|
jaroslav@601
|
16 |
*
|
jaroslav@601
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@601
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@601
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@601
|
20 |
*
|
jaroslav@601
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@601
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@601
|
23 |
* questions.
|
jaroslav@601
|
24 |
*/
|
jaroslav@601
|
25 |
|
jaroslav@601
|
26 |
package java.lang.reflect;
|
jaroslav@601
|
27 |
|
jaroslav@601
|
28 |
import sun.reflect.ConstructorAccessor;
|
jaroslav@601
|
29 |
import sun.reflect.Reflection;
|
jaroslav@601
|
30 |
import sun.reflect.generics.repository.ConstructorRepository;
|
jaroslav@601
|
31 |
import sun.reflect.generics.factory.CoreReflectionFactory;
|
jaroslav@601
|
32 |
import sun.reflect.generics.factory.GenericsFactory;
|
jaroslav@601
|
33 |
import sun.reflect.generics.scope.ConstructorScope;
|
jaroslav@601
|
34 |
import java.lang.annotation.Annotation;
|
jaroslav@601
|
35 |
import java.util.Map;
|
jaroslav@601
|
36 |
import sun.reflect.annotation.AnnotationParser;
|
jaroslav@601
|
37 |
import java.lang.annotation.AnnotationFormatError;
|
jaroslav@601
|
38 |
import java.lang.reflect.Modifier;
|
jaroslav@601
|
39 |
|
jaroslav@601
|
40 |
/**
|
jaroslav@601
|
41 |
* {@code Constructor} provides information about, and access to, a single
|
jaroslav@601
|
42 |
* constructor for a class.
|
jaroslav@601
|
43 |
*
|
jaroslav@601
|
44 |
* <p>{@code Constructor} permits widening conversions to occur when matching the
|
jaroslav@601
|
45 |
* actual parameters to newInstance() with the underlying
|
jaroslav@601
|
46 |
* constructor's formal parameters, but throws an
|
jaroslav@601
|
47 |
* {@code IllegalArgumentException} if a narrowing conversion would occur.
|
jaroslav@601
|
48 |
*
|
jaroslav@601
|
49 |
* @param <T> the class in which the constructor is declared
|
jaroslav@601
|
50 |
*
|
jaroslav@601
|
51 |
* @see Member
|
jaroslav@601
|
52 |
* @see java.lang.Class
|
jaroslav@601
|
53 |
* @see java.lang.Class#getConstructors()
|
jaroslav@601
|
54 |
* @see java.lang.Class#getConstructor(Class[])
|
jaroslav@601
|
55 |
* @see java.lang.Class#getDeclaredConstructors()
|
jaroslav@601
|
56 |
*
|
jaroslav@601
|
57 |
* @author Kenneth Russell
|
jaroslav@601
|
58 |
* @author Nakul Saraiya
|
jaroslav@601
|
59 |
*/
|
jaroslav@601
|
60 |
public final
|
jaroslav@601
|
61 |
class Constructor<T> extends AccessibleObject implements
|
jaroslav@601
|
62 |
GenericDeclaration,
|
jaroslav@601
|
63 |
Member {
|
jaroslav@601
|
64 |
|
jaroslav@601
|
65 |
private Class<T> clazz;
|
jaroslav@601
|
66 |
private int slot;
|
jaroslav@601
|
67 |
private Class<?>[] parameterTypes;
|
jaroslav@601
|
68 |
private Class<?>[] exceptionTypes;
|
jaroslav@601
|
69 |
private int modifiers;
|
jaroslav@601
|
70 |
// Generics and annotations support
|
jaroslav@601
|
71 |
private transient String signature;
|
jaroslav@601
|
72 |
// generic info repository; lazily initialized
|
jaroslav@601
|
73 |
private transient ConstructorRepository genericInfo;
|
jaroslav@601
|
74 |
private byte[] annotations;
|
jaroslav@601
|
75 |
private byte[] parameterAnnotations;
|
jaroslav@601
|
76 |
|
jaroslav@601
|
77 |
// Generics infrastructure
|
jaroslav@601
|
78 |
// Accessor for factory
|
jaroslav@601
|
79 |
private GenericsFactory getFactory() {
|
jaroslav@601
|
80 |
// create scope and factory
|
jaroslav@601
|
81 |
return CoreReflectionFactory.make(this, ConstructorScope.make(this));
|
jaroslav@601
|
82 |
}
|
jaroslav@601
|
83 |
|
jaroslav@601
|
84 |
// Accessor for generic info repository
|
jaroslav@601
|
85 |
private ConstructorRepository getGenericInfo() {
|
jaroslav@601
|
86 |
// lazily initialize repository if necessary
|
jaroslav@601
|
87 |
if (genericInfo == null) {
|
jaroslav@601
|
88 |
// create and cache generic info repository
|
jaroslav@601
|
89 |
genericInfo =
|
jaroslav@601
|
90 |
ConstructorRepository.make(getSignature(),
|
jaroslav@601
|
91 |
getFactory());
|
jaroslav@601
|
92 |
}
|
jaroslav@601
|
93 |
return genericInfo; //return cached repository
|
jaroslav@601
|
94 |
}
|
jaroslav@601
|
95 |
|
jaroslav@601
|
96 |
private volatile ConstructorAccessor constructorAccessor;
|
jaroslav@601
|
97 |
// For sharing of ConstructorAccessors. This branching structure
|
jaroslav@601
|
98 |
// is currently only two levels deep (i.e., one root Constructor
|
jaroslav@601
|
99 |
// and potentially many Constructor objects pointing to it.)
|
jaroslav@601
|
100 |
private Constructor<T> root;
|
jaroslav@601
|
101 |
|
jaroslav@601
|
102 |
/**
|
jaroslav@601
|
103 |
* Package-private constructor used by ReflectAccess to enable
|
jaroslav@601
|
104 |
* instantiation of these objects in Java code from the java.lang
|
jaroslav@601
|
105 |
* package via sun.reflect.LangReflectAccess.
|
jaroslav@601
|
106 |
*/
|
jaroslav@601
|
107 |
Constructor(Class<T> declaringClass,
|
jaroslav@601
|
108 |
Class<?>[] parameterTypes,
|
jaroslav@601
|
109 |
Class<?>[] checkedExceptions,
|
jaroslav@601
|
110 |
int modifiers,
|
jaroslav@601
|
111 |
int slot,
|
jaroslav@601
|
112 |
String signature,
|
jaroslav@601
|
113 |
byte[] annotations,
|
jaroslav@601
|
114 |
byte[] parameterAnnotations)
|
jaroslav@601
|
115 |
{
|
jaroslav@601
|
116 |
this.clazz = declaringClass;
|
jaroslav@601
|
117 |
this.parameterTypes = parameterTypes;
|
jaroslav@601
|
118 |
this.exceptionTypes = checkedExceptions;
|
jaroslav@601
|
119 |
this.modifiers = modifiers;
|
jaroslav@601
|
120 |
this.slot = slot;
|
jaroslav@601
|
121 |
this.signature = signature;
|
jaroslav@601
|
122 |
this.annotations = annotations;
|
jaroslav@601
|
123 |
this.parameterAnnotations = parameterAnnotations;
|
jaroslav@601
|
124 |
}
|
jaroslav@601
|
125 |
|
jaroslav@601
|
126 |
/**
|
jaroslav@601
|
127 |
* Package-private routine (exposed to java.lang.Class via
|
jaroslav@601
|
128 |
* ReflectAccess) which returns a copy of this Constructor. The copy's
|
jaroslav@601
|
129 |
* "root" field points to this Constructor.
|
jaroslav@601
|
130 |
*/
|
jaroslav@601
|
131 |
Constructor<T> copy() {
|
jaroslav@601
|
132 |
// This routine enables sharing of ConstructorAccessor objects
|
jaroslav@601
|
133 |
// among Constructor objects which refer to the same underlying
|
jaroslav@601
|
134 |
// method in the VM. (All of this contortion is only necessary
|
jaroslav@601
|
135 |
// because of the "accessibility" bit in AccessibleObject,
|
jaroslav@601
|
136 |
// which implicitly requires that new java.lang.reflect
|
jaroslav@601
|
137 |
// objects be fabricated for each reflective call on Class
|
jaroslav@601
|
138 |
// objects.)
|
jaroslav@601
|
139 |
Constructor<T> res = new Constructor<>(clazz,
|
jaroslav@601
|
140 |
parameterTypes,
|
jaroslav@601
|
141 |
exceptionTypes, modifiers, slot,
|
jaroslav@601
|
142 |
signature,
|
jaroslav@601
|
143 |
annotations,
|
jaroslav@601
|
144 |
parameterAnnotations);
|
jaroslav@601
|
145 |
res.root = this;
|
jaroslav@601
|
146 |
// Might as well eagerly propagate this if already present
|
jaroslav@601
|
147 |
res.constructorAccessor = constructorAccessor;
|
jaroslav@601
|
148 |
return res;
|
jaroslav@601
|
149 |
}
|
jaroslav@601
|
150 |
|
jaroslav@601
|
151 |
/**
|
jaroslav@601
|
152 |
* Returns the {@code Class} object representing the class that declares
|
jaroslav@601
|
153 |
* the constructor represented by this {@code Constructor} object.
|
jaroslav@601
|
154 |
*/
|
jaroslav@601
|
155 |
public Class<T> getDeclaringClass() {
|
jaroslav@601
|
156 |
return clazz;
|
jaroslav@601
|
157 |
}
|
jaroslav@601
|
158 |
|
jaroslav@601
|
159 |
/**
|
jaroslav@601
|
160 |
* Returns the name of this constructor, as a string. This is
|
jaroslav@601
|
161 |
* the binary name of the constructor's declaring class.
|
jaroslav@601
|
162 |
*/
|
jaroslav@601
|
163 |
public String getName() {
|
jaroslav@601
|
164 |
return getDeclaringClass().getName();
|
jaroslav@601
|
165 |
}
|
jaroslav@601
|
166 |
|
jaroslav@601
|
167 |
/**
|
jaroslav@601
|
168 |
* Returns the Java language modifiers for the constructor
|
jaroslav@601
|
169 |
* represented by this {@code Constructor} object, as an integer. The
|
jaroslav@601
|
170 |
* {@code Modifier} class should be used to decode the modifiers.
|
jaroslav@601
|
171 |
*
|
jaroslav@601
|
172 |
* @see Modifier
|
jaroslav@601
|
173 |
*/
|
jaroslav@601
|
174 |
public int getModifiers() {
|
jaroslav@601
|
175 |
return modifiers;
|
jaroslav@601
|
176 |
}
|
jaroslav@601
|
177 |
|
jaroslav@601
|
178 |
/**
|
jaroslav@601
|
179 |
* Returns an array of {@code TypeVariable} objects that represent the
|
jaroslav@601
|
180 |
* type variables declared by the generic declaration represented by this
|
jaroslav@601
|
181 |
* {@code GenericDeclaration} object, in declaration order. Returns an
|
jaroslav@601
|
182 |
* array of length 0 if the underlying generic declaration declares no type
|
jaroslav@601
|
183 |
* variables.
|
jaroslav@601
|
184 |
*
|
jaroslav@601
|
185 |
* @return an array of {@code TypeVariable} objects that represent
|
jaroslav@601
|
186 |
* the type variables declared by this generic declaration
|
jaroslav@601
|
187 |
* @throws GenericSignatureFormatError if the generic
|
jaroslav@601
|
188 |
* signature of this generic declaration does not conform to
|
jaroslav@601
|
189 |
* the format specified in
|
jaroslav@601
|
190 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jaroslav@601
|
191 |
* @since 1.5
|
jaroslav@601
|
192 |
*/
|
jaroslav@601
|
193 |
public TypeVariable<Constructor<T>>[] getTypeParameters() {
|
jaroslav@601
|
194 |
if (getSignature() != null) {
|
jaroslav@601
|
195 |
return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters();
|
jaroslav@601
|
196 |
} else
|
jaroslav@601
|
197 |
return (TypeVariable<Constructor<T>>[])new TypeVariable[0];
|
jaroslav@601
|
198 |
}
|
jaroslav@601
|
199 |
|
jaroslav@601
|
200 |
|
jaroslav@601
|
201 |
/**
|
jaroslav@601
|
202 |
* Returns an array of {@code Class} objects that represent the formal
|
jaroslav@601
|
203 |
* parameter types, in declaration order, of the constructor
|
jaroslav@601
|
204 |
* represented by this {@code Constructor} object. Returns an array of
|
jaroslav@601
|
205 |
* length 0 if the underlying constructor takes no parameters.
|
jaroslav@601
|
206 |
*
|
jaroslav@601
|
207 |
* @return the parameter types for the constructor this object
|
jaroslav@601
|
208 |
* represents
|
jaroslav@601
|
209 |
*/
|
jaroslav@601
|
210 |
public Class<?>[] getParameterTypes() {
|
jaroslav@601
|
211 |
return (Class<?>[]) parameterTypes.clone();
|
jaroslav@601
|
212 |
}
|
jaroslav@601
|
213 |
|
jaroslav@601
|
214 |
|
jaroslav@601
|
215 |
/**
|
jaroslav@601
|
216 |
* Returns an array of {@code Type} objects that represent the formal
|
jaroslav@601
|
217 |
* parameter types, in declaration order, of the method represented by
|
jaroslav@601
|
218 |
* this {@code Constructor} object. Returns an array of length 0 if the
|
jaroslav@601
|
219 |
* underlying method takes no parameters.
|
jaroslav@601
|
220 |
*
|
jaroslav@601
|
221 |
* <p>If a formal parameter type is a parameterized type,
|
jaroslav@601
|
222 |
* the {@code Type} object returned for it must accurately reflect
|
jaroslav@601
|
223 |
* the actual type parameters used in the source code.
|
jaroslav@601
|
224 |
*
|
jaroslav@601
|
225 |
* <p>If a formal parameter type is a type variable or a parameterized
|
jaroslav@601
|
226 |
* type, it is created. Otherwise, it is resolved.
|
jaroslav@601
|
227 |
*
|
jaroslav@601
|
228 |
* @return an array of {@code Type}s that represent the formal
|
jaroslav@601
|
229 |
* parameter types of the underlying method, in declaration order
|
jaroslav@601
|
230 |
* @throws GenericSignatureFormatError
|
jaroslav@601
|
231 |
* if the generic method signature does not conform to the format
|
jaroslav@601
|
232 |
* specified in
|
jaroslav@601
|
233 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jaroslav@601
|
234 |
* @throws TypeNotPresentException if any of the parameter
|
jaroslav@601
|
235 |
* types of the underlying method refers to a non-existent type
|
jaroslav@601
|
236 |
* declaration
|
jaroslav@601
|
237 |
* @throws MalformedParameterizedTypeException if any of
|
jaroslav@601
|
238 |
* the underlying method's parameter types refer to a parameterized
|
jaroslav@601
|
239 |
* type that cannot be instantiated for any reason
|
jaroslav@601
|
240 |
* @since 1.5
|
jaroslav@601
|
241 |
*/
|
jaroslav@601
|
242 |
public Type[] getGenericParameterTypes() {
|
jaroslav@601
|
243 |
if (getSignature() != null)
|
jaroslav@601
|
244 |
return getGenericInfo().getParameterTypes();
|
jaroslav@601
|
245 |
else
|
jaroslav@601
|
246 |
return getParameterTypes();
|
jaroslav@601
|
247 |
}
|
jaroslav@601
|
248 |
|
jaroslav@601
|
249 |
|
jaroslav@601
|
250 |
/**
|
jaroslav@601
|
251 |
* Returns an array of {@code Class} objects that represent the types
|
jaroslav@601
|
252 |
* of exceptions declared to be thrown by the underlying constructor
|
jaroslav@601
|
253 |
* represented by this {@code Constructor} object. Returns an array of
|
jaroslav@601
|
254 |
* length 0 if the constructor declares no exceptions in its {@code throws} clause.
|
jaroslav@601
|
255 |
*
|
jaroslav@601
|
256 |
* @return the exception types declared as being thrown by the
|
jaroslav@601
|
257 |
* constructor this object represents
|
jaroslav@601
|
258 |
*/
|
jaroslav@601
|
259 |
public Class<?>[] getExceptionTypes() {
|
jaroslav@601
|
260 |
return (Class<?>[])exceptionTypes.clone();
|
jaroslav@601
|
261 |
}
|
jaroslav@601
|
262 |
|
jaroslav@601
|
263 |
|
jaroslav@601
|
264 |
/**
|
jaroslav@601
|
265 |
* Returns an array of {@code Type} objects that represent the
|
jaroslav@601
|
266 |
* exceptions declared to be thrown by this {@code Constructor} object.
|
jaroslav@601
|
267 |
* Returns an array of length 0 if the underlying method declares
|
jaroslav@601
|
268 |
* no exceptions in its {@code throws} clause.
|
jaroslav@601
|
269 |
*
|
jaroslav@601
|
270 |
* <p>If an exception type is a type variable or a parameterized
|
jaroslav@601
|
271 |
* type, it is created. Otherwise, it is resolved.
|
jaroslav@601
|
272 |
*
|
jaroslav@601
|
273 |
* @return an array of Types that represent the exception types
|
jaroslav@601
|
274 |
* thrown by the underlying method
|
jaroslav@601
|
275 |
* @throws GenericSignatureFormatError
|
jaroslav@601
|
276 |
* if the generic method signature does not conform to the format
|
jaroslav@601
|
277 |
* specified in
|
jaroslav@601
|
278 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jaroslav@601
|
279 |
* @throws TypeNotPresentException if the underlying method's
|
jaroslav@601
|
280 |
* {@code throws} clause refers to a non-existent type declaration
|
jaroslav@601
|
281 |
* @throws MalformedParameterizedTypeException if
|
jaroslav@601
|
282 |
* the underlying method's {@code throws} clause refers to a
|
jaroslav@601
|
283 |
* parameterized type that cannot be instantiated for any reason
|
jaroslav@601
|
284 |
* @since 1.5
|
jaroslav@601
|
285 |
*/
|
jaroslav@601
|
286 |
public Type[] getGenericExceptionTypes() {
|
jaroslav@601
|
287 |
Type[] result;
|
jaroslav@601
|
288 |
if (getSignature() != null &&
|
jaroslav@601
|
289 |
( (result = getGenericInfo().getExceptionTypes()).length > 0 ))
|
jaroslav@601
|
290 |
return result;
|
jaroslav@601
|
291 |
else
|
jaroslav@601
|
292 |
return getExceptionTypes();
|
jaroslav@601
|
293 |
}
|
jaroslav@601
|
294 |
|
jaroslav@601
|
295 |
/**
|
jaroslav@601
|
296 |
* Compares this {@code Constructor} against the specified object.
|
jaroslav@601
|
297 |
* Returns true if the objects are the same. Two {@code Constructor} objects are
|
jaroslav@601
|
298 |
* the same if they were declared by the same class and have the
|
jaroslav@601
|
299 |
* same formal parameter types.
|
jaroslav@601
|
300 |
*/
|
jaroslav@601
|
301 |
public boolean equals(Object obj) {
|
jaroslav@601
|
302 |
if (obj != null && obj instanceof Constructor) {
|
jaroslav@601
|
303 |
Constructor<?> other = (Constructor<?>)obj;
|
jaroslav@601
|
304 |
if (getDeclaringClass() == other.getDeclaringClass()) {
|
jaroslav@601
|
305 |
/* Avoid unnecessary cloning */
|
jaroslav@601
|
306 |
Class<?>[] params1 = parameterTypes;
|
jaroslav@601
|
307 |
Class<?>[] params2 = other.parameterTypes;
|
jaroslav@601
|
308 |
if (params1.length == params2.length) {
|
jaroslav@601
|
309 |
for (int i = 0; i < params1.length; i++) {
|
jaroslav@601
|
310 |
if (params1[i] != params2[i])
|
jaroslav@601
|
311 |
return false;
|
jaroslav@601
|
312 |
}
|
jaroslav@601
|
313 |
return true;
|
jaroslav@601
|
314 |
}
|
jaroslav@601
|
315 |
}
|
jaroslav@601
|
316 |
}
|
jaroslav@601
|
317 |
return false;
|
jaroslav@601
|
318 |
}
|
jaroslav@601
|
319 |
|
jaroslav@601
|
320 |
/**
|
jaroslav@601
|
321 |
* Returns a hashcode for this {@code Constructor}. The hashcode is
|
jaroslav@601
|
322 |
* the same as the hashcode for the underlying constructor's
|
jaroslav@601
|
323 |
* declaring class name.
|
jaroslav@601
|
324 |
*/
|
jaroslav@601
|
325 |
public int hashCode() {
|
jaroslav@601
|
326 |
return getDeclaringClass().getName().hashCode();
|
jaroslav@601
|
327 |
}
|
jaroslav@601
|
328 |
|
jaroslav@601
|
329 |
/**
|
jaroslav@601
|
330 |
* Returns a string describing this {@code Constructor}. The string is
|
jaroslav@601
|
331 |
* formatted as the constructor access modifiers, if any,
|
jaroslav@601
|
332 |
* followed by the fully-qualified name of the declaring class,
|
jaroslav@601
|
333 |
* followed by a parenthesized, comma-separated list of the
|
jaroslav@601
|
334 |
* constructor's formal parameter types. For example:
|
jaroslav@601
|
335 |
* <pre>
|
jaroslav@601
|
336 |
* public java.util.Hashtable(int,float)
|
jaroslav@601
|
337 |
* </pre>
|
jaroslav@601
|
338 |
*
|
jaroslav@601
|
339 |
* <p>The only possible modifiers for constructors are the access
|
jaroslav@601
|
340 |
* modifiers {@code public}, {@code protected} or
|
jaroslav@601
|
341 |
* {@code private}. Only one of these may appear, or none if the
|
jaroslav@601
|
342 |
* constructor has default (package) access.
|
jaroslav@601
|
343 |
*/
|
jaroslav@601
|
344 |
public String toString() {
|
jaroslav@601
|
345 |
try {
|
jaroslav@601
|
346 |
StringBuffer sb = new StringBuffer();
|
jaroslav@601
|
347 |
int mod = getModifiers() & Modifier.constructorModifiers();
|
jaroslav@601
|
348 |
if (mod != 0) {
|
jaroslav@601
|
349 |
sb.append(Modifier.toString(mod) + " ");
|
jaroslav@601
|
350 |
}
|
jaroslav@601
|
351 |
sb.append(Field.getTypeName(getDeclaringClass()));
|
jaroslav@601
|
352 |
sb.append("(");
|
jaroslav@601
|
353 |
Class<?>[] params = parameterTypes; // avoid clone
|
jaroslav@601
|
354 |
for (int j = 0; j < params.length; j++) {
|
jaroslav@601
|
355 |
sb.append(Field.getTypeName(params[j]));
|
jaroslav@601
|
356 |
if (j < (params.length - 1))
|
jaroslav@601
|
357 |
sb.append(",");
|
jaroslav@601
|
358 |
}
|
jaroslav@601
|
359 |
sb.append(")");
|
jaroslav@601
|
360 |
Class<?>[] exceptions = exceptionTypes; // avoid clone
|
jaroslav@601
|
361 |
if (exceptions.length > 0) {
|
jaroslav@601
|
362 |
sb.append(" throws ");
|
jaroslav@601
|
363 |
for (int k = 0; k < exceptions.length; k++) {
|
jaroslav@601
|
364 |
sb.append(exceptions[k].getName());
|
jaroslav@601
|
365 |
if (k < (exceptions.length - 1))
|
jaroslav@601
|
366 |
sb.append(",");
|
jaroslav@601
|
367 |
}
|
jaroslav@601
|
368 |
}
|
jaroslav@601
|
369 |
return sb.toString();
|
jaroslav@601
|
370 |
} catch (Exception e) {
|
jaroslav@601
|
371 |
return "<" + e + ">";
|
jaroslav@601
|
372 |
}
|
jaroslav@601
|
373 |
}
|
jaroslav@601
|
374 |
|
jaroslav@601
|
375 |
/**
|
jaroslav@601
|
376 |
* Returns a string describing this {@code Constructor},
|
jaroslav@601
|
377 |
* including type parameters. The string is formatted as the
|
jaroslav@601
|
378 |
* constructor access modifiers, if any, followed by an
|
jaroslav@601
|
379 |
* angle-bracketed comma separated list of the constructor's type
|
jaroslav@601
|
380 |
* parameters, if any, followed by the fully-qualified name of the
|
jaroslav@601
|
381 |
* declaring class, followed by a parenthesized, comma-separated
|
jaroslav@601
|
382 |
* list of the constructor's generic formal parameter types.
|
jaroslav@601
|
383 |
*
|
jaroslav@601
|
384 |
* If this constructor was declared to take a variable number of
|
jaroslav@601
|
385 |
* arguments, instead of denoting the last parameter as
|
jaroslav@601
|
386 |
* "<tt><i>Type</i>[]</tt>", it is denoted as
|
jaroslav@601
|
387 |
* "<tt><i>Type</i>...</tt>".
|
jaroslav@601
|
388 |
*
|
jaroslav@601
|
389 |
* A space is used to separate access modifiers from one another
|
jaroslav@601
|
390 |
* and from the type parameters or return type. If there are no
|
jaroslav@601
|
391 |
* type parameters, the type parameter list is elided; if the type
|
jaroslav@601
|
392 |
* parameter list is present, a space separates the list from the
|
jaroslav@601
|
393 |
* class name. If the constructor is declared to throw
|
jaroslav@601
|
394 |
* exceptions, the parameter list is followed by a space, followed
|
jaroslav@601
|
395 |
* by the word "{@code throws}" followed by a
|
jaroslav@601
|
396 |
* comma-separated list of the thrown exception types.
|
jaroslav@601
|
397 |
*
|
jaroslav@601
|
398 |
* <p>The only possible modifiers for constructors are the access
|
jaroslav@601
|
399 |
* modifiers {@code public}, {@code protected} or
|
jaroslav@601
|
400 |
* {@code private}. Only one of these may appear, or none if the
|
jaroslav@601
|
401 |
* constructor has default (package) access.
|
jaroslav@601
|
402 |
*
|
jaroslav@601
|
403 |
* @return a string describing this {@code Constructor},
|
jaroslav@601
|
404 |
* include type parameters
|
jaroslav@601
|
405 |
*
|
jaroslav@601
|
406 |
* @since 1.5
|
jaroslav@601
|
407 |
*/
|
jaroslav@601
|
408 |
public String toGenericString() {
|
jaroslav@601
|
409 |
try {
|
jaroslav@601
|
410 |
StringBuilder sb = new StringBuilder();
|
jaroslav@601
|
411 |
int mod = getModifiers() & Modifier.constructorModifiers();
|
jaroslav@601
|
412 |
if (mod != 0) {
|
jaroslav@601
|
413 |
sb.append(Modifier.toString(mod) + " ");
|
jaroslav@601
|
414 |
}
|
jaroslav@601
|
415 |
TypeVariable<?>[] typeparms = getTypeParameters();
|
jaroslav@601
|
416 |
if (typeparms.length > 0) {
|
jaroslav@601
|
417 |
boolean first = true;
|
jaroslav@601
|
418 |
sb.append("<");
|
jaroslav@601
|
419 |
for(TypeVariable<?> typeparm: typeparms) {
|
jaroslav@601
|
420 |
if (!first)
|
jaroslav@601
|
421 |
sb.append(",");
|
jaroslav@601
|
422 |
// Class objects can't occur here; no need to test
|
jaroslav@601
|
423 |
// and call Class.getName().
|
jaroslav@601
|
424 |
sb.append(typeparm.toString());
|
jaroslav@601
|
425 |
first = false;
|
jaroslav@601
|
426 |
}
|
jaroslav@601
|
427 |
sb.append("> ");
|
jaroslav@601
|
428 |
}
|
jaroslav@601
|
429 |
sb.append(Field.getTypeName(getDeclaringClass()));
|
jaroslav@601
|
430 |
sb.append("(");
|
jaroslav@601
|
431 |
Type[] params = getGenericParameterTypes();
|
jaroslav@601
|
432 |
for (int j = 0; j < params.length; j++) {
|
jaroslav@601
|
433 |
String param = (params[j] instanceof Class<?>)?
|
jaroslav@601
|
434 |
Field.getTypeName((Class<?>)params[j]):
|
jaroslav@601
|
435 |
(params[j].toString());
|
jaroslav@601
|
436 |
if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
|
jaroslav@601
|
437 |
param = param.replaceFirst("\\[\\]$", "...");
|
jaroslav@601
|
438 |
sb.append(param);
|
jaroslav@601
|
439 |
if (j < (params.length - 1))
|
jaroslav@601
|
440 |
sb.append(",");
|
jaroslav@601
|
441 |
}
|
jaroslav@601
|
442 |
sb.append(")");
|
jaroslav@601
|
443 |
Type[] exceptions = getGenericExceptionTypes();
|
jaroslav@601
|
444 |
if (exceptions.length > 0) {
|
jaroslav@601
|
445 |
sb.append(" throws ");
|
jaroslav@601
|
446 |
for (int k = 0; k < exceptions.length; k++) {
|
jaroslav@601
|
447 |
sb.append((exceptions[k] instanceof Class)?
|
jaroslav@601
|
448 |
((Class<?>)exceptions[k]).getName():
|
jaroslav@601
|
449 |
exceptions[k].toString());
|
jaroslav@601
|
450 |
if (k < (exceptions.length - 1))
|
jaroslav@601
|
451 |
sb.append(",");
|
jaroslav@601
|
452 |
}
|
jaroslav@601
|
453 |
}
|
jaroslav@601
|
454 |
return sb.toString();
|
jaroslav@601
|
455 |
} catch (Exception e) {
|
jaroslav@601
|
456 |
return "<" + e + ">";
|
jaroslav@601
|
457 |
}
|
jaroslav@601
|
458 |
}
|
jaroslav@601
|
459 |
|
jaroslav@601
|
460 |
/**
|
jaroslav@601
|
461 |
* Uses the constructor represented by this {@code Constructor} object to
|
jaroslav@601
|
462 |
* create and initialize a new instance of the constructor's
|
jaroslav@601
|
463 |
* declaring class, with the specified initialization parameters.
|
jaroslav@601
|
464 |
* Individual parameters are automatically unwrapped to match
|
jaroslav@601
|
465 |
* primitive formal parameters, and both primitive and reference
|
jaroslav@601
|
466 |
* parameters are subject to method invocation conversions as necessary.
|
jaroslav@601
|
467 |
*
|
jaroslav@601
|
468 |
* <p>If the number of formal parameters required by the underlying constructor
|
jaroslav@601
|
469 |
* is 0, the supplied {@code initargs} array may be of length 0 or null.
|
jaroslav@601
|
470 |
*
|
jaroslav@601
|
471 |
* <p>If the constructor's declaring class is an inner class in a
|
jaroslav@601
|
472 |
* non-static context, the first argument to the constructor needs
|
jaroslav@601
|
473 |
* to be the enclosing instance; see section 15.9.3 of
|
jaroslav@601
|
474 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@601
|
475 |
*
|
jaroslav@601
|
476 |
* <p>If the required access and argument checks succeed and the
|
jaroslav@601
|
477 |
* instantiation will proceed, the constructor's declaring class
|
jaroslav@601
|
478 |
* is initialized if it has not already been initialized.
|
jaroslav@601
|
479 |
*
|
jaroslav@601
|
480 |
* <p>If the constructor completes normally, returns the newly
|
jaroslav@601
|
481 |
* created and initialized instance.
|
jaroslav@601
|
482 |
*
|
jaroslav@601
|
483 |
* @param initargs array of objects to be passed as arguments to
|
jaroslav@601
|
484 |
* the constructor call; values of primitive types are wrapped in
|
jaroslav@601
|
485 |
* a wrapper object of the appropriate type (e.g. a {@code float}
|
jaroslav@601
|
486 |
* in a {@link java.lang.Float Float})
|
jaroslav@601
|
487 |
*
|
jaroslav@601
|
488 |
* @return a new object created by calling the constructor
|
jaroslav@601
|
489 |
* this object represents
|
jaroslav@601
|
490 |
*
|
jaroslav@601
|
491 |
* @exception IllegalAccessException if this {@code Constructor} object
|
jaroslav@601
|
492 |
* is enforcing Java language access control and the underlying
|
jaroslav@601
|
493 |
* constructor is inaccessible.
|
jaroslav@601
|
494 |
* @exception IllegalArgumentException if the number of actual
|
jaroslav@601
|
495 |
* and formal parameters differ; if an unwrapping
|
jaroslav@601
|
496 |
* conversion for primitive arguments fails; or if,
|
jaroslav@601
|
497 |
* after possible unwrapping, a parameter value
|
jaroslav@601
|
498 |
* cannot be converted to the corresponding formal
|
jaroslav@601
|
499 |
* parameter type by a method invocation conversion; if
|
jaroslav@601
|
500 |
* this constructor pertains to an enum type.
|
jaroslav@601
|
501 |
* @exception InstantiationException if the class that declares the
|
jaroslav@601
|
502 |
* underlying constructor represents an abstract class.
|
jaroslav@601
|
503 |
* @exception InvocationTargetException if the underlying constructor
|
jaroslav@601
|
504 |
* throws an exception.
|
jaroslav@601
|
505 |
* @exception ExceptionInInitializerError if the initialization provoked
|
jaroslav@601
|
506 |
* by this method fails.
|
jaroslav@601
|
507 |
*/
|
jaroslav@601
|
508 |
public T newInstance(Object ... initargs)
|
jaroslav@601
|
509 |
throws InstantiationException, IllegalAccessException,
|
jaroslav@601
|
510 |
IllegalArgumentException, InvocationTargetException
|
jaroslav@601
|
511 |
{
|
jaroslav@601
|
512 |
if (!override) {
|
jaroslav@601
|
513 |
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
|
jaroslav@601
|
514 |
Class<?> caller = Reflection.getCallerClass(2);
|
jaroslav@601
|
515 |
|
jaroslav@601
|
516 |
checkAccess(caller, clazz, null, modifiers);
|
jaroslav@601
|
517 |
}
|
jaroslav@601
|
518 |
}
|
jaroslav@601
|
519 |
if ((clazz.getModifiers() & Modifier.ENUM) != 0)
|
jaroslav@601
|
520 |
throw new IllegalArgumentException("Cannot reflectively create enum objects");
|
jaroslav@601
|
521 |
ConstructorAccessor ca = constructorAccessor; // read volatile
|
jaroslav@601
|
522 |
if (ca == null) {
|
jaroslav@601
|
523 |
ca = acquireConstructorAccessor();
|
jaroslav@601
|
524 |
}
|
jaroslav@601
|
525 |
return (T) ca.newInstance(initargs);
|
jaroslav@601
|
526 |
}
|
jaroslav@601
|
527 |
|
jaroslav@601
|
528 |
/**
|
jaroslav@601
|
529 |
* Returns {@code true} if this constructor was declared to take
|
jaroslav@601
|
530 |
* a variable number of arguments; returns {@code false}
|
jaroslav@601
|
531 |
* otherwise.
|
jaroslav@601
|
532 |
*
|
jaroslav@601
|
533 |
* @return {@code true} if an only if this constructor was declared to
|
jaroslav@601
|
534 |
* take a variable number of arguments.
|
jaroslav@601
|
535 |
* @since 1.5
|
jaroslav@601
|
536 |
*/
|
jaroslav@601
|
537 |
public boolean isVarArgs() {
|
jaroslav@601
|
538 |
return (getModifiers() & Modifier.VARARGS) != 0;
|
jaroslav@601
|
539 |
}
|
jaroslav@601
|
540 |
|
jaroslav@601
|
541 |
/**
|
jaroslav@601
|
542 |
* Returns {@code true} if this constructor is a synthetic
|
jaroslav@601
|
543 |
* constructor; returns {@code false} otherwise.
|
jaroslav@601
|
544 |
*
|
jaroslav@601
|
545 |
* @return true if and only if this constructor is a synthetic
|
jaroslav@601
|
546 |
* constructor as defined by
|
jaroslav@601
|
547 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@601
|
548 |
* @since 1.5
|
jaroslav@601
|
549 |
*/
|
jaroslav@601
|
550 |
public boolean isSynthetic() {
|
jaroslav@601
|
551 |
return Modifier.isSynthetic(getModifiers());
|
jaroslav@601
|
552 |
}
|
jaroslav@601
|
553 |
|
jaroslav@601
|
554 |
// NOTE that there is no synchronization used here. It is correct
|
jaroslav@601
|
555 |
// (though not efficient) to generate more than one
|
jaroslav@601
|
556 |
// ConstructorAccessor for a given Constructor. However, avoiding
|
jaroslav@601
|
557 |
// synchronization will probably make the implementation more
|
jaroslav@601
|
558 |
// scalable.
|
jaroslav@601
|
559 |
private ConstructorAccessor acquireConstructorAccessor() {
|
jaroslav@601
|
560 |
// First check to see if one has been created yet, and take it
|
jaroslav@601
|
561 |
// if so.
|
jaroslav@601
|
562 |
ConstructorAccessor tmp = null;
|
jaroslav@601
|
563 |
if (root != null) tmp = root.getConstructorAccessor();
|
jaroslav@601
|
564 |
if (tmp != null) {
|
jaroslav@601
|
565 |
constructorAccessor = tmp;
|
jaroslav@601
|
566 |
} else {
|
jaroslav@601
|
567 |
// Otherwise fabricate one and propagate it up to the root
|
jaroslav@601
|
568 |
tmp = reflectionFactory.newConstructorAccessor(this);
|
jaroslav@601
|
569 |
setConstructorAccessor(tmp);
|
jaroslav@601
|
570 |
}
|
jaroslav@601
|
571 |
|
jaroslav@601
|
572 |
return tmp;
|
jaroslav@601
|
573 |
}
|
jaroslav@601
|
574 |
|
jaroslav@601
|
575 |
// Returns ConstructorAccessor for this Constructor object, not
|
jaroslav@601
|
576 |
// looking up the chain to the root
|
jaroslav@601
|
577 |
ConstructorAccessor getConstructorAccessor() {
|
jaroslav@601
|
578 |
return constructorAccessor;
|
jaroslav@601
|
579 |
}
|
jaroslav@601
|
580 |
|
jaroslav@601
|
581 |
// Sets the ConstructorAccessor for this Constructor object and
|
jaroslav@601
|
582 |
// (recursively) its root
|
jaroslav@601
|
583 |
void setConstructorAccessor(ConstructorAccessor accessor) {
|
jaroslav@601
|
584 |
constructorAccessor = accessor;
|
jaroslav@601
|
585 |
// Propagate up
|
jaroslav@601
|
586 |
if (root != null) {
|
jaroslav@601
|
587 |
root.setConstructorAccessor(accessor);
|
jaroslav@601
|
588 |
}
|
jaroslav@601
|
589 |
}
|
jaroslav@601
|
590 |
|
jaroslav@601
|
591 |
int getSlot() {
|
jaroslav@601
|
592 |
return slot;
|
jaroslav@601
|
593 |
}
|
jaroslav@601
|
594 |
|
jaroslav@601
|
595 |
String getSignature() {
|
jaroslav@601
|
596 |
return signature;
|
jaroslav@601
|
597 |
}
|
jaroslav@601
|
598 |
|
jaroslav@601
|
599 |
byte[] getRawAnnotations() {
|
jaroslav@601
|
600 |
return annotations;
|
jaroslav@601
|
601 |
}
|
jaroslav@601
|
602 |
|
jaroslav@601
|
603 |
byte[] getRawParameterAnnotations() {
|
jaroslav@601
|
604 |
return parameterAnnotations;
|
jaroslav@601
|
605 |
}
|
jaroslav@601
|
606 |
|
jaroslav@601
|
607 |
/**
|
jaroslav@601
|
608 |
* @throws NullPointerException {@inheritDoc}
|
jaroslav@601
|
609 |
* @since 1.5
|
jaroslav@601
|
610 |
*/
|
jaroslav@601
|
611 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
|
jaroslav@601
|
612 |
if (annotationClass == null)
|
jaroslav@601
|
613 |
throw new NullPointerException();
|
jaroslav@601
|
614 |
|
jaroslav@601
|
615 |
return (T) declaredAnnotations().get(annotationClass);
|
jaroslav@601
|
616 |
}
|
jaroslav@601
|
617 |
|
jaroslav@601
|
618 |
/**
|
jaroslav@601
|
619 |
* @since 1.5
|
jaroslav@601
|
620 |
*/
|
jaroslav@601
|
621 |
public Annotation[] getDeclaredAnnotations() {
|
jaroslav@601
|
622 |
return AnnotationParser.toArray(declaredAnnotations());
|
jaroslav@601
|
623 |
}
|
jaroslav@601
|
624 |
|
jaroslav@601
|
625 |
private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
|
jaroslav@601
|
626 |
|
jaroslav@601
|
627 |
private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
|
jaroslav@601
|
628 |
if (declaredAnnotations == null) {
|
jaroslav@601
|
629 |
declaredAnnotations = AnnotationParser.parseAnnotations(
|
jaroslav@601
|
630 |
annotations, sun.misc.SharedSecrets.getJavaLangAccess().
|
jaroslav@601
|
631 |
getConstantPool(getDeclaringClass()),
|
jaroslav@601
|
632 |
getDeclaringClass());
|
jaroslav@601
|
633 |
}
|
jaroslav@601
|
634 |
return declaredAnnotations;
|
jaroslav@601
|
635 |
}
|
jaroslav@601
|
636 |
|
jaroslav@601
|
637 |
/**
|
jaroslav@601
|
638 |
* Returns an array of arrays that represent the annotations on the formal
|
jaroslav@601
|
639 |
* parameters, in declaration order, of the method represented by
|
jaroslav@601
|
640 |
* this {@code Constructor} object. (Returns an array of length zero if the
|
jaroslav@601
|
641 |
* underlying method is parameterless. If the method has one or more
|
jaroslav@601
|
642 |
* parameters, a nested array of length zero is returned for each parameter
|
jaroslav@601
|
643 |
* with no annotations.) The annotation objects contained in the returned
|
jaroslav@601
|
644 |
* arrays are serializable. The caller of this method is free to modify
|
jaroslav@601
|
645 |
* the returned arrays; it will have no effect on the arrays returned to
|
jaroslav@601
|
646 |
* other callers.
|
jaroslav@601
|
647 |
*
|
jaroslav@601
|
648 |
* @return an array of arrays that represent the annotations on the formal
|
jaroslav@601
|
649 |
* parameters, in declaration order, of the method represented by this
|
jaroslav@601
|
650 |
* Constructor object
|
jaroslav@601
|
651 |
* @since 1.5
|
jaroslav@601
|
652 |
*/
|
jaroslav@601
|
653 |
public Annotation[][] getParameterAnnotations() {
|
jaroslav@601
|
654 |
int numParameters = parameterTypes.length;
|
jaroslav@601
|
655 |
if (parameterAnnotations == null)
|
jaroslav@601
|
656 |
return new Annotation[numParameters][0];
|
jaroslav@601
|
657 |
|
jaroslav@601
|
658 |
Annotation[][] result = AnnotationParser.parseParameterAnnotations(
|
jaroslav@601
|
659 |
parameterAnnotations,
|
jaroslav@601
|
660 |
sun.misc.SharedSecrets.getJavaLangAccess().
|
jaroslav@601
|
661 |
getConstantPool(getDeclaringClass()),
|
jaroslav@601
|
662 |
getDeclaringClass());
|
jaroslav@601
|
663 |
if (result.length != numParameters) {
|
jaroslav@601
|
664 |
Class<?> declaringClass = getDeclaringClass();
|
jaroslav@601
|
665 |
if (declaringClass.isEnum() ||
|
jaroslav@601
|
666 |
declaringClass.isAnonymousClass() ||
|
jaroslav@601
|
667 |
declaringClass.isLocalClass() )
|
jaroslav@601
|
668 |
; // Can't do reliable parameter counting
|
jaroslav@601
|
669 |
else {
|
jaroslav@601
|
670 |
if (!declaringClass.isMemberClass() || // top-level
|
jaroslav@601
|
671 |
// Check for the enclosing instance parameter for
|
jaroslav@601
|
672 |
// non-static member classes
|
jaroslav@601
|
673 |
(declaringClass.isMemberClass() &&
|
jaroslav@601
|
674 |
((declaringClass.getModifiers() & Modifier.STATIC) == 0) &&
|
jaroslav@601
|
675 |
result.length + 1 != numParameters) ) {
|
jaroslav@601
|
676 |
throw new AnnotationFormatError(
|
jaroslav@601
|
677 |
"Parameter annotations don't match number of parameters");
|
jaroslav@601
|
678 |
}
|
jaroslav@601
|
679 |
}
|
jaroslav@601
|
680 |
}
|
jaroslav@601
|
681 |
return result;
|
jaroslav@601
|
682 |
}
|
jaroslav@601
|
683 |
}
|