jaroslav@1646
|
1 |
/*
|
jaroslav@1646
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@1646
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1646
|
4 |
*
|
jaroslav@1646
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1646
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1646
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1646
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1646
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1646
|
10 |
*
|
jaroslav@1646
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1646
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1646
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1646
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1646
|
15 |
* accompanied this code).
|
jaroslav@1646
|
16 |
*
|
jaroslav@1646
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1646
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1646
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1646
|
20 |
*
|
jaroslav@1646
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1646
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1646
|
23 |
* questions.
|
jaroslav@1646
|
24 |
*/
|
jaroslav@1646
|
25 |
|
jaroslav@1646
|
26 |
package java.lang.invoke;
|
jaroslav@1646
|
27 |
|
jaroslav@1646
|
28 |
import java.lang.reflect.*;
|
jaroslav@1646
|
29 |
import java.util.*;
|
jaroslav@1646
|
30 |
import java.lang.invoke.MethodHandleNatives.Constants;
|
jaroslav@1646
|
31 |
import java.lang.invoke.MethodHandles.Lookup;
|
jaroslav@1646
|
32 |
import static java.lang.invoke.MethodHandleStatics.*;
|
jaroslav@1646
|
33 |
|
jaroslav@1646
|
34 |
/**
|
jaroslav@1646
|
35 |
* A symbolic reference obtained by cracking a direct method handle
|
jaroslav@1646
|
36 |
* into its consitutent symbolic parts.
|
jaroslav@1646
|
37 |
* To crack a direct method handle, call {@link Lookup#revealDirect Lookup.revealDirect}.
|
jaroslav@1646
|
38 |
* <h1><a name="directmh"></a>Direct Method Handles</h1>
|
jaroslav@1646
|
39 |
* A <em>direct method handle</em> represents a method, constructor, or field without
|
jaroslav@1646
|
40 |
* any intervening argument bindings or other transformations.
|
jaroslav@1646
|
41 |
* The method, constructor, or field referred to by a direct method handle is called
|
jaroslav@1646
|
42 |
* its <em>underlying member</em>.
|
jaroslav@1646
|
43 |
* Direct method handles may be obtained in any of these ways:
|
jaroslav@1646
|
44 |
* <ul>
|
jaroslav@1646
|
45 |
* <li>By executing an {@code ldc} instruction on a {@code CONSTANT_MethodHandle} constant.
|
jaroslav@1646
|
46 |
* (See the Java Virtual Machine Specification, sections 4.4.8 and 5.4.3.)
|
jaroslav@1646
|
47 |
* <li>By calling one of the <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a>,
|
jaroslav@1646
|
48 |
* such as {@link Lookup#findVirtual Lookup.findVirtual},
|
jaroslav@1646
|
49 |
* to resolve a symbolic reference into a method handle.
|
jaroslav@1646
|
50 |
* A symbolic reference consists of a class, name string, and type.
|
jaroslav@1646
|
51 |
* <li>By calling the factory method {@link Lookup#unreflect Lookup.unreflect}
|
jaroslav@1646
|
52 |
* or {@link Lookup#unreflectSpecial Lookup.unreflectSpecial}
|
jaroslav@1646
|
53 |
* to convert a {@link Method} into a method handle.
|
jaroslav@1646
|
54 |
* <li>By calling the factory method {@link Lookup#unreflectConstructor Lookup.unreflectConstructor}
|
jaroslav@1646
|
55 |
* to convert a {@link Constructor} into a method handle.
|
jaroslav@1646
|
56 |
* <li>By calling the factory method {@link Lookup#unreflectGetter Lookup.unreflectGetter}
|
jaroslav@1646
|
57 |
* or {@link Lookup#unreflectSetter Lookup.unreflectSetter}
|
jaroslav@1646
|
58 |
* to convert a {@link Field} into a method handle.
|
jaroslav@1646
|
59 |
* </ul>
|
jaroslav@1646
|
60 |
*
|
jaroslav@1646
|
61 |
* <h1>Restrictions on Cracking</h1>
|
jaroslav@1646
|
62 |
* Given a suitable {@code Lookup} object, it is possible to crack any direct method handle
|
jaroslav@1646
|
63 |
* to recover a symbolic reference for the underlying method, constructor, or field.
|
jaroslav@1646
|
64 |
* Cracking must be done via a {@code Lookup} object equivalent to that which created
|
jaroslav@1646
|
65 |
* the target method handle, or which has enough access permissions to recreate
|
jaroslav@1646
|
66 |
* an equivalent method handle.
|
jaroslav@1646
|
67 |
* <p>
|
jaroslav@1646
|
68 |
* If the underlying method is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>,
|
jaroslav@1646
|
69 |
* the direct method handle will have been "bound" to a particular caller class, the
|
jaroslav@1646
|
70 |
* {@linkplain java.lang.invoke.MethodHandles.Lookup#lookupClass() lookup class}
|
jaroslav@1646
|
71 |
* of the lookup object used to create it.
|
jaroslav@1646
|
72 |
* Cracking this method handle with a different lookup class will fail
|
jaroslav@1646
|
73 |
* even if the underlying method is public (like {@code Class.forName}).
|
jaroslav@1646
|
74 |
* <p>
|
jaroslav@1646
|
75 |
* The requirement of lookup object matching provides a "fast fail" behavior
|
jaroslav@1646
|
76 |
* for programs which may otherwise trust erroneous revelation of a method
|
jaroslav@1646
|
77 |
* handle with symbolic information (or caller binding) from an unexpected scope.
|
jaroslav@1646
|
78 |
* Use {@link java.lang.invoke.MethodHandles#reflectAs} to override this limitation.
|
jaroslav@1646
|
79 |
*
|
jaroslav@1646
|
80 |
* <h1><a name="refkinds"></a>Reference kinds</h1>
|
jaroslav@1646
|
81 |
* The <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a>
|
jaroslav@1646
|
82 |
* correspond to all major use cases for methods, constructors, and fields.
|
jaroslav@1646
|
83 |
* These use cases may be distinguished using small integers as follows:
|
jaroslav@1646
|
84 |
* <table border=1 cellpadding=5 summary="reference kinds">
|
jaroslav@1646
|
85 |
* <tr><th>reference kind</th><th>descriptive name</th><th>scope</th><th>member</th><th>behavior</th></tr>
|
jaroslav@1646
|
86 |
* <tr>
|
jaroslav@1646
|
87 |
* <td>{@code 1}</td><td>{@code REF_getField}</td><td>{@code class}</td>
|
jaroslav@1646
|
88 |
* <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
|
jaroslav@1646
|
89 |
* </tr>
|
jaroslav@1646
|
90 |
* <tr>
|
jaroslav@1646
|
91 |
* <td>{@code 2}</td><td>{@code REF_getStatic}</td><td>{@code class} or {@code interface}</td>
|
jaroslav@1646
|
92 |
* <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td>
|
jaroslav@1646
|
93 |
* </tr>
|
jaroslav@1646
|
94 |
* <tr>
|
jaroslav@1646
|
95 |
* <td>{@code 3}</td><td>{@code REF_putField}</td><td>{@code class}</td>
|
jaroslav@1646
|
96 |
* <td>{@code FT f;}</td><td>{@code this.f = x;}</td>
|
jaroslav@1646
|
97 |
* </tr>
|
jaroslav@1646
|
98 |
* <tr>
|
jaroslav@1646
|
99 |
* <td>{@code 4}</td><td>{@code REF_putStatic}</td><td>{@code class}</td>
|
jaroslav@1646
|
100 |
* <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td>
|
jaroslav@1646
|
101 |
* </tr>
|
jaroslav@1646
|
102 |
* <tr>
|
jaroslav@1646
|
103 |
* <td>{@code 5}</td><td>{@code REF_invokeVirtual}</td><td>{@code class}</td>
|
jaroslav@1646
|
104 |
* <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
|
jaroslav@1646
|
105 |
* </tr>
|
jaroslav@1646
|
106 |
* <tr>
|
jaroslav@1646
|
107 |
* <td>{@code 6}</td><td>{@code REF_invokeStatic}</td><td>{@code class} or {@code interface}</td>
|
jaroslav@1646
|
108 |
* <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td>
|
jaroslav@1646
|
109 |
* </tr>
|
jaroslav@1646
|
110 |
* <tr>
|
jaroslav@1646
|
111 |
* <td>{@code 7}</td><td>{@code REF_invokeSpecial}</td><td>{@code class} or {@code interface}</td>
|
jaroslav@1646
|
112 |
* <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
|
jaroslav@1646
|
113 |
* </tr>
|
jaroslav@1646
|
114 |
* <tr>
|
jaroslav@1646
|
115 |
* <td>{@code 8}</td><td>{@code REF_newInvokeSpecial}</td><td>{@code class}</td>
|
jaroslav@1646
|
116 |
* <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td>
|
jaroslav@1646
|
117 |
* </tr>
|
jaroslav@1646
|
118 |
* <tr>
|
jaroslav@1646
|
119 |
* <td>{@code 9}</td><td>{@code REF_invokeInterface}</td><td>{@code interface}</td>
|
jaroslav@1646
|
120 |
* <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
|
jaroslav@1646
|
121 |
* </tr>
|
jaroslav@1646
|
122 |
* </table>
|
jaroslav@1646
|
123 |
* @since 1.8
|
jaroslav@1646
|
124 |
*/
|
jaroslav@1646
|
125 |
public
|
jaroslav@1646
|
126 |
interface MethodHandleInfo {
|
jaroslav@1646
|
127 |
/**
|
jaroslav@1646
|
128 |
* A direct method handle reference kind,
|
jaroslav@1646
|
129 |
* as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>.
|
jaroslav@1646
|
130 |
*/
|
jaroslav@1646
|
131 |
public static final int
|
jaroslav@1646
|
132 |
REF_getField = Constants.REF_getField,
|
jaroslav@1646
|
133 |
REF_getStatic = Constants.REF_getStatic,
|
jaroslav@1646
|
134 |
REF_putField = Constants.REF_putField,
|
jaroslav@1646
|
135 |
REF_putStatic = Constants.REF_putStatic,
|
jaroslav@1646
|
136 |
REF_invokeVirtual = Constants.REF_invokeVirtual,
|
jaroslav@1646
|
137 |
REF_invokeStatic = Constants.REF_invokeStatic,
|
jaroslav@1646
|
138 |
REF_invokeSpecial = Constants.REF_invokeSpecial,
|
jaroslav@1646
|
139 |
REF_newInvokeSpecial = Constants.REF_newInvokeSpecial,
|
jaroslav@1646
|
140 |
REF_invokeInterface = Constants.REF_invokeInterface;
|
jaroslav@1646
|
141 |
|
jaroslav@1646
|
142 |
/**
|
jaroslav@1646
|
143 |
* Returns the reference kind of the cracked method handle, which in turn
|
jaroslav@1646
|
144 |
* determines whether the method handle's underlying member was a constructor, method, or field.
|
jaroslav@1646
|
145 |
* See the <a href="MethodHandleInfo.html#refkinds">table above</a> for definitions.
|
jaroslav@1646
|
146 |
* @return the integer code for the kind of reference used to access the underlying member
|
jaroslav@1646
|
147 |
*/
|
jaroslav@1646
|
148 |
public int getReferenceKind();
|
jaroslav@1646
|
149 |
|
jaroslav@1646
|
150 |
/**
|
jaroslav@1646
|
151 |
* Returns the class in which the cracked method handle's underlying member was defined.
|
jaroslav@1646
|
152 |
* @return the declaring class of the underlying member
|
jaroslav@1646
|
153 |
*/
|
jaroslav@1646
|
154 |
public Class<?> getDeclaringClass();
|
jaroslav@1646
|
155 |
|
jaroslav@1646
|
156 |
/**
|
jaroslav@1646
|
157 |
* Returns the name of the cracked method handle's underlying member.
|
jaroslav@1646
|
158 |
* This is {@code "<init>"} if the underlying member was a constructor,
|
jaroslav@1646
|
159 |
* else it is a simple method name or field name.
|
jaroslav@1646
|
160 |
* @return the simple name of the underlying member
|
jaroslav@1646
|
161 |
*/
|
jaroslav@1646
|
162 |
public String getName();
|
jaroslav@1646
|
163 |
|
jaroslav@1646
|
164 |
/**
|
jaroslav@1646
|
165 |
* Returns the nominal type of the cracked symbolic reference, expressed as a method type.
|
jaroslav@1646
|
166 |
* If the reference is to a constructor, the return type will be {@code void}.
|
jaroslav@1646
|
167 |
* If it is to a non-static method, the method type will not mention the {@code this} parameter.
|
jaroslav@1646
|
168 |
* If it is to a field and the requested access is to read the field,
|
jaroslav@1646
|
169 |
* the method type will have no parameters and return the field type.
|
jaroslav@1646
|
170 |
* If it is to a field and the requested access is to write the field,
|
jaroslav@1646
|
171 |
* the method type will have one parameter of the field type and return {@code void}.
|
jaroslav@1646
|
172 |
* <p>
|
jaroslav@1646
|
173 |
* Note that original direct method handle may include a leading {@code this} parameter,
|
jaroslav@1646
|
174 |
* or (in the case of a constructor) will replace the {@code void} return type
|
jaroslav@1646
|
175 |
* with the constructed class.
|
jaroslav@1646
|
176 |
* The nominal type does not include any {@code this} parameter,
|
jaroslav@1646
|
177 |
* and (in the case of a constructor) will return {@code void}.
|
jaroslav@1646
|
178 |
* @return the type of the underlying member, expressed as a method type
|
jaroslav@1646
|
179 |
*/
|
jaroslav@1646
|
180 |
public MethodType getMethodType();
|
jaroslav@1646
|
181 |
|
jaroslav@1646
|
182 |
// Utility methods.
|
jaroslav@1646
|
183 |
// NOTE: class/name/type and reference kind constitute a symbolic reference
|
jaroslav@1646
|
184 |
// member and modifiers are an add-on, derived from Core Reflection (or the equivalent)
|
jaroslav@1646
|
185 |
|
jaroslav@1646
|
186 |
/**
|
jaroslav@1646
|
187 |
* Reflects the underlying member as a method, constructor, or field object.
|
jaroslav@1646
|
188 |
* If the underlying member is public, it is reflected as if by
|
jaroslav@1646
|
189 |
* {@code getMethod}, {@code getConstructor}, or {@code getField}.
|
jaroslav@1646
|
190 |
* Otherwise, it is reflected as if by
|
jaroslav@1646
|
191 |
* {@code getDeclaredMethod}, {@code getDeclaredConstructor}, or {@code getDeclaredField}.
|
jaroslav@1646
|
192 |
* The underlying member must be accessible to the given lookup object.
|
jaroslav@1646
|
193 |
* @param <T> the desired type of the result, either {@link Member} or a subtype
|
jaroslav@1646
|
194 |
* @param expected a class object representing the desired result type {@code T}
|
jaroslav@1646
|
195 |
* @param lookup the lookup object that created this MethodHandleInfo, or one with equivalent access privileges
|
jaroslav@1646
|
196 |
* @return a reference to the method, constructor, or field object
|
jaroslav@1646
|
197 |
* @exception ClassCastException if the member is not of the expected type
|
jaroslav@1646
|
198 |
* @exception NullPointerException if either argument is {@code null}
|
jaroslav@1646
|
199 |
* @exception IllegalArgumentException if the underlying member is not accessible to the given lookup object
|
jaroslav@1646
|
200 |
*/
|
jaroslav@1646
|
201 |
public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup);
|
jaroslav@1646
|
202 |
|
jaroslav@1646
|
203 |
/**
|
jaroslav@1646
|
204 |
* Returns the access modifiers of the underlying member.
|
jaroslav@1646
|
205 |
* @return the Java language modifiers for underlying member,
|
jaroslav@1646
|
206 |
* or -1 if the member cannot be accessed
|
jaroslav@1646
|
207 |
* @see Modifier
|
jaroslav@1646
|
208 |
* @see #reflectAs
|
jaroslav@1646
|
209 |
*/
|
jaroslav@1646
|
210 |
public int getModifiers();
|
jaroslav@1646
|
211 |
|
jaroslav@1646
|
212 |
/**
|
jaroslav@1646
|
213 |
* Determines if the underlying member was a variable arity method or constructor.
|
jaroslav@1646
|
214 |
* Such members are represented by method handles that are varargs collectors.
|
jaroslav@1646
|
215 |
* @implSpec
|
jaroslav@1646
|
216 |
* This produces a result equivalent to:
|
jaroslav@1646
|
217 |
* <pre>{@code
|
jaroslav@1646
|
218 |
* getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers())
|
jaroslav@1646
|
219 |
* }</pre>
|
jaroslav@1646
|
220 |
*
|
jaroslav@1646
|
221 |
*
|
jaroslav@1646
|
222 |
* @return {@code true} if and only if the underlying member was declared with variable arity.
|
jaroslav@1646
|
223 |
*/
|
jaroslav@1646
|
224 |
// spelling derived from java.lang.reflect.Executable, not MethodHandle.isVarargsCollector
|
jaroslav@1646
|
225 |
public default boolean isVarArgs() {
|
jaroslav@1646
|
226 |
// fields are never varargs:
|
jaroslav@1646
|
227 |
if (MethodHandleNatives.refKindIsField((byte) getReferenceKind()))
|
jaroslav@1646
|
228 |
return false;
|
jaroslav@1646
|
229 |
// not in the public API: Modifier.VARARGS
|
jaroslav@1646
|
230 |
final int ACC_VARARGS = 0x00000080; // from JVMS 4.6 (Table 4.20)
|
jaroslav@1646
|
231 |
assert(ACC_VARARGS == Modifier.TRANSIENT);
|
jaroslav@1646
|
232 |
return Modifier.isTransient(getModifiers());
|
jaroslav@1646
|
233 |
}
|
jaroslav@1646
|
234 |
|
jaroslav@1646
|
235 |
/**
|
jaroslav@1646
|
236 |
* Returns the descriptive name of the given reference kind,
|
jaroslav@1646
|
237 |
* as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>.
|
jaroslav@1646
|
238 |
* The conventional prefix "REF_" is omitted.
|
jaroslav@1646
|
239 |
* @param referenceKind an integer code for a kind of reference used to access a class member
|
jaroslav@1646
|
240 |
* @return a mixed-case string such as {@code "getField"}
|
jaroslav@1646
|
241 |
* @exception IllegalArgumentException if the argument is not a valid
|
jaroslav@1646
|
242 |
* <a href="MethodHandleInfo.html#refkinds">reference kind number</a>
|
jaroslav@1646
|
243 |
*/
|
jaroslav@1646
|
244 |
public static String referenceKindToString(int referenceKind) {
|
jaroslav@1646
|
245 |
if (!MethodHandleNatives.refKindIsValid(referenceKind))
|
jaroslav@1646
|
246 |
throw newIllegalArgumentException("invalid reference kind", referenceKind);
|
jaroslav@1646
|
247 |
return MethodHandleNatives.refKindName((byte)referenceKind);
|
jaroslav@1646
|
248 |
}
|
jaroslav@1646
|
249 |
|
jaroslav@1646
|
250 |
/**
|
jaroslav@1646
|
251 |
* Returns a string representation for a {@code MethodHandleInfo},
|
jaroslav@1646
|
252 |
* given the four parts of its symbolic reference.
|
jaroslav@1646
|
253 |
* This is defined to be of the form {@code "RK C.N:MT"}, where {@code RK} is the
|
jaroslav@1646
|
254 |
* {@linkplain #referenceKindToString reference kind string} for {@code kind},
|
jaroslav@1646
|
255 |
* {@code C} is the {@linkplain java.lang.Class#getName name} of {@code defc}
|
jaroslav@1646
|
256 |
* {@code N} is the {@code name}, and
|
jaroslav@1646
|
257 |
* {@code MT} is the {@code type}.
|
jaroslav@1646
|
258 |
* These four values may be obtained from the
|
jaroslav@1646
|
259 |
* {@linkplain #getReferenceKind reference kind},
|
jaroslav@1646
|
260 |
* {@linkplain #getDeclaringClass declaring class},
|
jaroslav@1646
|
261 |
* {@linkplain #getName member name},
|
jaroslav@1646
|
262 |
* and {@linkplain #getMethodType method type}
|
jaroslav@1646
|
263 |
* of a {@code MethodHandleInfo} object.
|
jaroslav@1646
|
264 |
*
|
jaroslav@1646
|
265 |
* @implSpec
|
jaroslav@1646
|
266 |
* This produces a result equivalent to:
|
jaroslav@1646
|
267 |
* <pre>{@code
|
jaroslav@1646
|
268 |
* String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type)
|
jaroslav@1646
|
269 |
* }</pre>
|
jaroslav@1646
|
270 |
*
|
jaroslav@1646
|
271 |
* @param kind the {@linkplain #getReferenceKind reference kind} part of the symbolic reference
|
jaroslav@1646
|
272 |
* @param defc the {@linkplain #getDeclaringClass declaring class} part of the symbolic reference
|
jaroslav@1646
|
273 |
* @param name the {@linkplain #getName member name} part of the symbolic reference
|
jaroslav@1646
|
274 |
* @param type the {@linkplain #getMethodType method type} part of the symbolic reference
|
jaroslav@1646
|
275 |
* @return a string of the form {@code "RK C.N:MT"}
|
jaroslav@1646
|
276 |
* @exception IllegalArgumentException if the first argument is not a valid
|
jaroslav@1646
|
277 |
* <a href="MethodHandleInfo.html#refkinds">reference kind number</a>
|
jaroslav@1646
|
278 |
* @exception NullPointerException if any reference argument is {@code null}
|
jaroslav@1646
|
279 |
*/
|
jaroslav@1646
|
280 |
public static String toString(int kind, Class<?> defc, String name, MethodType type) {
|
jaroslav@1646
|
281 |
Objects.requireNonNull(name); Objects.requireNonNull(type);
|
jaroslav@1646
|
282 |
return String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type);
|
jaroslav@1646
|
283 |
}
|
jaroslav@1646
|
284 |
}
|