author | sherman |
Tue, 30 Aug 2011 11:53:11 -0700 | |
changeset 10419 | 12c063b39232 |
parent 10342 | ca0984bc9d32 |
child 13795 | 73850c397272 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
2 |
* Copyright (c) 1996, 2011, 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.MethodAccessor; |
|
29 |
import sun.reflect.Reflection; |
|
30 |
import sun.reflect.generics.repository.MethodRepository; |
|
31 |
import sun.reflect.generics.factory.CoreReflectionFactory; |
|
32 |
import sun.reflect.generics.factory.GenericsFactory; |
|
33 |
import sun.reflect.generics.scope.MethodScope; |
|
34 |
import sun.reflect.annotation.AnnotationType; |
|
35 |
import sun.reflect.annotation.AnnotationParser; |
|
36 |
import java.lang.annotation.Annotation; |
|
37 |
import java.lang.annotation.AnnotationFormatError; |
|
38 |
import java.nio.ByteBuffer; |
|
39 |
||
40 |
/** |
|
41 |
* A {@code Method} provides information about, and access to, a single method |
|
42 |
* on a class or interface. The reflected method may be a class method |
|
43 |
* or an instance method (including an abstract method). |
|
44 |
* |
|
45 |
* <p>A {@code Method} permits widening conversions to occur when matching the |
|
46 |
* actual parameters to invoke with the underlying method's formal |
|
47 |
* parameters, but it throws an {@code IllegalArgumentException} if a |
|
48 |
* narrowing conversion would occur. |
|
49 |
* |
|
50 |
* @see Member |
|
51 |
* @see java.lang.Class |
|
52 |
* @see java.lang.Class#getMethods() |
|
53 |
* @see java.lang.Class#getMethod(String, Class[]) |
|
54 |
* @see java.lang.Class#getDeclaredMethods() |
|
55 |
* @see java.lang.Class#getDeclaredMethod(String, Class[]) |
|
56 |
* |
|
57 |
* @author Kenneth Russell |
|
58 |
* @author Nakul Saraiya |
|
59 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
60 |
public final class Method extends Executable { |
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
61 |
private Class<?> clazz; |
2 | 62 |
private int slot; |
63 |
// This is guaranteed to be interned by the VM in the 1.4 |
|
64 |
// reflection implementation |
|
65 |
private String name; |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
66 |
private Class<?> returnType; |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
67 |
private Class<?>[] parameterTypes; |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
68 |
private Class<?>[] exceptionTypes; |
2 | 69 |
private int modifiers; |
70 |
// Generics and annotations support |
|
71 |
private transient String signature; |
|
72 |
// generic info repository; lazily initialized |
|
73 |
private transient MethodRepository genericInfo; |
|
74 |
private byte[] annotations; |
|
75 |
private byte[] parameterAnnotations; |
|
76 |
private byte[] annotationDefault; |
|
77 |
private volatile MethodAccessor methodAccessor; |
|
78 |
// For sharing of MethodAccessors. This branching structure is |
|
79 |
// currently only two levels deep (i.e., one root Method and |
|
80 |
// potentially many Method objects pointing to it.) |
|
81 |
private Method root; |
|
82 |
||
83 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
84 |
// Generics infrastructure |
2 | 85 |
private String getGenericSignature() {return signature;} |
86 |
||
87 |
// Accessor for factory |
|
88 |
private GenericsFactory getFactory() { |
|
89 |
// create scope and factory |
|
90 |
return CoreReflectionFactory.make(this, MethodScope.make(this)); |
|
91 |
} |
|
92 |
||
93 |
// Accessor for generic info repository |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
94 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
95 |
MethodRepository getGenericInfo() { |
2 | 96 |
// lazily initialize repository if necessary |
97 |
if (genericInfo == null) { |
|
98 |
// create and cache generic info repository |
|
99 |
genericInfo = MethodRepository.make(getGenericSignature(), |
|
100 |
getFactory()); |
|
101 |
} |
|
102 |
return genericInfo; //return cached repository |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Package-private constructor used by ReflectAccess to enable |
|
107 |
* instantiation of these objects in Java code from the java.lang |
|
108 |
* package via sun.reflect.LangReflectAccess. |
|
109 |
*/ |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
110 |
Method(Class<?> declaringClass, |
2 | 111 |
String name, |
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
112 |
Class<?>[] parameterTypes, |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
113 |
Class<?> returnType, |
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
114 |
Class<?>[] checkedExceptions, |
2 | 115 |
int modifiers, |
116 |
int slot, |
|
117 |
String signature, |
|
118 |
byte[] annotations, |
|
119 |
byte[] parameterAnnotations, |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
120 |
byte[] annotationDefault) { |
2 | 121 |
this.clazz = declaringClass; |
122 |
this.name = name; |
|
123 |
this.parameterTypes = parameterTypes; |
|
124 |
this.returnType = returnType; |
|
125 |
this.exceptionTypes = checkedExceptions; |
|
126 |
this.modifiers = modifiers; |
|
127 |
this.slot = slot; |
|
128 |
this.signature = signature; |
|
129 |
this.annotations = annotations; |
|
130 |
this.parameterAnnotations = parameterAnnotations; |
|
131 |
this.annotationDefault = annotationDefault; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Package-private routine (exposed to java.lang.Class via |
|
136 |
* ReflectAccess) which returns a copy of this Method. The copy's |
|
137 |
* "root" field points to this Method. |
|
138 |
*/ |
|
139 |
Method copy() { |
|
140 |
// This routine enables sharing of MethodAccessor objects |
|
141 |
// among Method objects which refer to the same underlying |
|
142 |
// method in the VM. (All of this contortion is only necessary |
|
143 |
// because of the "accessibility" bit in AccessibleObject, |
|
144 |
// which implicitly requires that new java.lang.reflect |
|
145 |
// objects be fabricated for each reflective call on Class |
|
146 |
// objects.) |
|
147 |
Method res = new Method(clazz, name, parameterTypes, returnType, |
|
148 |
exceptionTypes, modifiers, slot, signature, |
|
149 |
annotations, parameterAnnotations, annotationDefault); |
|
150 |
res.root = this; |
|
151 |
// Might as well eagerly propagate this if already present |
|
152 |
res.methodAccessor = methodAccessor; |
|
153 |
return res; |
|
154 |
} |
|
155 |
||
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
156 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
157 |
boolean hasGenericInformation() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
158 |
return (getGenericSignature() != null); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
159 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
160 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
161 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
162 |
byte[] getAnnotationBytes() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
163 |
return annotations; |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
164 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
165 |
|
2 | 166 |
/** |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
167 |
* {@inheritDoc} |
2 | 168 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
169 |
@Override |
2 | 170 |
public Class<?> getDeclaringClass() { |
171 |
return clazz; |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Returns the name of the method represented by this {@code Method} |
|
176 |
* object, as a {@code String}. |
|
177 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
178 |
@Override |
2 | 179 |
public String getName() { |
180 |
return name; |
|
181 |
} |
|
182 |
||
183 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
184 |
* {@inheritDoc} |
2 | 185 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
186 |
@Override |
2 | 187 |
public int getModifiers() { |
188 |
return modifiers; |
|
189 |
} |
|
190 |
||
191 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
192 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
193 |
* @throws GenericSignatureFormatError {@inheritDoc} |
2 | 194 |
* @since 1.5 |
195 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
196 |
@Override |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
197 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
2 | 198 |
public TypeVariable<Method>[] getTypeParameters() { |
199 |
if (getGenericSignature() != null) |
|
200 |
return (TypeVariable<Method>[])getGenericInfo().getTypeParameters(); |
|
201 |
else |
|
202 |
return (TypeVariable<Method>[])new TypeVariable[0]; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Returns a {@code Class} object that represents the formal return type |
|
207 |
* of the method represented by this {@code Method} object. |
|
208 |
* |
|
209 |
* @return the return type for the method this object represents |
|
210 |
*/ |
|
211 |
public Class<?> getReturnType() { |
|
212 |
return returnType; |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Returns a {@code Type} object that represents the formal return |
|
217 |
* type of the method represented by this {@code Method} object. |
|
218 |
* |
|
219 |
* <p>If the return type is a parameterized type, |
|
220 |
* the {@code Type} object returned must accurately reflect |
|
221 |
* the actual type parameters used in the source code. |
|
222 |
* |
|
223 |
* <p>If the return type is a type variable or a parameterized type, it |
|
224 |
* is created. Otherwise, it is resolved. |
|
225 |
* |
|
226 |
* @return a {@code Type} object that represents the formal return |
|
227 |
* type of the underlying method |
|
228 |
* @throws GenericSignatureFormatError |
|
229 |
* if the generic method signature does not conform to the format |
|
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
|
230 |
* specified in |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
9029
diff
changeset
|
231 |
* <cite>The Java™ Virtual Machine Specification</cite> |
2 | 232 |
* @throws TypeNotPresentException if the underlying method's |
233 |
* return type refers to a non-existent type declaration |
|
234 |
* @throws MalformedParameterizedTypeException if the |
|
235 |
* underlying method's return typed refers to a parameterized |
|
236 |
* type that cannot be instantiated for any reason |
|
237 |
* @since 1.5 |
|
238 |
*/ |
|
239 |
public Type getGenericReturnType() { |
|
240 |
if (getGenericSignature() != null) { |
|
241 |
return getGenericInfo().getReturnType(); |
|
242 |
} else { return getReturnType();} |
|
243 |
} |
|
244 |
||
245 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
246 |
* {@inheritDoc} |
2 | 247 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
248 |
@Override |
2 | 249 |
public Class<?>[] getParameterTypes() { |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
250 |
return parameterTypes.clone(); |
2 | 251 |
} |
252 |
||
253 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
254 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
255 |
* @throws GenericSignatureFormatError {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
256 |
* @throws TypeNotPresentException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
257 |
* @throws MalformedParameterizedTypeException {@inheritDoc} |
2 | 258 |
* @since 1.5 |
259 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
260 |
@Override |
2 | 261 |
public Type[] getGenericParameterTypes() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
262 |
return super.getGenericParameterTypes(); |
2 | 263 |
} |
264 |
||
265 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
266 |
* {@inheritDoc} |
2 | 267 |
*/ |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
268 |
@Override |
2 | 269 |
public Class<?>[] getExceptionTypes() { |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
10127
diff
changeset
|
270 |
return exceptionTypes.clone(); |
2 | 271 |
} |
272 |
||
273 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
274 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
275 |
* @throws GenericSignatureFormatError {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
276 |
* @throws TypeNotPresentException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
277 |
* @throws MalformedParameterizedTypeException {@inheritDoc} |
2 | 278 |
* @since 1.5 |
279 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
280 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
281 |
public Type[] getGenericExceptionTypes() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
282 |
return super.getGenericExceptionTypes(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
283 |
} |
2 | 284 |
|
285 |
/** |
|
286 |
* Compares this {@code Method} against the specified object. Returns |
|
287 |
* true if the objects are the same. Two {@code Methods} are the same if |
|
288 |
* they were declared by the same class and have the same name |
|
289 |
* and formal parameter types and return type. |
|
290 |
*/ |
|
291 |
public boolean equals(Object obj) { |
|
292 |
if (obj != null && obj instanceof Method) { |
|
293 |
Method other = (Method)obj; |
|
294 |
if ((getDeclaringClass() == other.getDeclaringClass()) |
|
295 |
&& (getName() == other.getName())) { |
|
296 |
if (!returnType.equals(other.getReturnType())) |
|
297 |
return false; |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
298 |
return equalParamTypes(parameterTypes, other.parameterTypes); |
2 | 299 |
} |
300 |
} |
|
301 |
return false; |
|
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Returns a hashcode for this {@code Method}. The hashcode is computed |
|
306 |
* as the exclusive-or of the hashcodes for the underlying |
|
307 |
* method's declaring class name and the method's name. |
|
308 |
*/ |
|
309 |
public int hashCode() { |
|
310 |
return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Returns a string describing this {@code Method}. The string is |
|
315 |
* formatted as the method access modifiers, if any, followed by |
|
316 |
* the method return type, followed by a space, followed by the |
|
317 |
* class declaring the method, followed by a period, followed by |
|
318 |
* the method name, followed by a parenthesized, comma-separated |
|
319 |
* list of the method's formal parameter types. If the method |
|
320 |
* throws checked exceptions, the parameter list is followed by a |
|
321 |
* space, followed by the word throws followed by a |
|
322 |
* comma-separated list of the thrown exception types. |
|
323 |
* For example: |
|
324 |
* <pre> |
|
325 |
* public boolean java.lang.Object.equals(java.lang.Object) |
|
326 |
* </pre> |
|
327 |
* |
|
328 |
* <p>The access modifiers are placed in canonical order as |
|
329 |
* specified by "The Java Language Specification". This is |
|
330 |
* {@code public}, {@code protected} or {@code private} first, |
|
331 |
* and then other modifiers in the following order: |
|
332 |
* {@code abstract}, {@code static}, {@code final}, |
|
3712
8851d55adef0
6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents:
2174
diff
changeset
|
333 |
* {@code synchronized}, {@code native}, {@code strictfp}. |
2 | 334 |
*/ |
335 |
public String toString() { |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
336 |
return sharedToString(Modifier.methodModifiers(), |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
337 |
parameterTypes, |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
338 |
exceptionTypes); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
339 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
340 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
341 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
342 |
void specificToStringHeader(StringBuilder sb) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
343 |
sb.append(Field.getTypeName(getReturnType())).append(' '); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
344 |
sb.append(Field.getTypeName(getDeclaringClass())).append('.'); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
345 |
sb.append(getName()); |
2 | 346 |
} |
347 |
||
348 |
/** |
|
349 |
* Returns a string describing this {@code Method}, including |
|
350 |
* type parameters. The string is formatted as the method access |
|
351 |
* modifiers, if any, followed by an angle-bracketed |
|
352 |
* comma-separated list of the method's type parameters, if any, |
|
353 |
* followed by the method's generic return type, followed by a |
|
354 |
* space, followed by the class declaring the method, followed by |
|
355 |
* a period, followed by the method name, followed by a |
|
356 |
* parenthesized, comma-separated list of the method's generic |
|
357 |
* formal parameter types. |
|
358 |
* |
|
359 |
* If this method was declared to take a variable number of |
|
360 |
* arguments, instead of denoting the last parameter as |
|
361 |
* "<tt><i>Type</i>[]</tt>", it is denoted as |
|
362 |
* "<tt><i>Type</i>...</tt>". |
|
363 |
* |
|
364 |
* A space is used to separate access modifiers from one another |
|
365 |
* and from the type parameters or return type. If there are no |
|
366 |
* type parameters, the type parameter list is elided; if the type |
|
367 |
* parameter list is present, a space separates the list from the |
|
368 |
* class name. If the method is declared to throw exceptions, the |
|
369 |
* parameter list is followed by a space, followed by the word |
|
370 |
* throws followed by a comma-separated list of the generic thrown |
|
371 |
* exception types. If there are no type parameters, the type |
|
372 |
* parameter list is elided. |
|
373 |
* |
|
374 |
* <p>The access modifiers are placed in canonical order as |
|
375 |
* specified by "The Java Language Specification". This is |
|
376 |
* {@code public}, {@code protected} or {@code private} first, |
|
377 |
* and then other modifiers in the following order: |
|
378 |
* {@code abstract}, {@code static}, {@code final}, |
|
3712
8851d55adef0
6261502: (reflect) Add the functionality to screen out the "inappropriate" modifier bits
darcy
parents:
2174
diff
changeset
|
379 |
* {@code synchronized}, {@code native}, {@code strictfp}. |
2 | 380 |
* |
381 |
* @return a string describing this {@code Method}, |
|
382 |
* include type parameters |
|
383 |
* |
|
384 |
* @since 1.5 |
|
385 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
386 |
@Override |
2 | 387 |
public String toGenericString() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
388 |
return sharedToGenericString(Modifier.methodModifiers()); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
389 |
} |
2 | 390 |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
391 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
392 |
void specificToGenericStringHeader(StringBuilder sb) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
393 |
Type genRetType = getGenericReturnType(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
394 |
sb.append( ((genRetType instanceof Class<?>)? |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
395 |
Field.getTypeName((Class<?>)genRetType):genRetType.toString())) |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
396 |
.append(' '); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
397 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
398 |
sb.append(Field.getTypeName(getDeclaringClass())).append('.'); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
399 |
sb.append(getName()); |
2 | 400 |
} |
401 |
||
402 |
/** |
|
403 |
* Invokes the underlying method represented by this {@code Method} |
|
404 |
* object, on the specified object with the specified parameters. |
|
405 |
* Individual parameters are automatically unwrapped to match |
|
406 |
* primitive formal parameters, and both primitive and reference |
|
407 |
* parameters are subject to method invocation conversions as |
|
408 |
* necessary. |
|
409 |
* |
|
410 |
* <p>If the underlying method is static, then the specified {@code obj} |
|
411 |
* argument is ignored. It may be null. |
|
412 |
* |
|
413 |
* <p>If the number of formal parameters required by the underlying method is |
|
414 |
* 0, the supplied {@code args} array may be of length 0 or null. |
|
415 |
* |
|
416 |
* <p>If the underlying method is an instance method, it is invoked |
|
417 |
* using dynamic method lookup as documented in The Java Language |
|
418 |
* Specification, Second Edition, section 15.12.4.4; in particular, |
|
419 |
* overriding based on the runtime type of the target object will occur. |
|
420 |
* |
|
421 |
* <p>If the underlying method is static, the class that declared |
|
422 |
* the method is initialized if it has not already been initialized. |
|
423 |
* |
|
424 |
* <p>If the method completes normally, the value it returns is |
|
425 |
* returned to the caller of invoke; if the value has a primitive |
|
426 |
* type, it is first appropriately wrapped in an object. However, |
|
427 |
* if the value has the type of an array of a primitive type, the |
|
428 |
* elements of the array are <i>not</i> wrapped in objects; in |
|
429 |
* other words, an array of primitive type is returned. If the |
|
430 |
* underlying method return type is void, the invocation returns |
|
431 |
* null. |
|
432 |
* |
|
433 |
* @param obj the object the underlying method is invoked from |
|
434 |
* @param args the arguments used for the method call |
|
435 |
* @return the result of dispatching the method represented by |
|
436 |
* this object on {@code obj} with parameters |
|
437 |
* {@code args} |
|
438 |
* |
|
439 |
* @exception IllegalAccessException if this {@code Method} object |
|
9022
b2e8758b10fd
6543593: (reflect) Clarify private final field mutability
darcy
parents:
5506
diff
changeset
|
440 |
* is enforcing Java language access control and the underlying |
2 | 441 |
* method is inaccessible. |
442 |
* @exception IllegalArgumentException if the method is an |
|
443 |
* instance method and the specified object argument |
|
444 |
* is not an instance of the class or interface |
|
445 |
* declaring the underlying method (or of a subclass |
|
446 |
* or implementor thereof); if the number of actual |
|
447 |
* and formal parameters differ; if an unwrapping |
|
448 |
* conversion for primitive arguments fails; or if, |
|
449 |
* after possible unwrapping, a parameter value |
|
450 |
* cannot be converted to the corresponding formal |
|
451 |
* parameter type by a method invocation conversion. |
|
452 |
* @exception InvocationTargetException if the underlying method |
|
453 |
* throws an exception. |
|
454 |
* @exception NullPointerException if the specified object is null |
|
455 |
* and the method is an instance method. |
|
456 |
* @exception ExceptionInInitializerError if the initialization |
|
457 |
* provoked by this method fails. |
|
458 |
*/ |
|
459 |
public Object invoke(Object obj, Object... args) |
|
460 |
throws IllegalAccessException, IllegalArgumentException, |
|
461 |
InvocationTargetException |
|
462 |
{ |
|
463 |
if (!override) { |
|
464 |
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
|
465 |
Class<?> caller = Reflection.getCallerClass(1); |
2 | 466 |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
467 |
checkAccess(caller, clazz, obj, modifiers); |
2 | 468 |
} |
469 |
} |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
470 |
MethodAccessor ma = methodAccessor; // read volatile |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
471 |
if (ma == null) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
472 |
ma = acquireMethodAccessor(); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
473 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
474 |
return ma.invoke(obj, args); |
2 | 475 |
} |
476 |
||
477 |
/** |
|
478 |
* Returns {@code true} if this method is a bridge |
|
479 |
* method; returns {@code false} otherwise. |
|
480 |
* |
|
481 |
* @return true if and only if this method is a bridge |
|
482 |
* method as defined by the Java Language Specification. |
|
483 |
* @since 1.5 |
|
484 |
*/ |
|
485 |
public boolean isBridge() { |
|
486 |
return (getModifiers() & Modifier.BRIDGE) != 0; |
|
487 |
} |
|
488 |
||
489 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
490 |
* {@inheritDoc} |
2 | 491 |
* @since 1.5 |
492 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
493 |
@Override |
2 | 494 |
public boolean isVarArgs() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
495 |
return super.isVarArgs(); |
2 | 496 |
} |
497 |
||
498 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
499 |
* {@inheritDoc} |
2 | 500 |
* @since 1.5 |
501 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
502 |
@Override |
2 | 503 |
public boolean isSynthetic() { |
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
504 |
return super.isSynthetic(); |
2 | 505 |
} |
506 |
||
507 |
// NOTE that there is no synchronization used here. It is correct |
|
508 |
// (though not efficient) to generate more than one MethodAccessor |
|
509 |
// for a given Method. However, avoiding synchronization will |
|
510 |
// probably make the implementation more scalable. |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
511 |
private MethodAccessor acquireMethodAccessor() { |
2 | 512 |
// First check to see if one has been created yet, and take it |
513 |
// if so |
|
514 |
MethodAccessor tmp = null; |
|
515 |
if (root != null) tmp = root.getMethodAccessor(); |
|
516 |
if (tmp != null) { |
|
517 |
methodAccessor = tmp; |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
518 |
} else { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
519 |
// 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
|
520 |
tmp = reflectionFactory.newMethodAccessor(this); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
521 |
setMethodAccessor(tmp); |
2 | 522 |
} |
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
523 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
9022
diff
changeset
|
524 |
return tmp; |
2 | 525 |
} |
526 |
||
527 |
// Returns MethodAccessor for this Method object, not looking up |
|
528 |
// the chain to the root |
|
529 |
MethodAccessor getMethodAccessor() { |
|
530 |
return methodAccessor; |
|
531 |
} |
|
532 |
||
533 |
// Sets the MethodAccessor for this Method object and |
|
534 |
// (recursively) its root |
|
535 |
void setMethodAccessor(MethodAccessor accessor) { |
|
536 |
methodAccessor = accessor; |
|
537 |
// Propagate up |
|
538 |
if (root != null) { |
|
539 |
root.setMethodAccessor(accessor); |
|
540 |
} |
|
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Returns the default value for the annotation member represented by |
|
545 |
* this {@code Method} instance. If the member is of a primitive type, |
|
546 |
* an instance of the corresponding wrapper type is returned. Returns |
|
547 |
* null if no default is associated with the member, or if the method |
|
548 |
* instance does not represent a declared member of an annotation type. |
|
549 |
* |
|
550 |
* @return the default value for the annotation member represented |
|
551 |
* by this {@code Method} instance. |
|
552 |
* @throws TypeNotPresentException if the annotation is of type |
|
553 |
* {@link Class} and no definition can be found for the |
|
554 |
* default class value. |
|
555 |
* @since 1.5 |
|
556 |
*/ |
|
557 |
public Object getDefaultValue() { |
|
558 |
if (annotationDefault == null) |
|
559 |
return null; |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
3941
diff
changeset
|
560 |
Class<?> memberType = AnnotationType.invocationHandlerReturnType( |
2 | 561 |
getReturnType()); |
562 |
Object result = AnnotationParser.parseMemberValue( |
|
563 |
memberType, ByteBuffer.wrap(annotationDefault), |
|
564 |
sun.misc.SharedSecrets.getJavaLangAccess(). |
|
565 |
getConstantPool(getDeclaringClass()), |
|
566 |
getDeclaringClass()); |
|
567 |
if (result instanceof sun.reflect.annotation.ExceptionProxy) |
|
568 |
throw new AnnotationFormatError("Invalid default: " + this); |
|
569 |
return result; |
|
570 |
} |
|
571 |
||
572 |
/** |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
573 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
574 |
* @throws NullPointerException {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
575 |
* @since 1.5 |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
576 |
*/ |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
577 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
578 |
return super.getAnnotation(annotationClass); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
579 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
580 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
581 |
/** |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
582 |
* {@inheritDoc} |
2 | 583 |
* @since 1.5 |
584 |
*/ |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
585 |
public Annotation[] getDeclaredAnnotations() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
586 |
return super.getDeclaredAnnotations(); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
587 |
} |
2 | 588 |
|
10127
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
589 |
/** |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
590 |
* {@inheritDoc} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
591 |
* @since 1.5 |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
592 |
*/ |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
593 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
594 |
public Annotation[][] getParameterAnnotations() { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
595 |
return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations); |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
596 |
} |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
597 |
|
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
598 |
@Override |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
599 |
void handleParameterNumberMismatch(int resultLength, int numParameters) { |
c85d1ec57ee7
7007535: (reflect) Please generalize Constructor and Method
darcy
parents:
9266
diff
changeset
|
600 |
throw new AnnotationFormatError("Parameter annotations don't match number of parameters"); |
2 | 601 |
} |
602 |
} |