java.lang.invoke.MethodType Java Examples

The following examples show how to use java.lang.invoke.MethodType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PermuteArgsReturnVoidTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example #2
Source File: CatchExceptionTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
        final int catchDrops) {
    this.testCase = testCase;
    this.dropped = catchDrops;
    MethodHandle thrower = testCase.thrower;
    int throwerLen = thrower.type().parameterCount();
    List<Class<?>> classes;
    int extra = Math.max(0, argsCount - throwerLen);
    classes = getThrowerParams(isVararg, extra);
    this.argsCount = throwerLen + classes.size();
    thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
    if (isVararg && argsCount > throwerLen) {
        MethodType mt = thrower.type();
        Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
        thrower = thrower.asVarargsCollector(lastParam);
    }
    this.thrower = thrower;
    this.dropped = Math.min(this.argsCount, catchDrops);
    catcher = testCase.getCatcher(getCatcherParams());
    nargs = Math.max(2, this.argsCount);
}
 
Example #3
Source File: FinalScriptFunctionData.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
CompiledFunction getBest(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden, boolean linkLogicOkay) {
    assert isValidCallSite(callSiteType) : callSiteType;

    CompiledFunction best = null;
    for (final CompiledFunction candidate: code) {
        if (!linkLogicOkay && candidate.hasLinkLogic()) {
            // Skip! Version with no link logic is desired, but this one has link logic!
            continue;
        }

        if (!forbidden.contains(candidate) && candidate.betterThanFinal(best, callSiteType)) {
            best = candidate;
        }
    }

    return best;
}
 
Example #4
Source File: MethodTypeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of toString method, of class MethodType.
 */
@Test
public void testToString() {
    System.out.println("toString");
    MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
    //String expResult = "void[int, class java.lang.String]";
    String[] expResults = {
        "(int,String)void",
        "(Object,Object)Object",
        "()void",
        "()Object",
        "(String,Integer)int",
        "(String,int)Integer",
        "(String,Integer)Integer",
        "(String,int)int"
    };
    for (int i = 0; i < instances.length; i++) {
        MethodType instance = instances[i];
        String result = instance.toString();
        System.out.println("#"+i+":"+result);
        assertEquals("#"+i, expResults[i], result);
    }
}
 
Example #5
Source File: LambdaReceiver.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example #6
Source File: LambdaDesugaring.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Produces a {@link MethodHandle} or {@link MethodType} using {@link #targetLoader} for the
 * given ASM {@link Handle} or {@link Type}. {@code lookup} is only used for resolving {@link
 * Handle}s.
 */
private Object toJvmMetatype(Lookup lookup, Object asm) throws ReflectiveOperationException {
  if (asm instanceof Number) {
    return asm;
  }
  if (asm instanceof Type) {
    Type type = (Type) asm;
    switch (type.getSort()) {
      case Type.OBJECT:
        return loadFromInternal(type.getInternalName());
      case Type.METHOD:
        return MethodType.fromMethodDescriptorString(type.getDescriptor(), targetLoader);
      default:
        throw new IllegalArgumentException("Cannot convert: " + asm);
    }
  }
  if (asm instanceof Handle) {
    return toMethodHandle(lookup, (Handle) asm, /*target*/ true);
  }
  throw new IllegalArgumentException("Cannot convert: " + asm);
}
 
Example #7
Source File: LambdaFactory.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
private static MethodType createLambdaMethodType(Method method, MethodType instantiatedMethodType) {
	boolean isStatic = Modifier.isStatic(method.getModifiers());
	MethodType signature = isStatic ? instantiatedMethodType : instantiatedMethodType.changeParameterType(0, Object.class);

	Class<?>[] params = method.getParameterTypes();
	for (int i=0; i<params.length; i++){
		if (Object.class.isAssignableFrom(params[i])){
			signature = signature.changeParameterType(isStatic ? i : i+1, Object.class);
		}
	}
	if (Object.class.isAssignableFrom(signature.returnType())){
		signature = signature.changeReturnType(Object.class);
	}
	
	return signature;
}
 
Example #8
Source File: RecompilableScriptFunctionData.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
Compiler getCompiler(final FunctionNode functionNode, final MethodType actualCallSiteType,
        final ScriptObject runtimeScope, final Map<Integer, Type> invalidatedProgramPoints,
        final int[] continuationEntryPoints) {
    final TypeMap typeMap = typeMap(actualCallSiteType);
    final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
    final Object typeInformationFile = OptimisticTypesPersistence.getLocationDescriptor(source, functionNodeId, paramTypes);
    final Context context = Context.getContextTrusted();
    return new Compiler(
            context,
            context.getEnv(),
            getInstallerForNewCode(),
            functionNode.getSource(),  // source
            context.getErrorManager(),
            isStrict() | functionNode.isStrict(), // is strict
            true,       // is on demand
            this,       // compiledFunction, i.e. this RecompilableScriptFunctionData
            typeMap,    // type map
            getEffectiveInvalidatedProgramPoints(invalidatedProgramPoints, typeInformationFile), // invalidated program points
            typeInformationFile,
            continuationEntryPoints, // continuation entry points
            runtimeScope); // runtime scope
}
 
Example #9
Source File: BeanLinkerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "flags")
public void systemLoadLibraryTest(final boolean publicLookup) {
    final CallSite cs1 = createGetMethodCallSite(publicLookup, "loadLibrary");
    final CallSite cs2 = createCallSite(publicLookup, CALL, MethodType.methodType(void.class, Object.class, Object.class, String.class));

    try {
        final Object method = cs1.getTarget().invoke(StaticClass.forClass(System.class));
        cs2.getTarget().invoke(method, StaticClass.forClass(System.class), "foo");
        throw new RuntimeException("should not reach here in any case!");
    } catch (final Throwable th) {
        if (publicLookup) {
            Assert.assertTrue(th instanceof IllegalAccessError);
        } else {
            Assert.assertTrue(th instanceof AccessControlException);
        }
    }
}
 
Example #10
Source File: FinalScriptFunctionData.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
CompiledFunction getBest(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden, boolean linkLogicOkay) {
    assert isValidCallSite(callSiteType) : callSiteType;

    CompiledFunction best = null;
    for (final CompiledFunction candidate: code) {
        if (!linkLogicOkay && candidate.hasLinkLogic()) {
            // Skip! Version with no link logic is desired, but this one has link logic!
            continue;
        }

        if (!forbidden.contains(candidate) && candidate.betterThanFinal(best, callSiteType)) {
            best = candidate;
        }
    }

    return best;
}
 
Example #11
Source File: TypeConverterFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
MethodHandle createConverter(final Class<?> sourceType, final Class<?> targetType) throws Exception {
    final MethodType type = MethodType.methodType(targetType, sourceType);
    final MethodHandle identity = IDENTITY_CONVERSION.asType(type);
    MethodHandle last = identity;
    boolean cacheable = true;
    for(int i = factories.length; i-- > 0;) {
        final GuardedTypeConversion next = factories[i].convertToType(sourceType, targetType);
        if(next != null) {
            cacheable = cacheable && next.isCacheable();
            final GuardedInvocation conversionInvocation = next.getConversionInvocation();
            conversionInvocation.assertType(type);
            last = conversionInvocation.compose(last);
        }
    }
    if(last == identity) {
        return IDENTITY_CONVERSION;
    }
    if(cacheable) {
        return last;
    }
    throw new NotCacheableConverter(last);
}
 
Example #12
Source File: LambdaReceiver.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example #13
Source File: SerializerH3Enum.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
private MethodHandle introspectConstructor()
{
  try {
    Method m = _type.getMethod("valueOf", String.class);
    
    Objects.requireNonNull(m);
    
    m.setAccessible(true);
      
    MethodHandle mh = MethodHandles.lookup().unreflect(m);
        
    mh = mh.asType(MethodType.methodType(Object.class, String.class));
      
    return mh;
  } catch (Exception e) {
    throw new H3ExceptionIn(_type.getName() + ": " + e.getMessage(), e);
  }
}
 
Example #14
Source File: ScriptFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static MethodHandle spreadGuardArguments(final MethodHandle guard, final MethodType descType) {
    final MethodType guardType = guard.type();
    final int guardParamCount = guardType.parameterCount();
    final int descParamCount = descType.parameterCount();
    final int spreadCount = guardParamCount - descParamCount + 1;
    if (spreadCount <= 0) {
        // Guard doesn't dip into the varargs
        return guard;
    }

    final MethodHandle arrayConvertingGuard;
    // If the last parameter type of the guard is an array, then it is already itself a guard for a vararg apply
    // invocation. We must filter the last argument with toApplyArgs otherwise deeper levels of nesting will fail
    // with ClassCastException of NativeArray to Object[].
    if (guardType.parameterType(guardParamCount - 1).isArray()) {
        arrayConvertingGuard = MH.filterArguments(guard, guardParamCount - 1, NativeFunction.TO_APPLY_ARGS);
    } else {
        arrayConvertingGuard = guard;
    }

    return ScriptObject.adaptHandleToVarArgCallSite(arrayConvertingGuard, descParamCount);
}
 
Example #15
Source File: WithObject.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
    // If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
    // expression.
    if(!"getMethod".equals(desc.getFirstOperator())) {
        return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
    }

    final MethodHandle linkInvocation = link.getInvocation();
    final MethodType linkType = linkInvocation.type();
    final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
    return link.replaceMethods(
            // Make sure getMethod will bind the script functions it receives to WithObject.expression
            MH.foldArguments(linkReturnsFunction ? BIND_TO_EXPRESSION_FN : BIND_TO_EXPRESSION_OBJ,
                    filter(linkInvocation.asType(linkType.changeReturnType(
                            linkReturnsFunction ? ScriptFunction.class : Object.class)), WITHEXPRESSIONFILTER)),
            // No clever things for the guard -- it is still identically filtered.
            filterGuard(link, WITHEXPRESSIONFILTER));
}
 
Example #16
Source File: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an implementation for a "dyn:callMethod" operation. Note that Nashorn internally never uses
 * "dyn:callMethod", but instead always emits two call sites in bytecode, one for "dyn:getMethod", and then another
 * one for "dyn:call". Explicit support for "dyn:callMethod" is provided for the benefit of potential external
 * callers. The implementation itself actually folds a "dyn:getMethod" method handle into a "dyn:call" method handle.
 *
 * @param desc    the call site descriptor.
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    // R(P0, P1, ...)
    final MethodType callType = desc.getMethodType();
    // use type Object(P0) for the getter
    final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0)));
    final GuardedInvocation getter = findGetMethod(getterType, request, "getMethod");

    // Object(P0) => Object(P0, P1, ...)
    final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount()));
    // R(Object, P0, P1, ...)
    final MethodHandle invoker = Bootstrap.createDynamicInvoker("dyn:call", callType.insertParameterTypes(0, argDroppingGetter.type().returnType()));
    // Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...)
    return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard());
}
 
Example #17
Source File: ScriptFunction.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
    final Class<?>   own = ScriptFunction.class;
    final MethodType mt  = MH.type(rtype, types);
    try {
        return MH.findStatic(MethodHandles.lookup(), own, name, mt);
    } catch (final MethodHandleFactory.LookupException e) {
        return MH.findVirtual(MethodHandles.lookup(), own, name, mt);
    }
}
 
Example #18
Source File: NativeObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle getBeanOperation(final GuardingDynamicLinker linker, final String operation,
        final MethodType methodType, final Object source) {
    final GuardedInvocation inv;
    try {
        inv = NashornBeansLinker.getGuardedInvocation(linker, createLinkRequest(operation, methodType, source), Bootstrap.getLinkerServices());
        assert passesGuard(source, inv.getGuard());
    } catch(RuntimeException|Error e) {
        throw e;
    } catch(final Throwable t) {
        throw new RuntimeException(t);
    }
    assert inv.getSwitchPoints() == null; // Linkers in Dynalink's beans package don't use switchpoints.
    // We discard the guard, as all method handles will be bound to a specific object.
    return inv.getInvocation();
}
 
Example #19
Source File: SerializedLambdaTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
 
Example #20
Source File: Guards.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a method handle that returns true if the argument in the specified position is a Java array.
 *
 * @param pos the position in the argument lit
 * @param type the method type of the handle
 * @return a method handle that returns true if the argument in the specified position is a Java array; the rest of
 * the arguments are ignored.
 */
@SuppressWarnings("boxing")
public static MethodHandle isArray(int pos, MethodType type) {
    final Class<?> declaredType = type.parameterType(pos);
    if(declaredType.isArray()) {
        LOG.log(Level.WARNING, "isArrayGuardAlwaysTrue", new Object[] { pos, type, DynamicLinker.getLinkedCallSiteLocation() });
        return constantTrue(type);
    }
    if(!declaredType.isAssignableFrom(Object[].class)) {
        LOG.log(Level.WARNING, "isArrayGuardAlwaysFalse", new Object[] { pos, type, DynamicLinker.getLinkedCallSiteLocation() });
        return constantFalse(type);
    }
    return asType(IS_ARRAY, pos, type);
}
 
Example #21
Source File: BigArityTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test254() throws Throwable {
    final int ARITY = 254;
    System.out.println("test"+ARITY);
    Object[] a = testArgs(ARITY);
    Object r0 = hashArguments(a);
    Object r;
    r = hashArguments_254(
// <editor-fold defaultstate="collapsed" desc="a[0x00], a[0x01], a[0x02], a[0x03], a[0x04], ...">
a[0x00], a[0x01], a[0x02], a[0x03], a[0x04], a[0x05], a[0x06], a[0x07], a[0x08], a[0x09], a[0x0A], a[0x0B], a[0x0C], a[0x0D], a[0x0E], a[0x0F],
a[0x10], a[0x11], a[0x12], a[0x13], a[0x14], a[0x15], a[0x16], a[0x17], a[0x18], a[0x19], a[0x1A], a[0x1B], a[0x1C], a[0x1D], a[0x1E], a[0x1F],
a[0x20], a[0x21], a[0x22], a[0x23], a[0x24], a[0x25], a[0x26], a[0x27], a[0x28], a[0x29], a[0x2A], a[0x2B], a[0x2C], a[0x2D], a[0x2E], a[0x2F],
a[0x30], a[0x31], a[0x32], a[0x33], a[0x34], a[0x35], a[0x36], a[0x37], a[0x38], a[0x39], a[0x3A], a[0x3B], a[0x3C], a[0x3D], a[0x3E], a[0x3F],
a[0x40], a[0x41], a[0x42], a[0x43], a[0x44], a[0x45], a[0x46], a[0x47], a[0x48], a[0x49], a[0x4A], a[0x4B], a[0x4C], a[0x4D], a[0x4E], a[0x4F],
a[0x50], a[0x51], a[0x52], a[0x53], a[0x54], a[0x55], a[0x56], a[0x57], a[0x58], a[0x59], a[0x5A], a[0x5B], a[0x5C], a[0x5D], a[0x5E], a[0x5F],
a[0x60], a[0x61], a[0x62], a[0x63], a[0x64], a[0x65], a[0x66], a[0x67], a[0x68], a[0x69], a[0x6A], a[0x6B], a[0x6C], a[0x6D], a[0x6E], a[0x6F],
a[0x70], a[0x71], a[0x72], a[0x73], a[0x74], a[0x75], a[0x76], a[0x77], a[0x78], a[0x79], a[0x7A], a[0x7B], a[0x7C], a[0x7D], a[0x7E], a[0x7F],
a[0x80], a[0x81], a[0x82], a[0x83], a[0x84], a[0x85], a[0x86], a[0x87], a[0x88], a[0x89], a[0x8A], a[0x8B], a[0x8C], a[0x8D], a[0x8E], a[0x8F],
a[0x90], a[0x91], a[0x92], a[0x93], a[0x94], a[0x95], a[0x96], a[0x97], a[0x98], a[0x99], a[0x9A], a[0x9B], a[0x9C], a[0x9D], a[0x9E], a[0x9F],
a[0xA0], a[0xA1], a[0xA2], a[0xA3], a[0xA4], a[0xA5], a[0xA6], a[0xA7], a[0xA8], a[0xA9], a[0xAA], a[0xAB], a[0xAC], a[0xAD], a[0xAE], a[0xAF],
a[0xB0], a[0xB1], a[0xB2], a[0xB3], a[0xB4], a[0xB5], a[0xB6], a[0xB7], a[0xB8], a[0xB9], a[0xBA], a[0xBB], a[0xBC], a[0xBD], a[0xBE], a[0xBF],
a[0xC0], a[0xC1], a[0xC2], a[0xC3], a[0xC4], a[0xC5], a[0xC6], a[0xC7], a[0xC8], a[0xC9], a[0xCA], a[0xCB], a[0xCC], a[0xCD], a[0xCE], a[0xCF],
a[0xD0], a[0xD1], a[0xD2], a[0xD3], a[0xD4], a[0xD5], a[0xD6], a[0xD7], a[0xD8], a[0xD9], a[0xDA], a[0xDB], a[0xDC], a[0xDD], a[0xDE], a[0xDF],
a[0xE0], a[0xE1], a[0xE2], a[0xE3], a[0xE4], a[0xE5], a[0xE6], a[0xE7], a[0xE8], a[0xE9], a[0xEA], a[0xEB], a[0xEC], a[0xED], a[0xEE], a[0xEF],
a[0xF0], a[0xF1], a[0xF2], a[0xF3], a[0xF4], a[0xF5], a[0xF6], a[0xF7],
// </editor-fold>
a[0xF8], a[0xF9], a[0xFA], a[0xFB], a[0xFC], a[0xFD]); // hashArguments_254
    assertEquals(r0, r);
    MethodType mt = MethodType.genericMethodType(ARITY);
    MethodHandle mh = MethodHandles.lookup().findStatic(BigArityTest.class, "hashArguments_"+ARITY, mt);
    test254(mh, a, r0);
    MethodHandle mh_CA = MH_hashArguments_VA.asFixedArity().asCollector(Object[].class, ARITY);
    test254(mh_CA, a, r0);
}
 
Example #22
Source File: Guards.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it
 * returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the
 * arguments will be ignored.
 *
 * @param clazz the class of the first argument to test for
 * @param type the method type
 * @return a method handle testing whether its first argument is of the specified class.
 */
@SuppressWarnings("boxing")
public static MethodHandle isOfClass(final Class<?> clazz, final MethodType type) {
    final Class<?> declaredType = type.parameterType(0);
    if(clazz == declaredType) {
        LOG.log(Level.WARNING, "isOfClassGuardAlwaysTrue", new Object[] { clazz.getName(), 0, type, DynamicLinker.getLinkedCallSiteLocation() });
        return constantTrue(type);
    }
    if(!declaredType.isAssignableFrom(clazz)) {
        LOG.log(Level.WARNING, "isOfClassGuardAlwaysFalse", new Object[] { clazz.getName(), 0, type, DynamicLinker.getLinkedCallSiteLocation() });
        return constantFalse(type);
    }
    return getClassBoundArgumentTest(IS_OF_CLASS, clazz, 0, type);
}
 
Example #23
Source File: MethodTypeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test of hashCode method, of class MethodType.
 */
@Test
public void testHashCode() {
    System.out.println("hashCode");
    MethodType instance = mt_viS;
    ArrayList<Class<?>> types = new ArrayList<>();
    types.add(instance.returnType());
    types.addAll(instance.parameterList());
    int expResult = types.hashCode();
    int result = instance.hashCode();
    assertEquals(expResult, result);
}
 
Example #24
Source File: Operators.java    From es6draft with MIT License 5 votes vote down vote up
public static CallSite runtimeBootstrap(MethodHandles.Lookup caller, String name, MethodType type) {
    assert "rt:stack".equals(name) || "rt:locals".equals(name);
    MethodHandle mh = MethodHandles.identity(Object[].class);
    mh = mh.asCollector(Object[].class, type.parameterCount());
    mh = mh.asType(type);
    return new ConstantCallSite(mh);
}
 
Example #25
Source File: MethodTypeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testHasWrappers() {
    System.out.println("hasWrappers");
    MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
    boolean[] expResults =   {false,  false,  false, false, true,   true,   true,   false};
    for (int i = 0; i < instances.length; i++) {
        System.out.println("  hasWrappers "+instances[i]);
        boolean result = instances[i].hasWrappers();
        assertEquals("#"+i, expResults[i], result);
    }
}
 
Example #26
Source File: MethodTypeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testWrap() {
    System.out.println("wrap");
    MethodType[] instances =  {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
    MethodType[] expResults = {mt_VIS, mt_OO2, mt_Vv, mt_Ov, mt_ISI, mt_ISI, mt_ISI, mt_ISI};
    for (int i = 0; i < instances.length; i++) {
        MethodType result = instances[i].wrap();
        assertSame("#"+i, expResults[i], result);
    }
}
 
Example #27
Source File: ValueConversionsTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    boolean testSingleCase = (tval != 0);
    final long tvalInit = tval;
    MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
    MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
    assertEquals(convType, conv.type());
    MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
    for (;;) {
        long n = tval;
        Object testValue = src.wrap(n);
        Object expResult = dst.cast(testValue, dst.primitiveType());
        Object result;
        switch (src) {
            case INT:     result = converter.invokeExact((int)n); break;
            case LONG:    result = converter.invokeExact(/*long*/n); break;
            case FLOAT:   result = converter.invokeExact((float)n); break;
            case DOUBLE:  result = converter.invokeExact((double)n); break;
            case CHAR:    result = converter.invokeExact((char)n); break;
            case BYTE:    result = converter.invokeExact((byte)n); break;
            case SHORT:   result = converter.invokeExact((short)n); break;
            case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
            default:  throw new AssertionError();
        }
        assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
                     expResult, result);
        if (testSingleCase)  break;
        // next test value:
        tval = nextTestValue(tval);
        if (tval == tvalInit)  break;  // repeat
    }
}
 
Example #28
Source File: RecompilableScriptFunctionData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodType getGenericType() {
    // 2 is for (callee, this)
    if (isVariableArity()) {
        return MethodType.genericMethodType(2, true);
    }
    return MethodType.genericMethodType(2 + getArity());
}
 
Example #29
Source File: MethodTypeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** Make sure the method types are all distinct. */
@Test
public void testDistinct() {
    List<MethodType> gallery2 = new ArrayList<>();
    for (MethodType mt : GALLERY) {
        assertFalse(mt.toString(), gallery2.contains(mt));
        gallery2.add(mt);
    }
    // check self-equality also:
    assertEquals(Arrays.asList(GALLERY), gallery2);
}
 
Example #30
Source File: ValueConversions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}