jaroslav@1646
|
1 |
/*
|
jaroslav@1646
|
2 |
* Copyright (c) 2008, 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.security.AccessController;
|
jaroslav@1646
|
29 |
import java.security.PrivilegedAction;
|
jaroslav@1646
|
30 |
import java.util.ArrayList;
|
jaroslav@1646
|
31 |
import java.util.Arrays;
|
jaroslav@1646
|
32 |
import java.util.HashMap;
|
jaroslav@1646
|
33 |
import sun.invoke.empty.Empty;
|
jaroslav@1646
|
34 |
import sun.invoke.util.ValueConversions;
|
jaroslav@1646
|
35 |
import sun.invoke.util.VerifyType;
|
jaroslav@1646
|
36 |
import sun.invoke.util.Wrapper;
|
jaroslav@1646
|
37 |
import static java.lang.invoke.LambdaForm.*;
|
jaroslav@1646
|
38 |
import static java.lang.invoke.MethodHandleStatics.*;
|
jaroslav@1646
|
39 |
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
|
jaroslav@1646
|
40 |
|
jaroslav@1646
|
41 |
/**
|
jaroslav@1646
|
42 |
* Trusted implementation code for MethodHandle.
|
jaroslav@1646
|
43 |
* @author jrose
|
jaroslav@1646
|
44 |
*/
|
jaroslav@1646
|
45 |
/*non-public*/ abstract class MethodHandleImpl {
|
jaroslav@1646
|
46 |
/// Factory methods to create method handles:
|
jaroslav@1646
|
47 |
|
jaroslav@1646
|
48 |
static void initStatics() {
|
jaroslav@1646
|
49 |
// Trigger selected static initializations.
|
jaroslav@1646
|
50 |
MemberName.Factory.INSTANCE.getClass();
|
jaroslav@1646
|
51 |
}
|
jaroslav@1646
|
52 |
|
jaroslav@1646
|
53 |
static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
|
jaroslav@1646
|
54 |
if (!arrayClass.isArray())
|
jaroslav@1646
|
55 |
throw newIllegalArgumentException("not an array: "+arrayClass);
|
jaroslav@1646
|
56 |
MethodHandle accessor = ArrayAccessor.getAccessor(arrayClass, isSetter);
|
jaroslav@1646
|
57 |
MethodType srcType = accessor.type().erase();
|
jaroslav@1646
|
58 |
MethodType lambdaType = srcType.invokerType();
|
jaroslav@1646
|
59 |
Name[] names = arguments(1, lambdaType);
|
jaroslav@1646
|
60 |
Name[] args = Arrays.copyOfRange(names, 1, 1 + srcType.parameterCount());
|
jaroslav@1646
|
61 |
names[names.length - 1] = new Name(accessor.asType(srcType), (Object[]) args);
|
jaroslav@1646
|
62 |
LambdaForm form = new LambdaForm("getElement", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
63 |
MethodHandle mh = SimpleMethodHandle.make(srcType, form);
|
jaroslav@1646
|
64 |
if (ArrayAccessor.needCast(arrayClass)) {
|
jaroslav@1646
|
65 |
mh = mh.bindTo(arrayClass);
|
jaroslav@1646
|
66 |
}
|
jaroslav@1646
|
67 |
mh = mh.asType(ArrayAccessor.correctType(arrayClass, isSetter));
|
jaroslav@1646
|
68 |
return mh;
|
jaroslav@1646
|
69 |
}
|
jaroslav@1646
|
70 |
|
jaroslav@1646
|
71 |
static final class ArrayAccessor {
|
jaroslav@1646
|
72 |
/// Support for array element access
|
jaroslav@1646
|
73 |
static final HashMap<Class<?>, MethodHandle> GETTER_CACHE = new HashMap<>(); // TODO use it
|
jaroslav@1646
|
74 |
static final HashMap<Class<?>, MethodHandle> SETTER_CACHE = new HashMap<>(); // TODO use it
|
jaroslav@1646
|
75 |
|
jaroslav@1646
|
76 |
static int getElementI(int[] a, int i) { return a[i]; }
|
jaroslav@1646
|
77 |
static long getElementJ(long[] a, int i) { return a[i]; }
|
jaroslav@1646
|
78 |
static float getElementF(float[] a, int i) { return a[i]; }
|
jaroslav@1646
|
79 |
static double getElementD(double[] a, int i) { return a[i]; }
|
jaroslav@1646
|
80 |
static boolean getElementZ(boolean[] a, int i) { return a[i]; }
|
jaroslav@1646
|
81 |
static byte getElementB(byte[] a, int i) { return a[i]; }
|
jaroslav@1646
|
82 |
static short getElementS(short[] a, int i) { return a[i]; }
|
jaroslav@1646
|
83 |
static char getElementC(char[] a, int i) { return a[i]; }
|
jaroslav@1646
|
84 |
static Object getElementL(Object[] a, int i) { return a[i]; }
|
jaroslav@1646
|
85 |
|
jaroslav@1646
|
86 |
static void setElementI(int[] a, int i, int x) { a[i] = x; }
|
jaroslav@1646
|
87 |
static void setElementJ(long[] a, int i, long x) { a[i] = x; }
|
jaroslav@1646
|
88 |
static void setElementF(float[] a, int i, float x) { a[i] = x; }
|
jaroslav@1646
|
89 |
static void setElementD(double[] a, int i, double x) { a[i] = x; }
|
jaroslav@1646
|
90 |
static void setElementZ(boolean[] a, int i, boolean x) { a[i] = x; }
|
jaroslav@1646
|
91 |
static void setElementB(byte[] a, int i, byte x) { a[i] = x; }
|
jaroslav@1646
|
92 |
static void setElementS(short[] a, int i, short x) { a[i] = x; }
|
jaroslav@1646
|
93 |
static void setElementC(char[] a, int i, char x) { a[i] = x; }
|
jaroslav@1646
|
94 |
static void setElementL(Object[] a, int i, Object x) { a[i] = x; }
|
jaroslav@1646
|
95 |
|
jaroslav@1646
|
96 |
static Object getElementL(Class<?> arrayClass, Object[] a, int i) { arrayClass.cast(a); return a[i]; }
|
jaroslav@1646
|
97 |
static void setElementL(Class<?> arrayClass, Object[] a, int i, Object x) { arrayClass.cast(a); a[i] = x; }
|
jaroslav@1646
|
98 |
|
jaroslav@1646
|
99 |
// Weakly typed wrappers of Object[] accessors:
|
jaroslav@1646
|
100 |
static Object getElementL(Object a, int i) { return getElementL((Object[])a, i); }
|
jaroslav@1646
|
101 |
static void setElementL(Object a, int i, Object x) { setElementL((Object[]) a, i, x); }
|
jaroslav@1646
|
102 |
static Object getElementL(Object arrayClass, Object a, int i) { return getElementL((Class<?>) arrayClass, (Object[])a, i); }
|
jaroslav@1646
|
103 |
static void setElementL(Object arrayClass, Object a, int i, Object x) { setElementL((Class<?>) arrayClass, (Object[])a, i, x); }
|
jaroslav@1646
|
104 |
|
jaroslav@1646
|
105 |
static boolean needCast(Class<?> arrayClass) {
|
jaroslav@1646
|
106 |
Class<?> elemClass = arrayClass.getComponentType();
|
jaroslav@1646
|
107 |
return !elemClass.isPrimitive() && elemClass != Object.class;
|
jaroslav@1646
|
108 |
}
|
jaroslav@1646
|
109 |
static String name(Class<?> arrayClass, boolean isSetter) {
|
jaroslav@1646
|
110 |
Class<?> elemClass = arrayClass.getComponentType();
|
jaroslav@1646
|
111 |
if (elemClass == null) throw new IllegalArgumentException();
|
jaroslav@1646
|
112 |
return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
|
jaroslav@1646
|
113 |
}
|
jaroslav@1646
|
114 |
static final boolean USE_WEAKLY_TYPED_ARRAY_ACCESSORS = false; // FIXME: decide
|
jaroslav@1646
|
115 |
static MethodType type(Class<?> arrayClass, boolean isSetter) {
|
jaroslav@1646
|
116 |
Class<?> elemClass = arrayClass.getComponentType();
|
jaroslav@1646
|
117 |
Class<?> arrayArgClass = arrayClass;
|
jaroslav@1646
|
118 |
if (!elemClass.isPrimitive()) {
|
jaroslav@1646
|
119 |
arrayArgClass = Object[].class;
|
jaroslav@1646
|
120 |
if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
|
jaroslav@1646
|
121 |
arrayArgClass = Object.class;
|
jaroslav@1646
|
122 |
}
|
jaroslav@1646
|
123 |
if (!needCast(arrayClass)) {
|
jaroslav@1646
|
124 |
return !isSetter ?
|
jaroslav@1646
|
125 |
MethodType.methodType(elemClass, arrayArgClass, int.class) :
|
jaroslav@1646
|
126 |
MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
|
jaroslav@1646
|
127 |
} else {
|
jaroslav@1646
|
128 |
Class<?> classArgClass = Class.class;
|
jaroslav@1646
|
129 |
if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
|
jaroslav@1646
|
130 |
classArgClass = Object.class;
|
jaroslav@1646
|
131 |
return !isSetter ?
|
jaroslav@1646
|
132 |
MethodType.methodType(Object.class, classArgClass, arrayArgClass, int.class) :
|
jaroslav@1646
|
133 |
MethodType.methodType(void.class, classArgClass, arrayArgClass, int.class, Object.class);
|
jaroslav@1646
|
134 |
}
|
jaroslav@1646
|
135 |
}
|
jaroslav@1646
|
136 |
static MethodType correctType(Class<?> arrayClass, boolean isSetter) {
|
jaroslav@1646
|
137 |
Class<?> elemClass = arrayClass.getComponentType();
|
jaroslav@1646
|
138 |
return !isSetter ?
|
jaroslav@1646
|
139 |
MethodType.methodType(elemClass, arrayClass, int.class) :
|
jaroslav@1646
|
140 |
MethodType.methodType(void.class, arrayClass, int.class, elemClass);
|
jaroslav@1646
|
141 |
}
|
jaroslav@1646
|
142 |
static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) {
|
jaroslav@1646
|
143 |
String name = name(arrayClass, isSetter);
|
jaroslav@1646
|
144 |
MethodType type = type(arrayClass, isSetter);
|
jaroslav@1646
|
145 |
try {
|
jaroslav@1646
|
146 |
return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
|
jaroslav@1646
|
147 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
148 |
throw uncaughtException(ex);
|
jaroslav@1646
|
149 |
}
|
jaroslav@1646
|
150 |
}
|
jaroslav@1646
|
151 |
}
|
jaroslav@1646
|
152 |
|
jaroslav@1646
|
153 |
/**
|
jaroslav@1646
|
154 |
* Create a JVM-level adapter method handle to conform the given method
|
jaroslav@1646
|
155 |
* handle to the similar newType, using only pairwise argument conversions.
|
jaroslav@1646
|
156 |
* For each argument, convert incoming argument to the exact type needed.
|
jaroslav@1646
|
157 |
* The argument conversions allowed are casting, boxing and unboxing,
|
jaroslav@1646
|
158 |
* integral widening or narrowing, and floating point widening or narrowing.
|
jaroslav@1646
|
159 |
* @param srcType required call type
|
jaroslav@1646
|
160 |
* @param target original method handle
|
jaroslav@1646
|
161 |
* @param level which strength of conversion is allowed
|
jaroslav@1646
|
162 |
* @return an adapter to the original handle with the desired new type,
|
jaroslav@1646
|
163 |
* or the original target if the types are already identical
|
jaroslav@1646
|
164 |
* or null if the adaptation cannot be made
|
jaroslav@1646
|
165 |
*/
|
jaroslav@1646
|
166 |
static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, int level) {
|
jaroslav@1646
|
167 |
assert(level >= 0 && level <= 2);
|
jaroslav@1646
|
168 |
MethodType dstType = target.type();
|
jaroslav@1646
|
169 |
assert(dstType.parameterCount() == target.type().parameterCount());
|
jaroslav@1646
|
170 |
if (srcType == dstType)
|
jaroslav@1646
|
171 |
return target;
|
jaroslav@1646
|
172 |
|
jaroslav@1646
|
173 |
// Calculate extra arguments (temporaries) required in the names array.
|
jaroslav@1646
|
174 |
// FIXME: Use an ArrayList<Name>. Some arguments require more than one conversion step.
|
jaroslav@1646
|
175 |
final int INARG_COUNT = srcType.parameterCount();
|
jaroslav@1646
|
176 |
int conversions = 0;
|
jaroslav@1646
|
177 |
boolean[] needConv = new boolean[1+INARG_COUNT];
|
jaroslav@1646
|
178 |
for (int i = 0; i <= INARG_COUNT; i++) {
|
jaroslav@1646
|
179 |
Class<?> src = (i == INARG_COUNT) ? dstType.returnType() : srcType.parameterType(i);
|
jaroslav@1646
|
180 |
Class<?> dst = (i == INARG_COUNT) ? srcType.returnType() : dstType.parameterType(i);
|
jaroslav@1646
|
181 |
if (!VerifyType.isNullConversion(src, dst) ||
|
jaroslav@1646
|
182 |
level <= 1 && dst.isInterface() && !dst.isAssignableFrom(src)) {
|
jaroslav@1646
|
183 |
needConv[i] = true;
|
jaroslav@1646
|
184 |
conversions++;
|
jaroslav@1646
|
185 |
}
|
jaroslav@1646
|
186 |
}
|
jaroslav@1646
|
187 |
boolean retConv = needConv[INARG_COUNT];
|
jaroslav@1646
|
188 |
|
jaroslav@1646
|
189 |
final int IN_MH = 0;
|
jaroslav@1646
|
190 |
final int INARG_BASE = 1;
|
jaroslav@1646
|
191 |
final int INARG_LIMIT = INARG_BASE + INARG_COUNT;
|
jaroslav@1646
|
192 |
final int NAME_LIMIT = INARG_LIMIT + conversions + 1;
|
jaroslav@1646
|
193 |
final int RETURN_CONV = (!retConv ? -1 : NAME_LIMIT - 1);
|
jaroslav@1646
|
194 |
final int OUT_CALL = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
|
jaroslav@1646
|
195 |
|
jaroslav@1646
|
196 |
// Now build a LambdaForm.
|
jaroslav@1646
|
197 |
MethodType lambdaType = srcType.basicType().invokerType();
|
jaroslav@1646
|
198 |
Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
|
jaroslav@1646
|
199 |
|
jaroslav@1646
|
200 |
// Collect the arguments to the outgoing call, maybe with conversions:
|
jaroslav@1646
|
201 |
final int OUTARG_BASE = 0; // target MH is Name.function, name Name.arguments[0]
|
jaroslav@1646
|
202 |
Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
|
jaroslav@1646
|
203 |
|
jaroslav@1646
|
204 |
int nameCursor = INARG_LIMIT;
|
jaroslav@1646
|
205 |
for (int i = 0; i < INARG_COUNT; i++) {
|
jaroslav@1646
|
206 |
Class<?> src = srcType.parameterType(i);
|
jaroslav@1646
|
207 |
Class<?> dst = dstType.parameterType(i);
|
jaroslav@1646
|
208 |
|
jaroslav@1646
|
209 |
if (!needConv[i]) {
|
jaroslav@1646
|
210 |
// do nothing: difference is trivial
|
jaroslav@1646
|
211 |
outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
|
jaroslav@1646
|
212 |
continue;
|
jaroslav@1646
|
213 |
}
|
jaroslav@1646
|
214 |
|
jaroslav@1646
|
215 |
// Tricky case analysis follows.
|
jaroslav@1646
|
216 |
MethodHandle fn = null;
|
jaroslav@1646
|
217 |
if (src.isPrimitive()) {
|
jaroslav@1646
|
218 |
if (dst.isPrimitive()) {
|
jaroslav@1646
|
219 |
fn = ValueConversions.convertPrimitive(src, dst);
|
jaroslav@1646
|
220 |
} else {
|
jaroslav@1646
|
221 |
Wrapper w = Wrapper.forPrimitiveType(src);
|
jaroslav@1646
|
222 |
MethodHandle boxMethod = ValueConversions.box(w);
|
jaroslav@1646
|
223 |
if (dst == w.wrapperType())
|
jaroslav@1646
|
224 |
fn = boxMethod;
|
jaroslav@1646
|
225 |
else
|
jaroslav@1646
|
226 |
fn = boxMethod.asType(MethodType.methodType(dst, src));
|
jaroslav@1646
|
227 |
}
|
jaroslav@1646
|
228 |
} else {
|
jaroslav@1646
|
229 |
if (dst.isPrimitive()) {
|
jaroslav@1646
|
230 |
// Caller has boxed a primitive. Unbox it for the target.
|
jaroslav@1646
|
231 |
Wrapper w = Wrapper.forPrimitiveType(dst);
|
jaroslav@1646
|
232 |
if (level == 0 || VerifyType.isNullConversion(src, w.wrapperType())) {
|
jaroslav@1646
|
233 |
fn = ValueConversions.unbox(dst);
|
jaroslav@1646
|
234 |
} else if (src == Object.class || !Wrapper.isWrapperType(src)) {
|
jaroslav@1646
|
235 |
// Examples: Object->int, Number->int, Comparable->int; Byte->int, Character->int
|
jaroslav@1646
|
236 |
// must include additional conversions
|
jaroslav@1646
|
237 |
// src must be examined at runtime, to detect Byte, Character, etc.
|
jaroslav@1646
|
238 |
MethodHandle unboxMethod = (level == 1
|
jaroslav@1646
|
239 |
? ValueConversions.unbox(dst)
|
jaroslav@1646
|
240 |
: ValueConversions.unboxCast(dst));
|
jaroslav@1646
|
241 |
fn = unboxMethod;
|
jaroslav@1646
|
242 |
} else {
|
jaroslav@1646
|
243 |
// Example: Byte->int
|
jaroslav@1646
|
244 |
// Do this by reformulating the problem to Byte->byte.
|
jaroslav@1646
|
245 |
Class<?> srcPrim = Wrapper.forWrapperType(src).primitiveType();
|
jaroslav@1646
|
246 |
MethodHandle unbox = ValueConversions.unbox(srcPrim);
|
jaroslav@1646
|
247 |
// Compose the two conversions. FIXME: should make two Names for this job
|
jaroslav@1646
|
248 |
fn = unbox.asType(MethodType.methodType(dst, src));
|
jaroslav@1646
|
249 |
}
|
jaroslav@1646
|
250 |
} else {
|
jaroslav@1646
|
251 |
// Simple reference conversion.
|
jaroslav@1646
|
252 |
// Note: Do not check for a class hierarchy relation
|
jaroslav@1646
|
253 |
// between src and dst. In all cases a 'null' argument
|
jaroslav@1646
|
254 |
// will pass the cast conversion.
|
jaroslav@1646
|
255 |
fn = ValueConversions.cast(dst);
|
jaroslav@1646
|
256 |
}
|
jaroslav@1646
|
257 |
}
|
jaroslav@1646
|
258 |
Name conv = new Name(fn, names[INARG_BASE + i]);
|
jaroslav@1646
|
259 |
assert(names[nameCursor] == null);
|
jaroslav@1646
|
260 |
names[nameCursor++] = conv;
|
jaroslav@1646
|
261 |
assert(outArgs[OUTARG_BASE + i] == null);
|
jaroslav@1646
|
262 |
outArgs[OUTARG_BASE + i] = conv;
|
jaroslav@1646
|
263 |
}
|
jaroslav@1646
|
264 |
|
jaroslav@1646
|
265 |
// Build argument array for the call.
|
jaroslav@1646
|
266 |
assert(nameCursor == OUT_CALL);
|
jaroslav@1646
|
267 |
names[OUT_CALL] = new Name(target, outArgs);
|
jaroslav@1646
|
268 |
|
jaroslav@1646
|
269 |
if (RETURN_CONV < 0) {
|
jaroslav@1646
|
270 |
assert(OUT_CALL == names.length-1);
|
jaroslav@1646
|
271 |
} else {
|
jaroslav@1646
|
272 |
Class<?> needReturn = srcType.returnType();
|
jaroslav@1646
|
273 |
Class<?> haveReturn = dstType.returnType();
|
jaroslav@1646
|
274 |
MethodHandle fn;
|
jaroslav@1646
|
275 |
Object[] arg = { names[OUT_CALL] };
|
jaroslav@1646
|
276 |
if (haveReturn == void.class) {
|
jaroslav@1646
|
277 |
// synthesize a zero value for the given void
|
jaroslav@1646
|
278 |
Object zero = Wrapper.forBasicType(needReturn).zero();
|
jaroslav@1646
|
279 |
fn = MethodHandles.constant(needReturn, zero);
|
jaroslav@1646
|
280 |
arg = new Object[0]; // don't pass names[OUT_CALL] to conversion
|
jaroslav@1646
|
281 |
} else {
|
jaroslav@1646
|
282 |
MethodHandle identity = MethodHandles.identity(needReturn);
|
jaroslav@1646
|
283 |
MethodType needConversion = identity.type().changeParameterType(0, haveReturn);
|
jaroslav@1646
|
284 |
fn = makePairwiseConvert(identity, needConversion, level);
|
jaroslav@1646
|
285 |
}
|
jaroslav@1646
|
286 |
assert(names[RETURN_CONV] == null);
|
jaroslav@1646
|
287 |
names[RETURN_CONV] = new Name(fn, arg);
|
jaroslav@1646
|
288 |
assert(RETURN_CONV == names.length-1);
|
jaroslav@1646
|
289 |
}
|
jaroslav@1646
|
290 |
|
jaroslav@1646
|
291 |
LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
292 |
return SimpleMethodHandle.make(srcType, form);
|
jaroslav@1646
|
293 |
}
|
jaroslav@1646
|
294 |
|
jaroslav@1646
|
295 |
static MethodHandle makeReferenceIdentity(Class<?> refType) {
|
jaroslav@1646
|
296 |
MethodType lambdaType = MethodType.genericMethodType(1).invokerType();
|
jaroslav@1646
|
297 |
Name[] names = arguments(1, lambdaType);
|
jaroslav@1646
|
298 |
names[names.length - 1] = new Name(ValueConversions.identity(), names[1]);
|
jaroslav@1646
|
299 |
LambdaForm form = new LambdaForm("identity", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
300 |
return SimpleMethodHandle.make(MethodType.methodType(refType, refType), form);
|
jaroslav@1646
|
301 |
}
|
jaroslav@1646
|
302 |
|
jaroslav@1646
|
303 |
static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
|
jaroslav@1646
|
304 |
MethodType type = target.type();
|
jaroslav@1646
|
305 |
int last = type.parameterCount() - 1;
|
jaroslav@1646
|
306 |
if (type.parameterType(last) != arrayType)
|
jaroslav@1646
|
307 |
target = target.asType(type.changeParameterType(last, arrayType));
|
jaroslav@1646
|
308 |
target = target.asFixedArity(); // make sure this attribute is turned off
|
jaroslav@1646
|
309 |
return new AsVarargsCollector(target, target.type(), arrayType);
|
jaroslav@1646
|
310 |
}
|
jaroslav@1646
|
311 |
|
jaroslav@1646
|
312 |
static class AsVarargsCollector extends MethodHandle {
|
jaroslav@1646
|
313 |
private final MethodHandle target;
|
jaroslav@1646
|
314 |
private final Class<?> arrayType;
|
jaroslav@1646
|
315 |
private /*@Stable*/ MethodHandle asCollectorCache;
|
jaroslav@1646
|
316 |
|
jaroslav@1646
|
317 |
AsVarargsCollector(MethodHandle target, MethodType type, Class<?> arrayType) {
|
jaroslav@1646
|
318 |
super(type, reinvokerForm(target));
|
jaroslav@1646
|
319 |
this.target = target;
|
jaroslav@1646
|
320 |
this.arrayType = arrayType;
|
jaroslav@1646
|
321 |
this.asCollectorCache = target.asCollector(arrayType, 0);
|
jaroslav@1646
|
322 |
}
|
jaroslav@1646
|
323 |
|
jaroslav@1646
|
324 |
@Override MethodHandle reinvokerTarget() { return target; }
|
jaroslav@1646
|
325 |
|
jaroslav@1646
|
326 |
@Override
|
jaroslav@1646
|
327 |
public boolean isVarargsCollector() {
|
jaroslav@1646
|
328 |
return true;
|
jaroslav@1646
|
329 |
}
|
jaroslav@1646
|
330 |
|
jaroslav@1646
|
331 |
@Override
|
jaroslav@1646
|
332 |
public MethodHandle asFixedArity() {
|
jaroslav@1646
|
333 |
return target;
|
jaroslav@1646
|
334 |
}
|
jaroslav@1646
|
335 |
|
jaroslav@1646
|
336 |
@Override
|
jaroslav@1646
|
337 |
public MethodHandle asTypeUncached(MethodType newType) {
|
jaroslav@1646
|
338 |
MethodType type = this.type();
|
jaroslav@1646
|
339 |
int collectArg = type.parameterCount() - 1;
|
jaroslav@1646
|
340 |
int newArity = newType.parameterCount();
|
jaroslav@1646
|
341 |
if (newArity == collectArg+1 &&
|
jaroslav@1646
|
342 |
type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
|
jaroslav@1646
|
343 |
// if arity and trailing parameter are compatible, do normal thing
|
jaroslav@1646
|
344 |
return asTypeCache = asFixedArity().asType(newType);
|
jaroslav@1646
|
345 |
}
|
jaroslav@1646
|
346 |
// check cache
|
jaroslav@1646
|
347 |
MethodHandle acc = asCollectorCache;
|
jaroslav@1646
|
348 |
if (acc != null && acc.type().parameterCount() == newArity)
|
jaroslav@1646
|
349 |
return asTypeCache = acc.asType(newType);
|
jaroslav@1646
|
350 |
// build and cache a collector
|
jaroslav@1646
|
351 |
int arrayLength = newArity - collectArg;
|
jaroslav@1646
|
352 |
MethodHandle collector;
|
jaroslav@1646
|
353 |
try {
|
jaroslav@1646
|
354 |
collector = asFixedArity().asCollector(arrayType, arrayLength);
|
jaroslav@1646
|
355 |
assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
|
jaroslav@1646
|
356 |
} catch (IllegalArgumentException ex) {
|
jaroslav@1646
|
357 |
throw new WrongMethodTypeException("cannot build collector", ex);
|
jaroslav@1646
|
358 |
}
|
jaroslav@1646
|
359 |
asCollectorCache = collector;
|
jaroslav@1646
|
360 |
return asTypeCache = collector.asType(newType);
|
jaroslav@1646
|
361 |
}
|
jaroslav@1646
|
362 |
|
jaroslav@1646
|
363 |
@Override
|
jaroslav@1646
|
364 |
MethodHandle setVarargs(MemberName member) {
|
jaroslav@1646
|
365 |
if (member.isVarargs()) return this;
|
jaroslav@1646
|
366 |
return asFixedArity();
|
jaroslav@1646
|
367 |
}
|
jaroslav@1646
|
368 |
|
jaroslav@1646
|
369 |
@Override
|
jaroslav@1646
|
370 |
MethodHandle viewAsType(MethodType newType) {
|
jaroslav@1646
|
371 |
if (newType.lastParameterType() != type().lastParameterType())
|
jaroslav@1646
|
372 |
throw new InternalError();
|
jaroslav@1646
|
373 |
MethodHandle newTarget = asFixedArity().viewAsType(newType);
|
jaroslav@1646
|
374 |
// put back the varargs bit:
|
jaroslav@1646
|
375 |
return new AsVarargsCollector(newTarget, newType, arrayType);
|
jaroslav@1646
|
376 |
}
|
jaroslav@1646
|
377 |
|
jaroslav@1646
|
378 |
@Override
|
jaroslav@1646
|
379 |
MemberName internalMemberName() {
|
jaroslav@1646
|
380 |
return asFixedArity().internalMemberName();
|
jaroslav@1646
|
381 |
}
|
jaroslav@1646
|
382 |
@Override
|
jaroslav@1646
|
383 |
Class<?> internalCallerClass() {
|
jaroslav@1646
|
384 |
return asFixedArity().internalCallerClass();
|
jaroslav@1646
|
385 |
}
|
jaroslav@1646
|
386 |
|
jaroslav@1646
|
387 |
/*non-public*/
|
jaroslav@1646
|
388 |
@Override
|
jaroslav@1646
|
389 |
boolean isInvokeSpecial() {
|
jaroslav@1646
|
390 |
return asFixedArity().isInvokeSpecial();
|
jaroslav@1646
|
391 |
}
|
jaroslav@1646
|
392 |
|
jaroslav@1646
|
393 |
|
jaroslav@1646
|
394 |
@Override
|
jaroslav@1646
|
395 |
MethodHandle bindArgument(int pos, char basicType, Object value) {
|
jaroslav@1646
|
396 |
return asFixedArity().bindArgument(pos, basicType, value);
|
jaroslav@1646
|
397 |
}
|
jaroslav@1646
|
398 |
|
jaroslav@1646
|
399 |
@Override
|
jaroslav@1646
|
400 |
MethodHandle bindReceiver(Object receiver) {
|
jaroslav@1646
|
401 |
return asFixedArity().bindReceiver(receiver);
|
jaroslav@1646
|
402 |
}
|
jaroslav@1646
|
403 |
|
jaroslav@1646
|
404 |
@Override
|
jaroslav@1646
|
405 |
MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
|
jaroslav@1646
|
406 |
return asFixedArity().dropArguments(srcType, pos, drops);
|
jaroslav@1646
|
407 |
}
|
jaroslav@1646
|
408 |
|
jaroslav@1646
|
409 |
@Override
|
jaroslav@1646
|
410 |
MethodHandle permuteArguments(MethodType newType, int[] reorder) {
|
jaroslav@1646
|
411 |
return asFixedArity().permuteArguments(newType, reorder);
|
jaroslav@1646
|
412 |
}
|
jaroslav@1646
|
413 |
}
|
jaroslav@1646
|
414 |
|
jaroslav@1646
|
415 |
/** Factory method: Spread selected argument. */
|
jaroslav@1646
|
416 |
static MethodHandle makeSpreadArguments(MethodHandle target,
|
jaroslav@1646
|
417 |
Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
|
jaroslav@1646
|
418 |
MethodType targetType = target.type();
|
jaroslav@1646
|
419 |
|
jaroslav@1646
|
420 |
for (int i = 0; i < spreadArgCount; i++) {
|
jaroslav@1646
|
421 |
Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
|
jaroslav@1646
|
422 |
if (arg == null) arg = Object.class;
|
jaroslav@1646
|
423 |
targetType = targetType.changeParameterType(spreadArgPos + i, arg);
|
jaroslav@1646
|
424 |
}
|
jaroslav@1646
|
425 |
target = target.asType(targetType);
|
jaroslav@1646
|
426 |
|
jaroslav@1646
|
427 |
MethodType srcType = targetType
|
jaroslav@1646
|
428 |
.replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
|
jaroslav@1646
|
429 |
// Now build a LambdaForm.
|
jaroslav@1646
|
430 |
MethodType lambdaType = srcType.invokerType();
|
jaroslav@1646
|
431 |
Name[] names = arguments(spreadArgCount + 2, lambdaType);
|
jaroslav@1646
|
432 |
int nameCursor = lambdaType.parameterCount();
|
jaroslav@1646
|
433 |
int[] indexes = new int[targetType.parameterCount()];
|
jaroslav@1646
|
434 |
|
jaroslav@1646
|
435 |
for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
|
jaroslav@1646
|
436 |
Class<?> src = lambdaType.parameterType(i);
|
jaroslav@1646
|
437 |
if (i == spreadArgPos) {
|
jaroslav@1646
|
438 |
// Spread the array.
|
jaroslav@1646
|
439 |
MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
|
jaroslav@1646
|
440 |
Name array = names[argIndex];
|
jaroslav@1646
|
441 |
names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount);
|
jaroslav@1646
|
442 |
for (int j = 0; j < spreadArgCount; i++, j++) {
|
jaroslav@1646
|
443 |
indexes[i] = nameCursor;
|
jaroslav@1646
|
444 |
names[nameCursor++] = new Name(aload, array, j);
|
jaroslav@1646
|
445 |
}
|
jaroslav@1646
|
446 |
} else if (i < indexes.length) {
|
jaroslav@1646
|
447 |
indexes[i] = argIndex;
|
jaroslav@1646
|
448 |
}
|
jaroslav@1646
|
449 |
}
|
jaroslav@1646
|
450 |
assert(nameCursor == names.length-1); // leave room for the final call
|
jaroslav@1646
|
451 |
|
jaroslav@1646
|
452 |
// Build argument array for the call.
|
jaroslav@1646
|
453 |
Name[] targetArgs = new Name[targetType.parameterCount()];
|
jaroslav@1646
|
454 |
for (int i = 0; i < targetType.parameterCount(); i++) {
|
jaroslav@1646
|
455 |
int idx = indexes[i];
|
jaroslav@1646
|
456 |
targetArgs[i] = names[idx];
|
jaroslav@1646
|
457 |
}
|
jaroslav@1646
|
458 |
names[names.length - 1] = new Name(target, (Object[]) targetArgs);
|
jaroslav@1646
|
459 |
|
jaroslav@1646
|
460 |
LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
461 |
return SimpleMethodHandle.make(srcType, form);
|
jaroslav@1646
|
462 |
}
|
jaroslav@1646
|
463 |
|
jaroslav@1646
|
464 |
static void checkSpreadArgument(Object av, int n) {
|
jaroslav@1646
|
465 |
if (av == null) {
|
jaroslav@1646
|
466 |
if (n == 0) return;
|
jaroslav@1646
|
467 |
} else if (av instanceof Object[]) {
|
jaroslav@1646
|
468 |
int len = ((Object[])av).length;
|
jaroslav@1646
|
469 |
if (len == n) return;
|
jaroslav@1646
|
470 |
} else {
|
jaroslav@1646
|
471 |
int len = java.lang.reflect.Array.getLength(av);
|
jaroslav@1646
|
472 |
if (len == n) return;
|
jaroslav@1646
|
473 |
}
|
jaroslav@1646
|
474 |
// fall through to error:
|
jaroslav@1646
|
475 |
throw newIllegalArgumentException("array is not of length "+n);
|
jaroslav@1646
|
476 |
}
|
jaroslav@1646
|
477 |
|
jaroslav@1646
|
478 |
/**
|
jaroslav@1646
|
479 |
* Pre-initialized NamedFunctions for bootstrapping purposes.
|
jaroslav@1646
|
480 |
* Factored in an inner class to delay initialization until first usage.
|
jaroslav@1646
|
481 |
*/
|
jaroslav@1646
|
482 |
private static class Lazy {
|
jaroslav@1646
|
483 |
static final NamedFunction NF_checkSpreadArgument;
|
jaroslav@1646
|
484 |
static {
|
jaroslav@1646
|
485 |
try {
|
jaroslav@1646
|
486 |
NF_checkSpreadArgument = new NamedFunction(MethodHandleImpl.class
|
jaroslav@1646
|
487 |
.getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
|
jaroslav@1646
|
488 |
NF_checkSpreadArgument.resolve();
|
jaroslav@1646
|
489 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
490 |
throw newInternalError(ex);
|
jaroslav@1646
|
491 |
}
|
jaroslav@1646
|
492 |
}
|
jaroslav@1646
|
493 |
}
|
jaroslav@1646
|
494 |
|
jaroslav@1646
|
495 |
/** Factory method: Collect or filter selected argument(s). */
|
jaroslav@1646
|
496 |
static MethodHandle makeCollectArguments(MethodHandle target,
|
jaroslav@1646
|
497 |
MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
|
jaroslav@1646
|
498 |
MethodType targetType = target.type(); // (a..., c, [b...])=>r
|
jaroslav@1646
|
499 |
MethodType collectorType = collector.type(); // (b...)=>c
|
jaroslav@1646
|
500 |
int collectArgCount = collectorType.parameterCount();
|
jaroslav@1646
|
501 |
Class<?> collectValType = collectorType.returnType();
|
jaroslav@1646
|
502 |
int collectValCount = (collectValType == void.class ? 0 : 1);
|
jaroslav@1646
|
503 |
MethodType srcType = targetType // (a..., [b...])=>r
|
jaroslav@1646
|
504 |
.dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
|
jaroslav@1646
|
505 |
if (!retainOriginalArgs) { // (a..., b...)=>r
|
jaroslav@1646
|
506 |
srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
|
jaroslav@1646
|
507 |
}
|
jaroslav@1646
|
508 |
// in arglist: [0: ...keep1 | cpos: collect... | cpos+cacount: keep2... ]
|
jaroslav@1646
|
509 |
// out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
|
jaroslav@1646
|
510 |
// out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
|
jaroslav@1646
|
511 |
|
jaroslav@1646
|
512 |
// Now build a LambdaForm.
|
jaroslav@1646
|
513 |
MethodType lambdaType = srcType.invokerType();
|
jaroslav@1646
|
514 |
Name[] names = arguments(2, lambdaType);
|
jaroslav@1646
|
515 |
final int collectNamePos = names.length - 2;
|
jaroslav@1646
|
516 |
final int targetNamePos = names.length - 1;
|
jaroslav@1646
|
517 |
|
jaroslav@1646
|
518 |
Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
|
jaroslav@1646
|
519 |
names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
|
jaroslav@1646
|
520 |
|
jaroslav@1646
|
521 |
// Build argument array for the target.
|
jaroslav@1646
|
522 |
// Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
|
jaroslav@1646
|
523 |
// Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
|
jaroslav@1646
|
524 |
Name[] targetArgs = new Name[targetType.parameterCount()];
|
jaroslav@1646
|
525 |
int inputArgPos = 1; // incoming LF args to copy to target
|
jaroslav@1646
|
526 |
int targetArgPos = 0; // fill pointer for targetArgs
|
jaroslav@1646
|
527 |
int chunk = collectArgPos; // |headArgs|
|
jaroslav@1646
|
528 |
System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
|
jaroslav@1646
|
529 |
inputArgPos += chunk;
|
jaroslav@1646
|
530 |
targetArgPos += chunk;
|
jaroslav@1646
|
531 |
if (collectValType != void.class) {
|
jaroslav@1646
|
532 |
targetArgs[targetArgPos++] = names[collectNamePos];
|
jaroslav@1646
|
533 |
}
|
jaroslav@1646
|
534 |
chunk = collectArgCount;
|
jaroslav@1646
|
535 |
if (retainOriginalArgs) {
|
jaroslav@1646
|
536 |
System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
|
jaroslav@1646
|
537 |
targetArgPos += chunk; // optionally pass on the collected chunk
|
jaroslav@1646
|
538 |
}
|
jaroslav@1646
|
539 |
inputArgPos += chunk;
|
jaroslav@1646
|
540 |
chunk = targetArgs.length - targetArgPos; // all the rest
|
jaroslav@1646
|
541 |
System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
|
jaroslav@1646
|
542 |
assert(inputArgPos + chunk == collectNamePos); // use of rest of input args also
|
jaroslav@1646
|
543 |
names[targetNamePos] = new Name(target, (Object[]) targetArgs);
|
jaroslav@1646
|
544 |
|
jaroslav@1646
|
545 |
LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
546 |
return SimpleMethodHandle.make(srcType, form);
|
jaroslav@1646
|
547 |
}
|
jaroslav@1646
|
548 |
|
jaroslav@1646
|
549 |
static
|
jaroslav@1646
|
550 |
MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
|
jaroslav@1646
|
551 |
return testResult ? target : fallback;
|
jaroslav@1646
|
552 |
}
|
jaroslav@1646
|
553 |
|
jaroslav@1646
|
554 |
static MethodHandle SELECT_ALTERNATIVE;
|
jaroslav@1646
|
555 |
static MethodHandle selectAlternative() {
|
jaroslav@1646
|
556 |
if (SELECT_ALTERNATIVE != null) return SELECT_ALTERNATIVE;
|
jaroslav@1646
|
557 |
try {
|
jaroslav@1646
|
558 |
SELECT_ALTERNATIVE
|
jaroslav@1646
|
559 |
= IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
|
jaroslav@1646
|
560 |
MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
|
jaroslav@1646
|
561 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
562 |
throw new RuntimeException(ex);
|
jaroslav@1646
|
563 |
}
|
jaroslav@1646
|
564 |
return SELECT_ALTERNATIVE;
|
jaroslav@1646
|
565 |
}
|
jaroslav@1646
|
566 |
|
jaroslav@1646
|
567 |
static
|
jaroslav@1646
|
568 |
MethodHandle makeGuardWithTest(MethodHandle test,
|
jaroslav@1646
|
569 |
MethodHandle target,
|
jaroslav@1646
|
570 |
MethodHandle fallback) {
|
jaroslav@1646
|
571 |
MethodType basicType = target.type().basicType();
|
jaroslav@1646
|
572 |
MethodHandle invokeBasic = MethodHandles.basicInvoker(basicType);
|
jaroslav@1646
|
573 |
int arity = basicType.parameterCount();
|
jaroslav@1646
|
574 |
int extraNames = 3;
|
jaroslav@1646
|
575 |
MethodType lambdaType = basicType.invokerType();
|
jaroslav@1646
|
576 |
Name[] names = arguments(extraNames, lambdaType);
|
jaroslav@1646
|
577 |
|
jaroslav@1646
|
578 |
Object[] testArgs = Arrays.copyOfRange(names, 1, 1 + arity, Object[].class);
|
jaroslav@1646
|
579 |
Object[] targetArgs = Arrays.copyOfRange(names, 0, 1 + arity, Object[].class);
|
jaroslav@1646
|
580 |
|
jaroslav@1646
|
581 |
// call test
|
jaroslav@1646
|
582 |
names[arity + 1] = new Name(test, testArgs);
|
jaroslav@1646
|
583 |
|
jaroslav@1646
|
584 |
// call selectAlternative
|
jaroslav@1646
|
585 |
Object[] selectArgs = { names[arity + 1], target, fallback };
|
jaroslav@1646
|
586 |
names[arity + 2] = new Name(MethodHandleImpl.selectAlternative(), selectArgs);
|
jaroslav@1646
|
587 |
targetArgs[0] = names[arity + 2];
|
jaroslav@1646
|
588 |
|
jaroslav@1646
|
589 |
// call target or fallback
|
jaroslav@1646
|
590 |
names[arity + 3] = new Name(new NamedFunction(invokeBasic), targetArgs);
|
jaroslav@1646
|
591 |
|
jaroslav@1646
|
592 |
LambdaForm form = new LambdaForm("guard", lambdaType.parameterCount(), names);
|
jaroslav@1646
|
593 |
return SimpleMethodHandle.make(target.type(), form);
|
jaroslav@1646
|
594 |
}
|
jaroslav@1646
|
595 |
|
jaroslav@1646
|
596 |
private static class GuardWithCatch {
|
jaroslav@1646
|
597 |
private final MethodHandle target;
|
jaroslav@1646
|
598 |
private final Class<? extends Throwable> exType;
|
jaroslav@1646
|
599 |
private final MethodHandle catcher;
|
jaroslav@1646
|
600 |
// FIXME: Build the control flow out of foldArguments.
|
jaroslav@1646
|
601 |
GuardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher) {
|
jaroslav@1646
|
602 |
this.target = target;
|
jaroslav@1646
|
603 |
this.exType = exType;
|
jaroslav@1646
|
604 |
this.catcher = catcher;
|
jaroslav@1646
|
605 |
}
|
jaroslav@1646
|
606 |
@LambdaForm.Hidden
|
jaroslav@1646
|
607 |
private Object invoke_V(Object... av) throws Throwable {
|
jaroslav@1646
|
608 |
try {
|
jaroslav@1646
|
609 |
return target.invokeExact(av);
|
jaroslav@1646
|
610 |
} catch (Throwable t) {
|
jaroslav@1646
|
611 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
612 |
return catcher.invokeExact(t, av);
|
jaroslav@1646
|
613 |
}
|
jaroslav@1646
|
614 |
}
|
jaroslav@1646
|
615 |
@LambdaForm.Hidden
|
jaroslav@1646
|
616 |
private Object invoke_L0() throws Throwable {
|
jaroslav@1646
|
617 |
try {
|
jaroslav@1646
|
618 |
return target.invokeExact();
|
jaroslav@1646
|
619 |
} catch (Throwable t) {
|
jaroslav@1646
|
620 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
621 |
return catcher.invokeExact(t);
|
jaroslav@1646
|
622 |
}
|
jaroslav@1646
|
623 |
}
|
jaroslav@1646
|
624 |
@LambdaForm.Hidden
|
jaroslav@1646
|
625 |
private Object invoke_L1(Object a0) throws Throwable {
|
jaroslav@1646
|
626 |
try {
|
jaroslav@1646
|
627 |
return target.invokeExact(a0);
|
jaroslav@1646
|
628 |
} catch (Throwable t) {
|
jaroslav@1646
|
629 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
630 |
return catcher.invokeExact(t, a0);
|
jaroslav@1646
|
631 |
}
|
jaroslav@1646
|
632 |
}
|
jaroslav@1646
|
633 |
@LambdaForm.Hidden
|
jaroslav@1646
|
634 |
private Object invoke_L2(Object a0, Object a1) throws Throwable {
|
jaroslav@1646
|
635 |
try {
|
jaroslav@1646
|
636 |
return target.invokeExact(a0, a1);
|
jaroslav@1646
|
637 |
} catch (Throwable t) {
|
jaroslav@1646
|
638 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
639 |
return catcher.invokeExact(t, a0, a1);
|
jaroslav@1646
|
640 |
}
|
jaroslav@1646
|
641 |
}
|
jaroslav@1646
|
642 |
@LambdaForm.Hidden
|
jaroslav@1646
|
643 |
private Object invoke_L3(Object a0, Object a1, Object a2) throws Throwable {
|
jaroslav@1646
|
644 |
try {
|
jaroslav@1646
|
645 |
return target.invokeExact(a0, a1, a2);
|
jaroslav@1646
|
646 |
} catch (Throwable t) {
|
jaroslav@1646
|
647 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
648 |
return catcher.invokeExact(t, a0, a1, a2);
|
jaroslav@1646
|
649 |
}
|
jaroslav@1646
|
650 |
}
|
jaroslav@1646
|
651 |
@LambdaForm.Hidden
|
jaroslav@1646
|
652 |
private Object invoke_L4(Object a0, Object a1, Object a2, Object a3) throws Throwable {
|
jaroslav@1646
|
653 |
try {
|
jaroslav@1646
|
654 |
return target.invokeExact(a0, a1, a2, a3);
|
jaroslav@1646
|
655 |
} catch (Throwable t) {
|
jaroslav@1646
|
656 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
657 |
return catcher.invokeExact(t, a0, a1, a2, a3);
|
jaroslav@1646
|
658 |
}
|
jaroslav@1646
|
659 |
}
|
jaroslav@1646
|
660 |
@LambdaForm.Hidden
|
jaroslav@1646
|
661 |
private Object invoke_L5(Object a0, Object a1, Object a2, Object a3, Object a4) throws Throwable {
|
jaroslav@1646
|
662 |
try {
|
jaroslav@1646
|
663 |
return target.invokeExact(a0, a1, a2, a3, a4);
|
jaroslav@1646
|
664 |
} catch (Throwable t) {
|
jaroslav@1646
|
665 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
666 |
return catcher.invokeExact(t, a0, a1, a2, a3, a4);
|
jaroslav@1646
|
667 |
}
|
jaroslav@1646
|
668 |
}
|
jaroslav@1646
|
669 |
@LambdaForm.Hidden
|
jaroslav@1646
|
670 |
private Object invoke_L6(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5) throws Throwable {
|
jaroslav@1646
|
671 |
try {
|
jaroslav@1646
|
672 |
return target.invokeExact(a0, a1, a2, a3, a4, a5);
|
jaroslav@1646
|
673 |
} catch (Throwable t) {
|
jaroslav@1646
|
674 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
675 |
return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5);
|
jaroslav@1646
|
676 |
}
|
jaroslav@1646
|
677 |
}
|
jaroslav@1646
|
678 |
@LambdaForm.Hidden
|
jaroslav@1646
|
679 |
private Object invoke_L7(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) throws Throwable {
|
jaroslav@1646
|
680 |
try {
|
jaroslav@1646
|
681 |
return target.invokeExact(a0, a1, a2, a3, a4, a5, a6);
|
jaroslav@1646
|
682 |
} catch (Throwable t) {
|
jaroslav@1646
|
683 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
684 |
return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5, a6);
|
jaroslav@1646
|
685 |
}
|
jaroslav@1646
|
686 |
}
|
jaroslav@1646
|
687 |
@LambdaForm.Hidden
|
jaroslav@1646
|
688 |
private Object invoke_L8(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) throws Throwable {
|
jaroslav@1646
|
689 |
try {
|
jaroslav@1646
|
690 |
return target.invokeExact(a0, a1, a2, a3, a4, a5, a6, a7);
|
jaroslav@1646
|
691 |
} catch (Throwable t) {
|
jaroslav@1646
|
692 |
if (!exType.isInstance(t)) throw t;
|
jaroslav@1646
|
693 |
return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5, a6, a7);
|
jaroslav@1646
|
694 |
}
|
jaroslav@1646
|
695 |
}
|
jaroslav@1646
|
696 |
static MethodHandle[] makeInvokes() {
|
jaroslav@1646
|
697 |
ArrayList<MethodHandle> invokes = new ArrayList<>();
|
jaroslav@1646
|
698 |
MethodHandles.Lookup lookup = IMPL_LOOKUP;
|
jaroslav@1646
|
699 |
for (;;) {
|
jaroslav@1646
|
700 |
int nargs = invokes.size();
|
jaroslav@1646
|
701 |
String name = "invoke_L"+nargs;
|
jaroslav@1646
|
702 |
MethodHandle invoke = null;
|
jaroslav@1646
|
703 |
try {
|
jaroslav@1646
|
704 |
invoke = lookup.findVirtual(GuardWithCatch.class, name, MethodType.genericMethodType(nargs));
|
jaroslav@1646
|
705 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
706 |
}
|
jaroslav@1646
|
707 |
if (invoke == null) break;
|
jaroslav@1646
|
708 |
invokes.add(invoke);
|
jaroslav@1646
|
709 |
}
|
jaroslav@1646
|
710 |
assert(invokes.size() == 9); // current number of methods
|
jaroslav@1646
|
711 |
return invokes.toArray(new MethodHandle[0]);
|
jaroslav@1646
|
712 |
};
|
jaroslav@1646
|
713 |
static final MethodHandle[] INVOKES = makeInvokes();
|
jaroslav@1646
|
714 |
// For testing use this:
|
jaroslav@1646
|
715 |
//static final MethodHandle[] INVOKES = Arrays.copyOf(makeInvokes(), 2);
|
jaroslav@1646
|
716 |
static final MethodHandle VARARGS_INVOKE;
|
jaroslav@1646
|
717 |
static {
|
jaroslav@1646
|
718 |
try {
|
jaroslav@1646
|
719 |
VARARGS_INVOKE = IMPL_LOOKUP.findVirtual(GuardWithCatch.class, "invoke_V", MethodType.genericMethodType(0, true));
|
jaroslav@1646
|
720 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
721 |
throw uncaughtException(ex);
|
jaroslav@1646
|
722 |
}
|
jaroslav@1646
|
723 |
}
|
jaroslav@1646
|
724 |
}
|
jaroslav@1646
|
725 |
|
jaroslav@1646
|
726 |
|
jaroslav@1646
|
727 |
static
|
jaroslav@1646
|
728 |
MethodHandle makeGuardWithCatch(MethodHandle target,
|
jaroslav@1646
|
729 |
Class<? extends Throwable> exType,
|
jaroslav@1646
|
730 |
MethodHandle catcher) {
|
jaroslav@1646
|
731 |
MethodType type = target.type();
|
jaroslav@1646
|
732 |
MethodType ctype = catcher.type();
|
jaroslav@1646
|
733 |
int nargs = type.parameterCount();
|
jaroslav@1646
|
734 |
if (nargs < GuardWithCatch.INVOKES.length) {
|
jaroslav@1646
|
735 |
MethodType gtype = type.generic();
|
jaroslav@1646
|
736 |
MethodType gcatchType = gtype.insertParameterTypes(0, Throwable.class);
|
jaroslav@1646
|
737 |
// Note: convertArguments(...2) avoids interface casts present in convertArguments(...0)
|
jaroslav@1646
|
738 |
MethodHandle gtarget = makePairwiseConvert(target, gtype, 2);
|
jaroslav@1646
|
739 |
MethodHandle gcatcher = makePairwiseConvert(catcher, gcatchType, 2);
|
jaroslav@1646
|
740 |
GuardWithCatch gguard = new GuardWithCatch(gtarget, exType, gcatcher);
|
jaroslav@1646
|
741 |
if (gtarget == null || gcatcher == null) throw new InternalError();
|
jaroslav@1646
|
742 |
MethodHandle ginvoker = GuardWithCatch.INVOKES[nargs].bindReceiver(gguard);
|
jaroslav@1646
|
743 |
return makePairwiseConvert(ginvoker, type, 2);
|
jaroslav@1646
|
744 |
} else {
|
jaroslav@1646
|
745 |
target = target.asType(type.changeReturnType(Object.class));
|
jaroslav@1646
|
746 |
MethodHandle gtarget = makeSpreadArguments(target, Object[].class, 0, nargs);
|
jaroslav@1646
|
747 |
MethodType catcherType = ctype.changeParameterType(0, Throwable.class)
|
jaroslav@1646
|
748 |
.changeReturnType(Object.class);
|
jaroslav@1646
|
749 |
catcher = catcher.asType(catcherType);
|
jaroslav@1646
|
750 |
MethodHandle gcatcher = makeSpreadArguments(catcher, Object[].class, 1, nargs);
|
jaroslav@1646
|
751 |
GuardWithCatch gguard = new GuardWithCatch(gtarget, exType, gcatcher);
|
jaroslav@1646
|
752 |
if (gtarget == null || gcatcher == null) throw new InternalError();
|
jaroslav@1646
|
753 |
MethodHandle ginvoker = GuardWithCatch.VARARGS_INVOKE.bindReceiver(gguard);
|
jaroslav@1646
|
754 |
MethodHandle gcollect = makeCollectArguments(ginvoker, ValueConversions.varargsArray(nargs), 0, false);
|
jaroslav@1646
|
755 |
return makePairwiseConvert(gcollect, type, 2);
|
jaroslav@1646
|
756 |
}
|
jaroslav@1646
|
757 |
}
|
jaroslav@1646
|
758 |
|
jaroslav@1646
|
759 |
static
|
jaroslav@1646
|
760 |
MethodHandle throwException(MethodType type) {
|
jaroslav@1646
|
761 |
assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
|
jaroslav@1646
|
762 |
int arity = type.parameterCount();
|
jaroslav@1646
|
763 |
if (arity > 1) {
|
jaroslav@1646
|
764 |
return throwException(type.dropParameterTypes(1, arity)).dropArguments(type, 1, arity-1);
|
jaroslav@1646
|
765 |
}
|
jaroslav@1646
|
766 |
return makePairwiseConvert(throwException(), type, 2);
|
jaroslav@1646
|
767 |
}
|
jaroslav@1646
|
768 |
|
jaroslav@1646
|
769 |
static MethodHandle THROW_EXCEPTION;
|
jaroslav@1646
|
770 |
static MethodHandle throwException() {
|
jaroslav@1646
|
771 |
MethodHandle mh = THROW_EXCEPTION;
|
jaroslav@1646
|
772 |
if (mh != null) return mh;
|
jaroslav@1646
|
773 |
try {
|
jaroslav@1646
|
774 |
mh
|
jaroslav@1646
|
775 |
= IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "throwException",
|
jaroslav@1646
|
776 |
MethodType.methodType(Empty.class, Throwable.class));
|
jaroslav@1646
|
777 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
778 |
throw new RuntimeException(ex);
|
jaroslav@1646
|
779 |
}
|
jaroslav@1646
|
780 |
THROW_EXCEPTION = mh;
|
jaroslav@1646
|
781 |
return mh;
|
jaroslav@1646
|
782 |
}
|
jaroslav@1646
|
783 |
static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
|
jaroslav@1646
|
784 |
|
jaroslav@1646
|
785 |
static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
|
jaroslav@1646
|
786 |
static MethodHandle fakeMethodHandleInvoke(MemberName method) {
|
jaroslav@1646
|
787 |
int idx;
|
jaroslav@1646
|
788 |
assert(method.isMethodHandleInvoke());
|
jaroslav@1646
|
789 |
switch (method.getName()) {
|
jaroslav@1646
|
790 |
case "invoke": idx = 0; break;
|
jaroslav@1646
|
791 |
case "invokeExact": idx = 1; break;
|
jaroslav@1646
|
792 |
default: throw new InternalError(method.getName());
|
jaroslav@1646
|
793 |
}
|
jaroslav@1646
|
794 |
MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
|
jaroslav@1646
|
795 |
if (mh != null) return mh;
|
jaroslav@1646
|
796 |
MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
|
jaroslav@1646
|
797 |
MethodHandle.class, Object[].class);
|
jaroslav@1646
|
798 |
mh = throwException(type);
|
jaroslav@1646
|
799 |
mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
|
jaroslav@1646
|
800 |
if (!method.getInvocationType().equals(mh.type()))
|
jaroslav@1646
|
801 |
throw new InternalError(method.toString());
|
jaroslav@1646
|
802 |
mh = mh.withInternalMemberName(method);
|
jaroslav@1646
|
803 |
mh = mh.asVarargsCollector(Object[].class);
|
jaroslav@1646
|
804 |
assert(method.isVarargs());
|
jaroslav@1646
|
805 |
FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
|
jaroslav@1646
|
806 |
return mh;
|
jaroslav@1646
|
807 |
}
|
jaroslav@1646
|
808 |
|
jaroslav@1646
|
809 |
/**
|
jaroslav@1646
|
810 |
* Create an alias for the method handle which, when called,
|
jaroslav@1646
|
811 |
* appears to be called from the same class loader and protection domain
|
jaroslav@1646
|
812 |
* as hostClass.
|
jaroslav@1646
|
813 |
* This is an expensive no-op unless the method which is called
|
jaroslav@1646
|
814 |
* is sensitive to its caller. A small number of system methods
|
jaroslav@1646
|
815 |
* are in this category, including Class.forName and Method.invoke.
|
jaroslav@1646
|
816 |
*/
|
jaroslav@1646
|
817 |
static
|
jaroslav@1646
|
818 |
MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
|
jaroslav@1646
|
819 |
return BindCaller.bindCaller(mh, hostClass);
|
jaroslav@1646
|
820 |
}
|
jaroslav@1646
|
821 |
|
jaroslav@1646
|
822 |
// Put the whole mess into its own nested class.
|
jaroslav@1646
|
823 |
// That way we can lazily load the code and set up the constants.
|
jaroslav@1646
|
824 |
private static class BindCaller {
|
jaroslav@1646
|
825 |
static
|
jaroslav@1646
|
826 |
MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
|
jaroslav@1646
|
827 |
// Do not use this function to inject calls into system classes.
|
jaroslav@1646
|
828 |
if (hostClass == null
|
jaroslav@1646
|
829 |
|| (hostClass.isArray() ||
|
jaroslav@1646
|
830 |
hostClass.isPrimitive() ||
|
jaroslav@1646
|
831 |
hostClass.getName().startsWith("java.") ||
|
jaroslav@1646
|
832 |
hostClass.getName().startsWith("sun."))) {
|
jaroslav@1646
|
833 |
throw new InternalError(); // does not happen, and should not anyway
|
jaroslav@1646
|
834 |
}
|
jaroslav@1646
|
835 |
// For simplicity, convert mh to a varargs-like method.
|
jaroslav@1646
|
836 |
MethodHandle vamh = prepareForInvoker(mh);
|
jaroslav@1646
|
837 |
// Cache the result of makeInjectedInvoker once per argument class.
|
jaroslav@1646
|
838 |
MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
|
jaroslav@1646
|
839 |
return restoreToType(bccInvoker.bindTo(vamh), mh.type(), mh.internalMemberName(), hostClass);
|
jaroslav@1646
|
840 |
}
|
jaroslav@1646
|
841 |
|
jaroslav@1646
|
842 |
private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
|
jaroslav@1651
|
843 |
throw new IllegalStateException("Implement me too!");
|
jaroslav@1651
|
844 |
/*
|
jaroslav@1646
|
845 |
Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
|
jaroslav@1646
|
846 |
if (hostClass.getClassLoader() != bcc.getClassLoader())
|
jaroslav@1646
|
847 |
throw new InternalError(hostClass.getName()+" (CL)");
|
jaroslav@1646
|
848 |
try {
|
jaroslav@1646
|
849 |
if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
|
jaroslav@1646
|
850 |
throw new InternalError(hostClass.getName()+" (PD)");
|
jaroslav@1646
|
851 |
} catch (SecurityException ex) {
|
jaroslav@1646
|
852 |
// Self-check was blocked by security manager. This is OK.
|
jaroslav@1646
|
853 |
// In fact the whole try body could be turned into an assertion.
|
jaroslav@1646
|
854 |
}
|
jaroslav@1646
|
855 |
try {
|
jaroslav@1646
|
856 |
MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
|
jaroslav@1646
|
857 |
init.invokeExact(); // force initialization of the class
|
jaroslav@1646
|
858 |
} catch (Throwable ex) {
|
jaroslav@1646
|
859 |
throw uncaughtException(ex);
|
jaroslav@1646
|
860 |
}
|
jaroslav@1646
|
861 |
MethodHandle bccInvoker;
|
jaroslav@1646
|
862 |
try {
|
jaroslav@1646
|
863 |
MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
|
jaroslav@1646
|
864 |
bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
|
jaroslav@1646
|
865 |
} catch (ReflectiveOperationException ex) {
|
jaroslav@1646
|
866 |
throw uncaughtException(ex);
|
jaroslav@1646
|
867 |
}
|
jaroslav@1646
|
868 |
// Test the invoker, to ensure that it really injects into the right place.
|
jaroslav@1646
|
869 |
try {
|
jaroslav@1646
|
870 |
MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
|
jaroslav@1646
|
871 |
Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
|
jaroslav@1646
|
872 |
} catch (Throwable ex) {
|
jaroslav@1646
|
873 |
throw new InternalError(ex);
|
jaroslav@1646
|
874 |
}
|
jaroslav@1646
|
875 |
return bccInvoker;
|
jaroslav@1651
|
876 |
*/
|
jaroslav@1646
|
877 |
}
|
jaroslav@1646
|
878 |
private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
|
jaroslav@1646
|
879 |
@Override protected MethodHandle computeValue(Class<?> hostClass) {
|
jaroslav@1646
|
880 |
return makeInjectedInvoker(hostClass);
|
jaroslav@1646
|
881 |
}
|
jaroslav@1646
|
882 |
};
|
jaroslav@1646
|
883 |
|
jaroslav@1646
|
884 |
// Adapt mh so that it can be called directly from an injected invoker:
|
jaroslav@1646
|
885 |
private static MethodHandle prepareForInvoker(MethodHandle mh) {
|
jaroslav@1646
|
886 |
mh = mh.asFixedArity();
|
jaroslav@1646
|
887 |
MethodType mt = mh.type();
|
jaroslav@1646
|
888 |
int arity = mt.parameterCount();
|
jaroslav@1646
|
889 |
MethodHandle vamh = mh.asType(mt.generic());
|
jaroslav@1646
|
890 |
vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames
|
jaroslav@1646
|
891 |
vamh = vamh.asSpreader(Object[].class, arity);
|
jaroslav@1646
|
892 |
vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames
|
jaroslav@1646
|
893 |
return vamh;
|
jaroslav@1646
|
894 |
}
|
jaroslav@1646
|
895 |
|
jaroslav@1646
|
896 |
// Undo the adapter effect of prepareForInvoker:
|
jaroslav@1646
|
897 |
private static MethodHandle restoreToType(MethodHandle vamh, MethodType type,
|
jaroslav@1646
|
898 |
MemberName member,
|
jaroslav@1646
|
899 |
Class<?> hostClass) {
|
jaroslav@1646
|
900 |
MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
|
jaroslav@1646
|
901 |
mh = mh.asType(type);
|
jaroslav@1646
|
902 |
mh = new WrappedMember(mh, type, member, hostClass);
|
jaroslav@1646
|
903 |
return mh;
|
jaroslav@1646
|
904 |
}
|
jaroslav@1646
|
905 |
|
jaroslav@1646
|
906 |
private static final MethodHandle MH_checkCallerClass;
|
jaroslav@1646
|
907 |
static {
|
jaroslav@1646
|
908 |
final Class<?> THIS_CLASS = BindCaller.class;
|
jaroslav@1651
|
909 |
// assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
|
jaroslav@1646
|
910 |
try {
|
jaroslav@1646
|
911 |
MH_checkCallerClass = IMPL_LOOKUP
|
jaroslav@1646
|
912 |
.findStatic(THIS_CLASS, "checkCallerClass",
|
jaroslav@1646
|
913 |
MethodType.methodType(boolean.class, Class.class, Class.class));
|
jaroslav@1646
|
914 |
assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
|
jaroslav@1646
|
915 |
} catch (Throwable ex) {
|
jaroslav@1646
|
916 |
throw new InternalError(ex);
|
jaroslav@1646
|
917 |
}
|
jaroslav@1646
|
918 |
}
|
jaroslav@1646
|
919 |
|
jaroslav@1651
|
920 |
// @CallerSensitive
|
jaroslav@1651
|
921 |
// private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
|
jaroslav@1651
|
922 |
// // This method is called via MH_checkCallerClass and so it's
|
jaroslav@1651
|
923 |
// // correct to ask for the immediate caller here.
|
jaroslav@1651
|
924 |
// Class<?> actual = Reflection.getCallerClass();
|
jaroslav@1651
|
925 |
// if (actual != expected && actual != expected2)
|
jaroslav@1651
|
926 |
// throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
|
jaroslav@1651
|
927 |
// +(expected == expected2 ? "" : ", or else "+expected2.getName()));
|
jaroslav@1651
|
928 |
// return true;
|
jaroslav@1651
|
929 |
// }
|
jaroslav@1646
|
930 |
|
jaroslav@1646
|
931 |
private static final byte[] T_BYTES;
|
jaroslav@1646
|
932 |
static {
|
jaroslav@1646
|
933 |
final Object[] values = {null};
|
jaroslav@1646
|
934 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
jaroslav@1646
|
935 |
public Void run() {
|
jaroslav@1646
|
936 |
try {
|
jaroslav@1646
|
937 |
Class<T> tClass = T.class;
|
jaroslav@1646
|
938 |
String tName = tClass.getName();
|
jaroslav@1646
|
939 |
String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
|
jaroslav@1646
|
940 |
java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
|
jaroslav@1646
|
941 |
int len = uconn.getContentLength();
|
jaroslav@1646
|
942 |
byte[] bytes = new byte[len];
|
jaroslav@1646
|
943 |
try (java.io.InputStream str = uconn.getInputStream()) {
|
jaroslav@1646
|
944 |
int nr = str.read(bytes);
|
jaroslav@1646
|
945 |
if (nr != len) throw new java.io.IOException(tResource);
|
jaroslav@1646
|
946 |
}
|
jaroslav@1646
|
947 |
values[0] = bytes;
|
jaroslav@1646
|
948 |
} catch (java.io.IOException ex) {
|
jaroslav@1646
|
949 |
throw new InternalError(ex);
|
jaroslav@1646
|
950 |
}
|
jaroslav@1646
|
951 |
return null;
|
jaroslav@1646
|
952 |
}
|
jaroslav@1646
|
953 |
});
|
jaroslav@1646
|
954 |
T_BYTES = (byte[]) values[0];
|
jaroslav@1646
|
955 |
}
|
jaroslav@1646
|
956 |
|
jaroslav@1646
|
957 |
// The following class is used as a template for Unsafe.defineAnonymousClass:
|
jaroslav@1646
|
958 |
private static class T {
|
jaroslav@1646
|
959 |
static void init() { } // side effect: initializes this class
|
jaroslav@1646
|
960 |
static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
|
jaroslav@1646
|
961 |
return vamh.invokeExact(args);
|
jaroslav@1646
|
962 |
}
|
jaroslav@1646
|
963 |
}
|
jaroslav@1646
|
964 |
}
|
jaroslav@1646
|
965 |
|
jaroslav@1646
|
966 |
|
jaroslav@1646
|
967 |
/** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
|
jaroslav@1646
|
968 |
static class WrappedMember extends MethodHandle {
|
jaroslav@1646
|
969 |
private final MethodHandle target;
|
jaroslav@1646
|
970 |
private final MemberName member;
|
jaroslav@1646
|
971 |
private final Class<?> callerClass;
|
jaroslav@1646
|
972 |
|
jaroslav@1646
|
973 |
private WrappedMember(MethodHandle target, MethodType type, MemberName member, Class<?> callerClass) {
|
jaroslav@1646
|
974 |
super(type, reinvokerForm(target));
|
jaroslav@1646
|
975 |
this.target = target;
|
jaroslav@1646
|
976 |
this.member = member;
|
jaroslav@1646
|
977 |
this.callerClass = callerClass;
|
jaroslav@1646
|
978 |
}
|
jaroslav@1646
|
979 |
|
jaroslav@1646
|
980 |
@Override
|
jaroslav@1646
|
981 |
MethodHandle reinvokerTarget() {
|
jaroslav@1646
|
982 |
return target;
|
jaroslav@1646
|
983 |
}
|
jaroslav@1646
|
984 |
@Override
|
jaroslav@1646
|
985 |
public MethodHandle asTypeUncached(MethodType newType) {
|
jaroslav@1646
|
986 |
// This MH is an alias for target, except for the MemberName
|
jaroslav@1646
|
987 |
// Drop the MemberName if there is any conversion.
|
jaroslav@1646
|
988 |
return asTypeCache = target.asType(newType);
|
jaroslav@1646
|
989 |
}
|
jaroslav@1646
|
990 |
@Override
|
jaroslav@1646
|
991 |
MemberName internalMemberName() {
|
jaroslav@1646
|
992 |
return member;
|
jaroslav@1646
|
993 |
}
|
jaroslav@1646
|
994 |
@Override
|
jaroslav@1646
|
995 |
Class<?> internalCallerClass() {
|
jaroslav@1646
|
996 |
return callerClass;
|
jaroslav@1646
|
997 |
}
|
jaroslav@1646
|
998 |
@Override
|
jaroslav@1646
|
999 |
boolean isInvokeSpecial() {
|
jaroslav@1646
|
1000 |
return target.isInvokeSpecial();
|
jaroslav@1646
|
1001 |
}
|
jaroslav@1646
|
1002 |
@Override
|
jaroslav@1646
|
1003 |
MethodHandle viewAsType(MethodType newType) {
|
jaroslav@1646
|
1004 |
return new WrappedMember(target, newType, member, callerClass);
|
jaroslav@1646
|
1005 |
}
|
jaroslav@1646
|
1006 |
}
|
jaroslav@1646
|
1007 |
|
jaroslav@1646
|
1008 |
static MethodHandle makeWrappedMember(MethodHandle target, MemberName member) {
|
jaroslav@1646
|
1009 |
if (member.equals(target.internalMemberName()))
|
jaroslav@1646
|
1010 |
return target;
|
jaroslav@1646
|
1011 |
return new WrappedMember(target, target.type(), member, null);
|
jaroslav@1646
|
1012 |
}
|
jaroslav@1646
|
1013 |
|
jaroslav@1646
|
1014 |
}
|