2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26 package java.lang.invoke;
28 import static java.lang.invoke.LambdaForm.basicTypes;
29 import static java.lang.invoke.MethodHandleStatics.*;
31 import java.lang.invoke.LambdaForm.Name;
32 import java.lang.invoke.LambdaForm.NamedFunction;
33 import java.lang.invoke.MethodHandles.Lookup;
34 import java.util.Arrays;
35 import java.util.HashMap;
37 import sun.invoke.util.ValueConversions;
40 * The flavor of method handle which emulates an invoke instruction
41 * on a predetermined argument. The JVM dispatches to the correct method
42 * when the handle is created, not when it is invoked.
44 * All bound arguments are encapsulated in dedicated species.
46 /* non-public */ abstract class BoundMethodHandle extends MethodHandle {
48 /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) {
53 // BMH API and internals
56 static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) {
57 // for some type signatures, there exist pre-defined concrete BMH classes
61 if (true) return bindSingle(type, form, x); // Use known fast path.
62 return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x);
64 return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
66 return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x);
68 return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x);
70 return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x);
71 default : throw new InternalError("unexpected xtype: " + xtype);
73 } catch (Throwable t) {
74 throw newInternalError(t);
78 static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) {
79 return new Species_L(type, form, x);
82 MethodHandle cloneExtend(MethodType type, LambdaForm form, char xtype, Object x) {
85 case 'L': return cloneExtendL(type, form, x);
86 case 'I': return cloneExtendI(type, form, ValueConversions.widenSubword(x));
87 case 'J': return cloneExtendJ(type, form, (long) x);
88 case 'F': return cloneExtendF(type, form, (float) x);
89 case 'D': return cloneExtendD(type, form, (double) x);
91 } catch (Throwable t) {
92 throw newInternalError(t);
94 throw new InternalError("unexpected type: " + xtype);
98 MethodHandle bindArgument(int pos, char basicType, Object value) {
99 MethodType type = type().dropParameterTypes(pos, pos+1);
100 LambdaForm form = internalForm().bind(1+pos, speciesData());
101 return cloneExtend(type, form, basicType, value);
105 MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
106 LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos+drops));
108 return clone(srcType, form);
109 } catch (Throwable t) {
110 throw newInternalError(t);
115 MethodHandle permuteArguments(MethodType newType, int[] reorder) {
117 return clone(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList())));
118 } catch (Throwable t) {
119 throw newInternalError(t);
123 static final String EXTENSION_TYPES = "LIJFD";
124 static final byte INDEX_L = 0, INDEX_I = 1, INDEX_J = 2, INDEX_F = 3, INDEX_D = 4;
125 static byte extensionIndex(char type) {
126 int i = EXTENSION_TYPES.indexOf(type);
127 if (i < 0) throw new InternalError();
132 * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a
133 * static field containing this value, and they must accordingly implement this method.
135 protected abstract SpeciesData speciesData();
138 final Object internalProperties() {
139 return "/BMH="+internalValues();
143 final Object internalValues() {
144 Object[] boundValues = new Object[speciesData().fieldCount()];
145 for (int i = 0; i < boundValues.length; ++i) {
146 boundValues[i] = arg(i);
148 return Arrays.asList(boundValues);
151 public final Object arg(int i) {
153 switch (speciesData().fieldType(i)) {
154 case 'L': return argL(i);
155 case 'I': return argI(i);
156 case 'F': return argF(i);
157 case 'D': return argD(i);
158 case 'J': return argJ(i);
160 } catch (Throwable ex) {
161 throw newInternalError(ex);
163 throw new InternalError("unexpected type: " + speciesData().types+"."+i);
165 public final Object argL(int i) throws Throwable { return speciesData().getters[i].invokeBasic(this); }
166 public final int argI(int i) throws Throwable { return (int) speciesData().getters[i].invokeBasic(this); }
167 public final float argF(int i) throws Throwable { return (float) speciesData().getters[i].invokeBasic(this); }
168 public final double argD(int i) throws Throwable { return (double) speciesData().getters[i].invokeBasic(this); }
169 public final long argJ(int i) throws Throwable { return (long) speciesData().getters[i].invokeBasic(this); }
175 public abstract BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable;
176 public abstract BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable;
177 public abstract BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable;
178 public abstract BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable;
179 public abstract BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable;
180 public abstract BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable;
182 // The following is a grossly irregular hack:
183 @Override MethodHandle reinvokerTarget() {
185 return (MethodHandle) argL(0);
186 } catch (Throwable ex) {
187 throw newInternalError(ex);
192 // concrete BMH classes required to close bootstrap loops
195 private // make it private to force users to access the enclosing class first
196 static final class Species_L extends BoundMethodHandle {
198 public Species_L(MethodType mt, LambdaForm lf, Object argL0) {
202 // The following is a grossly irregular hack:
203 @Override MethodHandle reinvokerTarget() { return (MethodHandle) argL0; }
205 public SpeciesData speciesData() {
208 public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class);
210 public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
211 return new Species_L(mt, lf, argL0);
214 public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
215 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, narg);
218 public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
219 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, narg);
222 public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
223 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, narg);
226 public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
227 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, narg);
230 public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
231 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, narg);
236 static final class Species_LL extends BoundMethodHandle {
239 public Species_LL(MethodType mt, LambdaForm lf, Object argL0, Object argL1) {
245 public SpeciesData speciesData() {
248 public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LL", Species_LL.class);
250 public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
251 return new Species_LL(mt, lf, argL0, argL1);
254 public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
255 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
258 public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
259 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
262 public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
263 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
266 public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
267 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
270 public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
271 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg);
275 static final class Species_JL extends BoundMethodHandle {
278 public Species_JL(MethodType mt, LambdaForm lf, long argJ0, Object argL1) {
284 public SpeciesData speciesData() {
287 public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("JL", Species_JL.class);
288 @Override public final long argJ0() { return argJ0; }
289 @Override public final Object argL1() { return argL1; }
291 public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable {
292 return new Species_JL(mt, lf, argJ0, argL1);
295 public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable {
296 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
299 public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable {
300 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
303 public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable {
304 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
307 public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable {
308 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
311 public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable {
312 return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg);
318 // BMH species meta-data
322 * Meta-data wrapper for concrete BMH classes.
324 static class SpeciesData {
326 final Class<? extends BoundMethodHandle> clazz;
327 // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH
328 // Therefore, we need a non-final link in the chain. Use array elements.
329 final MethodHandle[] constructor;
330 final MethodHandle[] getters;
331 final SpeciesData[] extensions;
333 public int fieldCount() {
334 return types.length();
336 public char fieldType(int i) {
337 return types.charAt(i);
340 public String toString() {
341 return "SpeciesData["+(isPlaceholder() ? "<placeholder>" : clazz.getSimpleName())+":"+types+"]";
345 * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that
346 * represents a MH bound to a generic invoker, which in turn forwards to the corresponding
349 Name getterName(Name mhName, int i) {
350 MethodHandle mh = getters[i];
351 assert(mh != null) : this+"."+i;
352 return new Name(mh, mhName);
355 NamedFunction getterFunction(int i) {
356 return new NamedFunction(getters[i]);
359 static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class);
361 private SpeciesData(String types, Class<? extends BoundMethodHandle> clazz) {
365 this.constructor = new MethodHandle[1];
366 this.getters = new MethodHandle[types.length()];
368 throw new IllegalStateException("bound method handle");
369 // this.constructor = Factory.makeCtors(clazz, types, null);
370 // this.getters = Factory.makeGetters(clazz, types, null);
372 this.extensions = new SpeciesData[EXTENSION_TYPES.length()];
375 private void initForBootstrap() {
377 if (constructor[0] == null) {
378 // Factory.makeCtors(clazz, types, this.constructor);
379 // Factory.makeGetters(clazz, types, this.getters);
383 private SpeciesData(String types) {
387 this.constructor = null;
389 this.extensions = null;
391 private boolean isPlaceholder() { return clazz == null; }
393 private static final HashMap<String, SpeciesData> CACHE = new HashMap<>();
394 static { CACHE.put("", EMPTY); } // make bootstrap predictable
395 private static final boolean INIT_DONE; // set after <clinit> finishes...
397 SpeciesData extendWithType(char type) {
398 int i = extensionIndex(type);
399 SpeciesData d = extensions[i];
400 if (d != null) return d;
401 extensions[i] = d = get(types+type);
405 SpeciesData extendWithIndex(byte index) {
406 SpeciesData d = extensions[index];
407 if (d != null) return d;
408 extensions[index] = d = get(types+EXTENSION_TYPES.charAt(index));
412 private static SpeciesData get(String types) {
413 // Acquire cache lock for query.
414 SpeciesData d = lookupCache(types);
415 if (!d.isPlaceholder())
418 // Use synch. on the placeholder to prevent multiple instantiation of one species.
419 // Creating this class forces a recursive call to getForClass.
420 if (lookupCache(types).isPlaceholder())
421 throw new IllegalStateException("Cannot generate anything");
423 // Reacquire cache lock.
424 d = lookupCache(types);
425 // Class loading must have upgraded the cache.
426 assert(d != null && !d.isPlaceholder());
429 static SpeciesData getForClass(String types, Class<? extends BoundMethodHandle> clazz) {
430 // clazz is a new class which is initializing its SPECIES_DATA field
431 return updateCache(types, new SpeciesData(types, clazz));
433 private static synchronized SpeciesData lookupCache(String types) {
434 SpeciesData d = CACHE.get(types);
435 if (d != null) return d;
436 d = new SpeciesData(types);
437 assert(d.isPlaceholder());
441 private static synchronized SpeciesData updateCache(String types, SpeciesData d) {
443 assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder());
444 assert(!d.isPlaceholder());
450 // pre-fill the BMH speciesdata cache with BMH's inner classes
451 final Class<BoundMethodHandle> rootCls = BoundMethodHandle.class;
452 SpeciesData d0 = BoundMethodHandle.SPECIES_DATA; // trigger class init
453 assert(d0 == null || d0 == lookupCache("")) : d0;
456 for (Class<?> c : rootCls.getDeclaredClasses()) {
457 if (rootCls.isAssignableFrom(c)) {
458 final Class<? extends BoundMethodHandle> cbmh = c.asSubclass(BoundMethodHandle.class);
459 SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh);
460 assert(d != null) : cbmh.getName();
461 assert(d.clazz == cbmh);
462 assert(d == lookupCache(d.types));
466 } catch (Throwable e) {
467 throw newInternalError(e);
470 for (SpeciesData d : CACHE.values()) {
471 d.initForBootstrap();
473 // Note: Do not simplify this, because INIT_DONE must not be
474 // a compile-time constant during bootstrapping.
475 INIT_DONE = Boolean.TRUE;
479 static SpeciesData getSpeciesData(String types) {
480 return SpeciesData.get(types);
485 private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP;
488 * All subclasses must provide such a value describing their type signature.
490 static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY;