jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import static java.lang.invoke.LambdaForm.basicTypes; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: jaroslav@1646: import java.lang.invoke.LambdaForm.Name; jaroslav@1646: import java.lang.invoke.LambdaForm.NamedFunction; jaroslav@1646: import java.lang.invoke.MethodHandles.Lookup; jaroslav@1646: import java.util.Arrays; jaroslav@1646: import java.util.HashMap; jaroslav@1646: jaroslav@1646: import sun.invoke.util.ValueConversions; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * The flavor of method handle which emulates an invoke instruction jaroslav@1646: * on a predetermined argument. The JVM dispatches to the correct method jaroslav@1646: * when the handle is created, not when it is invoked. jaroslav@1646: * jaroslav@1646: * All bound arguments are encapsulated in dedicated species. jaroslav@1646: */ jaroslav@1646: /* non-public */ abstract class BoundMethodHandle extends MethodHandle { jaroslav@1646: jaroslav@1646: /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) { jaroslav@1646: super(type, form); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // BMH API and internals jaroslav@1646: // jaroslav@1646: jaroslav@1646: static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) { jaroslav@1646: // for some type signatures, there exist pre-defined concrete BMH classes jaroslav@1646: try { jaroslav@1646: switch (xtype) { jaroslav@1646: case 'L': jaroslav@1646: if (true) return bindSingle(type, form, x); // Use known fast path. jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x); jaroslav@1646: case 'I': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x)); jaroslav@1646: case 'J': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x); jaroslav@1646: case 'F': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x); jaroslav@1646: case 'D': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x); jaroslav@1646: default : throw new InternalError("unexpected xtype: " + xtype); jaroslav@1646: } jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) { jaroslav@1646: return new Species_L(type, form, x); jaroslav@1646: } jaroslav@1646: jaroslav@1646: MethodHandle cloneExtend(MethodType type, LambdaForm form, char xtype, Object x) { jaroslav@1646: try { jaroslav@1646: switch (xtype) { jaroslav@1646: case 'L': return cloneExtendL(type, form, x); jaroslav@1646: case 'I': return cloneExtendI(type, form, ValueConversions.widenSubword(x)); jaroslav@1646: case 'J': return cloneExtendJ(type, form, (long) x); jaroslav@1646: case 'F': return cloneExtendF(type, form, (float) x); jaroslav@1646: case 'D': return cloneExtendD(type, form, (double) x); jaroslav@1646: } jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: throw new InternalError("unexpected type: " + xtype); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle bindArgument(int pos, char basicType, Object value) { jaroslav@1646: MethodType type = type().dropParameterTypes(pos, pos+1); jaroslav@1646: LambdaForm form = internalForm().bind(1+pos, speciesData()); jaroslav@1646: return cloneExtend(type, form, basicType, value); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle dropArguments(MethodType srcType, int pos, int drops) { jaroslav@1646: LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos+drops)); jaroslav@1646: try { jaroslav@1646: return clone(srcType, form); jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle permuteArguments(MethodType newType, int[] reorder) { jaroslav@1646: try { jaroslav@1646: return clone(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList()))); jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final String EXTENSION_TYPES = "LIJFD"; jaroslav@1646: static final byte INDEX_L = 0, INDEX_I = 1, INDEX_J = 2, INDEX_F = 3, INDEX_D = 4; jaroslav@1646: static byte extensionIndex(char type) { jaroslav@1646: int i = EXTENSION_TYPES.indexOf(type); jaroslav@1646: if (i < 0) throw new InternalError(); jaroslav@1646: return (byte) i; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a jaroslav@1646: * static field containing this value, and they must accordingly implement this method. jaroslav@1646: */ jaroslav@1646: protected abstract SpeciesData speciesData(); jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: final Object internalProperties() { jaroslav@1646: return "/BMH="+internalValues(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: final Object internalValues() { jaroslav@1646: Object[] boundValues = new Object[speciesData().fieldCount()]; jaroslav@1646: for (int i = 0; i < boundValues.length; ++i) { jaroslav@1646: boundValues[i] = arg(i); jaroslav@1646: } jaroslav@1646: return Arrays.asList(boundValues); jaroslav@1646: } jaroslav@1646: jaroslav@1646: public final Object arg(int i) { jaroslav@1646: try { jaroslav@1646: switch (speciesData().fieldType(i)) { jaroslav@1646: case 'L': return argL(i); jaroslav@1646: case 'I': return argI(i); jaroslav@1646: case 'F': return argF(i); jaroslav@1646: case 'D': return argD(i); jaroslav@1646: case 'J': return argJ(i); jaroslav@1646: } jaroslav@1646: } catch (Throwable ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: throw new InternalError("unexpected type: " + speciesData().types+"."+i); jaroslav@1646: } jaroslav@1646: public final Object argL(int i) throws Throwable { return speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final int argI(int i) throws Throwable { return (int) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final float argF(int i) throws Throwable { return (float) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final double argD(int i) throws Throwable { return (double) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final long argJ(int i) throws Throwable { return (long) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // cloning API jaroslav@1646: // jaroslav@1646: jaroslav@1646: public abstract BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable; jaroslav@1646: jaroslav@1646: // The following is a grossly irregular hack: jaroslav@1646: @Override MethodHandle reinvokerTarget() { jaroslav@1646: try { jaroslav@1646: return (MethodHandle) argL(0); jaroslav@1646: } catch (Throwable ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // concrete BMH classes required to close bootstrap loops jaroslav@1646: // jaroslav@1646: jaroslav@1646: private // make it private to force users to access the enclosing class first jaroslav@1646: static final class Species_L extends BoundMethodHandle { jaroslav@1646: final Object argL0; jaroslav@1646: public Species_L(MethodType mt, LambdaForm lf, Object argL0) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argL0 = argL0; jaroslav@1646: } jaroslav@1646: // The following is a grossly irregular hack: jaroslav@1646: @Override MethodHandle reinvokerTarget() { return (MethodHandle) argL0; } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class); jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_L(mt, lf, argL0); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /* jaroslav@1646: static final class Species_LL extends BoundMethodHandle { jaroslav@1646: final Object argL0; jaroslav@1646: final Object argL1; jaroslav@1646: public Species_LL(MethodType mt, LambdaForm lf, Object argL0, Object argL1) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argL0 = argL0; jaroslav@1646: this.argL1 = argL1; jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LL", Species_LL.class); jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_LL(mt, lf, argL0, argL1); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final class Species_JL extends BoundMethodHandle { jaroslav@1646: final long argJ0; jaroslav@1646: final Object argL1; jaroslav@1646: public Species_JL(MethodType mt, LambdaForm lf, long argJ0, Object argL1) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argJ0 = argJ0; jaroslav@1646: this.argL1 = argL1; jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("JL", Species_JL.class); jaroslav@1646: @Override public final long argJ0() { return argJ0; } jaroslav@1646: @Override public final Object argL1() { return argL1; } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_JL(mt, lf, argJ0, argL1); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: */ jaroslav@1646: jaroslav@1646: // jaroslav@1646: // BMH species meta-data jaroslav@1646: // jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Meta-data wrapper for concrete BMH classes. jaroslav@1646: */ jaroslav@1646: static class SpeciesData { jaroslav@1646: final String types; jaroslav@1646: final Class clazz; jaroslav@1646: // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH jaroslav@1646: // Therefore, we need a non-final link in the chain. Use array elements. jaroslav@1646: final MethodHandle[] constructor; jaroslav@1646: final MethodHandle[] getters; jaroslav@1646: final SpeciesData[] extensions; jaroslav@1646: jaroslav@1646: public int fieldCount() { jaroslav@1646: return types.length(); jaroslav@1646: } jaroslav@1646: public char fieldType(int i) { jaroslav@1646: return types.charAt(i); jaroslav@1646: } jaroslav@1646: jaroslav@1646: public String toString() { jaroslav@1646: return "SpeciesData["+(isPlaceholder() ? "" : clazz.getSimpleName())+":"+types+"]"; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that jaroslav@1646: * represents a MH bound to a generic invoker, which in turn forwards to the corresponding jaroslav@1646: * getter. jaroslav@1646: */ jaroslav@1646: Name getterName(Name mhName, int i) { jaroslav@1646: MethodHandle mh = getters[i]; jaroslav@1646: assert(mh != null) : this+"."+i; jaroslav@1646: return new Name(mh, mhName); jaroslav@1646: } jaroslav@1646: jaroslav@1646: NamedFunction getterFunction(int i) { jaroslav@1646: return new NamedFunction(getters[i]); jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class); jaroslav@1646: jaroslav@1646: private SpeciesData(String types, Class clazz) { jaroslav@1646: this.types = types; jaroslav@1646: this.clazz = clazz; jaroslav@1646: if (!INIT_DONE) { jaroslav@1646: this.constructor = new MethodHandle[1]; jaroslav@1646: this.getters = new MethodHandle[types.length()]; jaroslav@1646: } else { jaroslav@1653: throw new IllegalStateException("bound method handle"); jaroslav@1653: // this.constructor = Factory.makeCtors(clazz, types, null); jaroslav@1653: // this.getters = Factory.makeGetters(clazz, types, null); jaroslav@1646: } jaroslav@1646: this.extensions = new SpeciesData[EXTENSION_TYPES.length()]; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private void initForBootstrap() { jaroslav@1646: assert(!INIT_DONE); jaroslav@1646: if (constructor[0] == null) { jaroslav@1653: // Factory.makeCtors(clazz, types, this.constructor); jaroslav@1653: // Factory.makeGetters(clazz, types, this.getters); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: private SpeciesData(String types) { jaroslav@1646: // Placeholder only. jaroslav@1646: this.types = types; jaroslav@1646: this.clazz = null; jaroslav@1646: this.constructor = null; jaroslav@1646: this.getters = null; jaroslav@1646: this.extensions = null; jaroslav@1646: } jaroslav@1646: private boolean isPlaceholder() { return clazz == null; } jaroslav@1646: jaroslav@1646: private static final HashMap CACHE = new HashMap<>(); jaroslav@1646: static { CACHE.put("", EMPTY); } // make bootstrap predictable jaroslav@1646: private static final boolean INIT_DONE; // set after finishes... jaroslav@1646: jaroslav@1646: SpeciesData extendWithType(char type) { jaroslav@1646: int i = extensionIndex(type); jaroslav@1646: SpeciesData d = extensions[i]; jaroslav@1646: if (d != null) return d; jaroslav@1646: extensions[i] = d = get(types+type); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: SpeciesData extendWithIndex(byte index) { jaroslav@1646: SpeciesData d = extensions[index]; jaroslav@1646: if (d != null) return d; jaroslav@1646: extensions[index] = d = get(types+EXTENSION_TYPES.charAt(index)); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static SpeciesData get(String types) { jaroslav@1646: // Acquire cache lock for query. jaroslav@1646: SpeciesData d = lookupCache(types); jaroslav@1646: if (!d.isPlaceholder()) jaroslav@1646: return d; jaroslav@1646: synchronized (d) { jaroslav@1646: // Use synch. on the placeholder to prevent multiple instantiation of one species. jaroslav@1646: // Creating this class forces a recursive call to getForClass. jaroslav@1646: if (lookupCache(types).isPlaceholder()) jaroslav@1653: throw new IllegalStateException("Cannot generate anything"); jaroslav@1646: } jaroslav@1646: // Reacquire cache lock. jaroslav@1646: d = lookupCache(types); jaroslav@1646: // Class loading must have upgraded the cache. jaroslav@1646: assert(d != null && !d.isPlaceholder()); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: static SpeciesData getForClass(String types, Class clazz) { jaroslav@1646: // clazz is a new class which is initializing its SPECIES_DATA field jaroslav@1646: return updateCache(types, new SpeciesData(types, clazz)); jaroslav@1646: } jaroslav@1646: private static synchronized SpeciesData lookupCache(String types) { jaroslav@1646: SpeciesData d = CACHE.get(types); jaroslav@1646: if (d != null) return d; jaroslav@1646: d = new SpeciesData(types); jaroslav@1646: assert(d.isPlaceholder()); jaroslav@1646: CACHE.put(types, d); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: private static synchronized SpeciesData updateCache(String types, SpeciesData d) { jaroslav@1646: SpeciesData d2; jaroslav@1646: assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder()); jaroslav@1646: assert(!d.isPlaceholder()); jaroslav@1646: CACHE.put(types, d); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: static { jaroslav@1646: // pre-fill the BMH speciesdata cache with BMH's inner classes jaroslav@1646: final Class rootCls = BoundMethodHandle.class; jaroslav@1646: SpeciesData d0 = BoundMethodHandle.SPECIES_DATA; // trigger class init jaroslav@1646: assert(d0 == null || d0 == lookupCache("")) : d0; jaroslav@1646: try { jaroslav@1653: /* jaroslav@1646: for (Class c : rootCls.getDeclaredClasses()) { jaroslav@1646: if (rootCls.isAssignableFrom(c)) { jaroslav@1646: final Class cbmh = c.asSubclass(BoundMethodHandle.class); jaroslav@1646: SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh); jaroslav@1646: assert(d != null) : cbmh.getName(); jaroslav@1646: assert(d.clazz == cbmh); jaroslav@1646: assert(d == lookupCache(d.types)); jaroslav@1646: } jaroslav@1646: } jaroslav@1653: */ jaroslav@1646: } catch (Throwable e) { jaroslav@1646: throw newInternalError(e); jaroslav@1646: } jaroslav@1646: jaroslav@1646: for (SpeciesData d : CACHE.values()) { jaroslav@1646: d.initForBootstrap(); jaroslav@1646: } jaroslav@1646: // Note: Do not simplify this, because INIT_DONE must not be jaroslav@1646: // a compile-time constant during bootstrapping. jaroslav@1646: INIT_DONE = Boolean.TRUE; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static SpeciesData getSpeciesData(String types) { jaroslav@1646: return SpeciesData.get(types); jaroslav@1646: } jaroslav@1646: jaroslav@1646: jaroslav@1646: jaroslav@1646: private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * All subclasses must provide such a value describing their type signature. jaroslav@1646: */ jaroslav@1646: static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY; jaroslav@1646: }