author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 47280 | 95192765a858 |
permissions | -rw-r--r-- |
26472 | 1 |
/* |
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package java.lang.invoke; |
|
27 |
||
28 |
import java.util.Arrays; |
|
29 |
import static java.lang.invoke.LambdaForm.*; |
|
40408
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
30 |
import static java.lang.invoke.LambdaForm.Kind.*; |
47280
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
31 |
import static java.lang.invoke.MethodHandleNatives.Constants.REF_invokeVirtual; |
26472 | 32 |
import static java.lang.invoke.MethodHandleStatics.*; |
33 |
||
34 |
/** |
|
35 |
* A method handle whose invocation behavior is determined by a target. |
|
36 |
* The delegating MH itself can hold extra "intentions" beyond the simple behavior. |
|
37 |
* @author jrose |
|
38 |
*/ |
|
39 |
/*non-public*/ |
|
40 |
abstract class DelegatingMethodHandle extends MethodHandle { |
|
41 |
protected DelegatingMethodHandle(MethodHandle target) { |
|
42 |
this(target.type(), target); |
|
43 |
} |
|
44 |
||
45 |
protected DelegatingMethodHandle(MethodType type, MethodHandle target) { |
|
46 |
super(type, chooseDelegatingForm(target)); |
|
47 |
} |
|
48 |
||
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
49 |
protected DelegatingMethodHandle(MethodType type, LambdaForm form) { |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
50 |
super(type, form); |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
51 |
} |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
52 |
|
26472 | 53 |
/** Define this to extract the delegated target which supplies the invocation behavior. */ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
27295
diff
changeset
|
54 |
protected abstract MethodHandle getTarget(); |
26472 | 55 |
|
56 |
@Override |
|
57 |
abstract MethodHandle asTypeUncached(MethodType newType); |
|
58 |
||
59 |
@Override |
|
60 |
MemberName internalMemberName() { |
|
61 |
return getTarget().internalMemberName(); |
|
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
boolean isInvokeSpecial() { |
|
66 |
return getTarget().isInvokeSpecial(); |
|
67 |
} |
|
68 |
||
69 |
@Override |
|
70 |
Class<?> internalCallerClass() { |
|
71 |
return getTarget().internalCallerClass(); |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
MethodHandle copyWith(MethodType mt, LambdaForm lf) { |
|
76 |
// FIXME: rethink 'copyWith' protocol; it is too low-level for use on all MHs |
|
77 |
throw newIllegalArgumentException("do not use this"); |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
String internalProperties() { |
|
82 |
return "\n& Class="+getClass().getSimpleName()+ |
|
83 |
"\n& Target="+getTarget().debugString(); |
|
84 |
} |
|
85 |
||
86 |
@Override |
|
87 |
BoundMethodHandle rebind() { |
|
88 |
return getTarget().rebind(); |
|
89 |
} |
|
90 |
||
91 |
private static LambdaForm chooseDelegatingForm(MethodHandle target) { |
|
92 |
if (target instanceof SimpleMethodHandle) |
|
93 |
return target.internalForm(); // no need for an indirection |
|
26479
a60a19ec5ce5
8057657: Annotate LambdaForm parameters with types
vlivanov
parents:
26472
diff
changeset
|
94 |
return makeReinvokerForm(target, MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, NF_getTarget); |
26472 | 95 |
} |
96 |
||
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
97 |
static LambdaForm makeReinvokerForm(MethodHandle target, |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
98 |
int whichCache, |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
99 |
Object constraint, |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
100 |
NamedFunction getTargetFn) { |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
101 |
// No pre-action needed. |
43893 | 102 |
return makeReinvokerForm(target, whichCache, constraint, true, getTargetFn, null); |
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
103 |
} |
26472 | 104 |
/** Create a LF which simply reinvokes a target of the given basic type. */ |
105 |
static LambdaForm makeReinvokerForm(MethodHandle target, |
|
106 |
int whichCache, |
|
26479
a60a19ec5ce5
8057657: Annotate LambdaForm parameters with types
vlivanov
parents:
26472
diff
changeset
|
107 |
Object constraint, |
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
108 |
boolean forceInline, |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
109 |
NamedFunction getTargetFn, |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
110 |
NamedFunction preActionFn) { |
26472 | 111 |
MethodType mtype = target.type().basicType(); |
40408
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
112 |
Kind kind = whichKind(whichCache); |
26472 | 113 |
boolean customized = (whichCache < 0 || |
114 |
mtype.parameterSlotCount() > MethodType.MAX_MH_INVOKER_ARITY); |
|
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
115 |
boolean hasPreAction = (preActionFn != null); |
26472 | 116 |
LambdaForm form; |
117 |
if (!customized) { |
|
118 |
form = mtype.form().cachedLambdaForm(whichCache); |
|
119 |
if (form != null) return form; |
|
120 |
} |
|
121 |
final int THIS_DMH = 0; |
|
122 |
final int ARG_BASE = 1; |
|
123 |
final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); |
|
124 |
int nameCursor = ARG_LIMIT; |
|
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
125 |
final int PRE_ACTION = hasPreAction ? nameCursor++ : -1; |
26472 | 126 |
final int NEXT_MH = customized ? -1 : nameCursor++; |
127 |
final int REINVOKE = nameCursor++; |
|
128 |
LambdaForm.Name[] names = LambdaForm.arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); |
|
129 |
assert(names.length == nameCursor); |
|
26479
a60a19ec5ce5
8057657: Annotate LambdaForm parameters with types
vlivanov
parents:
26472
diff
changeset
|
130 |
names[THIS_DMH] = names[THIS_DMH].withConstraint(constraint); |
26472 | 131 |
Object[] targetArgs; |
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
132 |
if (hasPreAction) { |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
133 |
names[PRE_ACTION] = new LambdaForm.Name(preActionFn, names[THIS_DMH]); |
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
134 |
} |
26472 | 135 |
if (customized) { |
136 |
targetArgs = Arrays.copyOfRange(names, ARG_BASE, ARG_LIMIT, Object[].class); |
|
137 |
names[REINVOKE] = new LambdaForm.Name(target, targetArgs); // the invoker is the target itself |
|
138 |
} else { |
|
139 |
names[NEXT_MH] = new LambdaForm.Name(getTargetFn, names[THIS_DMH]); |
|
140 |
targetArgs = Arrays.copyOfRange(names, THIS_DMH, ARG_LIMIT, Object[].class); |
|
141 |
targetArgs[0] = names[NEXT_MH]; // overwrite this MH with next MH |
|
142 |
names[REINVOKE] = new LambdaForm.Name(mtype, targetArgs); |
|
143 |
} |
|
43893 | 144 |
form = new LambdaForm(ARG_LIMIT, names, forceInline, kind); |
26472 | 145 |
if (!customized) { |
146 |
form = mtype.form().setCachedLambdaForm(whichCache, form); |
|
147 |
} |
|
148 |
return form; |
|
149 |
} |
|
150 |
||
40408
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
151 |
private static Kind whichKind(int whichCache) { |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
152 |
switch(whichCache) { |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
153 |
case MethodTypeForm.LF_REBIND: return BOUND_REINVOKER; |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
154 |
case MethodTypeForm.LF_DELEGATE: return DELEGATE; |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
155 |
default: return REINVOKER; |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
156 |
} |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
157 |
} |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
158 |
|
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26479
diff
changeset
|
159 |
static final NamedFunction NF_getTarget; |
26472 | 160 |
static { |
161 |
try { |
|
47280
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
162 |
MemberName member = new MemberName(DelegatingMethodHandle.class, "getTarget", |
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
163 |
MethodType.methodType(MethodHandle.class), REF_invokeVirtual); |
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
164 |
NF_getTarget = new NamedFunction( |
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
165 |
MemberName.getFactory() |
95192765a858
8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents:
47216
diff
changeset
|
166 |
.resolveOrFail(REF_invokeVirtual, member, DelegatingMethodHandle.class, NoSuchMethodException.class)); |
26472 | 167 |
} catch (ReflectiveOperationException ex) { |
168 |
throw newInternalError(ex); |
|
169 |
} |
|
40408
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
170 |
// The Holder class will contain pre-generated DelegatingMethodHandles resolved |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
171 |
// speculatively using MemberName.getFactory().resolveOrNull. However, that |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
172 |
// doesn't initialize the class, which subtly breaks inlining etc. By forcing |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
173 |
// initialization of the Holder class we avoid these issues. |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
174 |
UNSAFE.ensureClassInitialized(Holder.class); |
26472 | 175 |
} |
40408
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
176 |
|
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
177 |
/* Placeholder class for DelegatingMethodHandles generated ahead of time */ |
cf7e826d4d63
8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents:
32649
diff
changeset
|
178 |
final class Holder {} |
26472 | 179 |
} |