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