Java Code Examples for java.lang.invoke.MethodHandles#insertArguments()

The following examples show how to use java.lang.invoke.MethodHandles#insertArguments() . 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: MapToMapCast.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, Metadata metadata)
{
    checkArgument(arity == 1, "Expected arity to be 1");
    Type fromKeyType = boundVariables.getTypeVariable("FK");
    Type fromValueType = boundVariables.getTypeVariable("FV");
    Type toKeyType = boundVariables.getTypeVariable("TK");
    Type toValueType = boundVariables.getTypeVariable("TV");
    Type toMapType = metadata.getParameterizedType(
            "map",
            ImmutableList.of(
                    TypeSignatureParameter.typeParameter(toKeyType.getTypeSignature()),
                    TypeSignatureParameter.typeParameter(toValueType.getTypeSignature())));

    MethodHandle keyProcessor = buildProcessor(metadata, fromKeyType, toKeyType, true);
    MethodHandle valueProcessor = buildProcessor(metadata, fromValueType, toValueType, false);
    MethodHandle target = MethodHandles.insertArguments(METHOD_HANDLE, 0, keyProcessor, valueProcessor, toMapType);
    return new ScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), target);
}
 
Example 2
Source File: ExpressionInterpreter.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
protected Object visitBindExpression(BindExpression node, Object context)
{
    List<Object> values = node.getValues().stream()
            .map(value -> process(value, context))
            .collect(toList()); // values are nullable
    Object function = process(node.getFunction(), context);

    if (hasUnresolvedValue(values) || hasUnresolvedValue(function)) {
        ImmutableList.Builder<Expression> builder = ImmutableList.builder();
        for (int i = 0; i < values.size(); i++) {
            builder.add(toExpression(values.get(i), type(node.getValues().get(i))));
        }

        return new BindExpression(
                builder.build(),
                toExpression(function, type(node.getFunction())));
    }

    return MethodHandles.insertArguments((MethodHandle) function, 0, values.toArray());
}
 
Example 3
Source File: Selector.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a MethodHandle, which will use the meta class path.
 * This method is called only if no handle has been created before. This
 * is usually the case if the method selection failed.
 */
public void setMetaClassCallHandleIfNeeded(boolean standardMetaClass) {
    if (handle != null) return;
    useMetaClass = true;
    if (LOG_ENABLED) LOG.info("set meta class invocation path");
    Object receiver = getCorrectedReceiver();
    if (receiver instanceof Class) {
        handle = META_CLASS_INVOKE_STATIC_METHOD.bindTo(mc);
        if (LOG_ENABLED) LOG.info("use invokeStaticMethod with bound meta class");
    } else {
        handle = MOP_INVOKE_METHOD.bindTo(mc);
        if (LOG_ENABLED) LOG.info("use invokeMethod with bound meta class");

        if (receiver instanceof GroovyObject) {
            // if the meta class call fails we may still want to fall back to call
            // GroovyObject#invokeMethod if the receiver is a GroovyObject
            if (LOG_ENABLED) LOG.info("add MissingMethod handler for GrooObject#invokeMethod fallback path");
            handle = MethodHandles.catchException(handle, MissingMethodException.class, GROOVY_OBJECT_INVOKER);
        }
    }
    handle = MethodHandles.insertArguments(handle, 1, name);
    if (!spread) handle = handle.asCollector(Object[].class, targetType.parameterCount() - 1);
    if (LOG_ENABLED) LOG.info("bind method name and create collector for arguments");
}
 
Example 4
Source File: ObjectStreamClass.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** Binds the given stream field values to the given method handle. */
@SuppressWarnings("preview")
static MethodHandle bindCtrValues(MethodHandle ctrMH,
                                  ObjectStreamClass desc,
                                  ObjectInputStream.FieldValues fieldValues) {
    RecordComponent[] recordComponents;
    try {
        Class<?> cls = desc.forClass();
        PrivilegedExceptionAction<RecordComponent[]> pa = cls::getRecordComponents;
        recordComponents = AccessController.doPrivileged(pa);
    } catch (PrivilegedActionException e) {
        throw new InternalError(e.getCause());
    }

    Object[] args = new Object[recordComponents.length];
    for (int i = 0; i < recordComponents.length; i++) {
        String name = recordComponents[i].getName();
        Class<?> type= recordComponents[i].getType();
        Object o = streamFieldValue(name, type, desc, fieldValues);
        args[i] = o;
    }

    return MethodHandles.insertArguments(ctrMH, 0, args);
}
 
Example 5
Source File: TypeTransformers.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a type transformer applied at runtime.
 * This method handles transformations to String from GString,
 * array transformations and number based transformations
 */
protected static MethodHandle addTransformer(MethodHandle handle, int pos, Object arg, Class<?> parameter) {
    MethodHandle transformer = null;
    if (arg instanceof GString) {
        transformer = TO_STRING;
    } else if (arg instanceof Closure) {
        transformer = createSAMTransform(arg, parameter);
    } else if (Number.class.isAssignableFrom(parameter)) {
        transformer = selectNumberTransformer(parameter, arg);
    } else if (parameter.isArray()) {
        transformer = MethodHandles.insertArguments(AS_ARRAY, 1, parameter);
    }
    if (transformer == null)
        throw new GroovyBugError("Unknown transformation for argument " + arg + " at position " + pos + " with " + arg.getClass() + " for parameter of type " + parameter);
    return applyUnsharpFilter(handle, pos, transformer);
}
 
Example 6
Source File: Selector.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void handleSAM() {
    if (handle != null) return;

    if (!(args[0] instanceof Closure)) return;
    Method m = CachedSAMClass.getSAMMethod(staticTargetType);
    if (m == null) return;
    //TODO: optimize: add guard based on type Closure
    handle = MethodHandles.insertArguments(SAM_CONVERSION, 1, m, staticTargetType);
}
 
Example 7
Source File: BaseCallSite.java    From gravel with Apache License 2.0 5 votes vote down vote up
private MethodHandle getFallbackMethod() {
	try {
		final MethodType fallbackType = MethodType.genericMethodType(
				type.parameterCount()).insertParameterTypes(0,
				BaseCallSite.class);
		final MethodHandle fallbackHandle = MethodHandles.insertArguments(
				MethodHandles.lookup().findStatic(BaseCallSite.class,
						"invocationFallback", fallbackType), 0, this);
		return fallbackHandle.asType(type);
	} catch (NoSuchMethodException | IllegalAccessException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: DynamicMethodLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example 9
Source File: ChainedCallSite.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
 * chain.
 * @param relink the ultimate fallback for the chain (the {@code DynamicLinker}'s relink).
 * @return a method handle for prune-and-invoke
 */
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relink, final MethodHandle prune) {
    // Bind prune to (this, relink)
    final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relink);
    // Make it ignore all incoming arguments
    final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
    // Invoke prune, then invoke the call site target with original arguments
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
}
 
Example 10
Source File: ValueConversions.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle unbox(Wrapper wrap, int kind) {
    // kind 0 -> strongly typed with NPE
    // kind 1 -> strongly typed but zero for null,
    // kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
    // kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
    WrapperCache cache = UNBOX_CONVERSIONS[kind];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    switch (wrap) {
        case OBJECT:
        case VOID:
            throw new IllegalArgumentException("unbox "+wrap);
    }
    // look up the method
    String name = "unbox" + wrap.wrapperSimpleName();
    MethodType type = unboxType(wrap, kind);
    try {
        mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
    } catch (ReflectiveOperationException ex) {
        mh = null;
    }
    if (mh != null) {
        if (kind > 0) {
            boolean cast = (kind != 2);
            mh = MethodHandles.insertArguments(mh, 1, cast);
        }
        if (kind == 1) {  // casting but exact (null -> zero)
            mh = mh.asType(unboxType(wrap, 0));
        }
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
            + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
 
Example 11
Source File: ValueConversions.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle unbox(Wrapper wrap, int kind) {
    // kind 0 -> strongly typed with NPE
    // kind 1 -> strongly typed but zero for null,
    // kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
    // kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
    WrapperCache cache = UNBOX_CONVERSIONS[kind];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    switch (wrap) {
        case OBJECT:
        case VOID:
            throw new IllegalArgumentException("unbox "+wrap);
    }
    // look up the method
    String name = "unbox" + wrap.wrapperSimpleName();
    MethodType type = unboxType(wrap, kind);
    try {
        mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
    } catch (ReflectiveOperationException ex) {
        mh = null;
    }
    if (mh != null) {
        if (kind > 0) {
            boolean cast = (kind != 2);
            mh = MethodHandles.insertArguments(mh, 1, cast);
        }
        if (kind == 1) {  // casting but exact (null -> zero)
            mh = mh.asType(unboxType(wrap, 0));
        }
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
            + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
 
Example 12
Source File: DynamicLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
    // Make a bound MH of invoke() for this linker and call site
    final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
            relinkCount));
    // Make a MH that gathers all arguments to the invocation into an Object[]
    final MethodType type = callSite.getDescriptor().getMethodType();
    final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
            type.changeReturnType(MethodHandle.class)));
}
 
Example 13
Source File: ChainedCallSite.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
 * chain.
 * @param relinkAndInvoke the ultimate fallback for the chain passed from the dynamic linker.
 * @return a method handle for prune-and-invoke
 */
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relinkAndInvoke, final MethodHandle prune) {
    // Bind prune to (this, relink)
    final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relinkAndInvoke);
    // Make it ignore all incoming arguments
    final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
    // Invoke prune, then invoke the call site target with original arguments
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
}
 
Example 14
Source File: CatchExceptionTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void runTest() {
    if (Helper.IS_VERBOSE) {
        System.out.printf("CatchException(%s, isVararg=%b argsCount=%d "
                + "dropped=%d)%n",
                testCase, thrower.isVarargsCollector(),
                argsCount, dropped);
    }

    Helper.clear();

    Object[] args = Helper.randomArgs(
            argsCount, thrower.type().parameterArray());
    Object arg0 = Helper.MISSING_ARG;
    Object arg1 = testCase.thrown;
    if (argsCount > 0) {
        arg0 = args[0];
    }
    if (argsCount > 1) {
        args[1] = arg1;
    }
    Asserts.assertEQ(nargs, thrower.type().parameterCount());
    if (argsCount < nargs) {
        Object[] appendArgs = {arg0, arg1};
        appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
        thrower = MethodHandles.insertArguments(
                thrower, argsCount, appendArgs);
    }
    Asserts.assertEQ(argsCount, thrower.type().parameterCount());

    MethodHandle target = MethodHandles.catchException(
            testCase.filter(thrower), testCase.throwableClass,
            testCase.filter(catcher));

    Asserts.assertEQ(thrower.type(), target.type());
    Asserts.assertEQ(argsCount, target.type().parameterCount());

    Object returned;
    try {
        returned = target.invokeWithArguments(args);
    } catch (Throwable ex) {
        if (CodeCacheOverflowProcessor.isThrowableCausedByVME(ex)) {
            // This error will be treated by CodeCacheOverflowProcessor
            // to prevent the test from failing because of code cache overflow.
            throw new Error(ex);
        }
        testCase.assertCatch(ex);
        returned = ex;
    }

    testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
 
Example 15
Source File: NativeCalls.java    From es6draft with MIT License 4 votes vote down vote up
private static MethodHandle invalidCallHandle(String name, MethodType type) {
    MethodHandle mh = MethodHandles.insertArguments(invalidNativeCallMH, 0, name);
    return MethodHandles.dropArguments(mh, 1, type.dropParameterTypes(0, 1).parameterArray());
}
 
Example 16
Source File: AbstractJavaLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> ops) throws Exception {
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
            // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
            // runtime might not allow coercing at that call site.
            final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
            // Must have exactly two arguments: receiver and name
            assertParameterCount(callSiteDescriptor, 2);

            // What's below is basically:
            //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
            // only with a bunch of method signature adjustments. Basically, retrieve method getter
            // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
            // or delegate to next component's invocation.

            final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
                    AnnotatedDynamicMethod.class));
            final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
                    GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup(), linkerServices);
            final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
                    callSiteBoundMethodGetter);
            // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
            final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
                    MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
            // Since it's in the target of a fold, drop the unnecessary second argument
            // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
            final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
                    type.parameterType(1));
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, ops);

            final MethodHandle fallbackFolded;
            if(nextComponent == null) {
                // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
                fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
                        type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
            } else {
                // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
                // drop the extra argument resulting from fold and to change its return type to Object.
                final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
                final MethodType nextType = nextInvocation.type();
                fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
                        nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
            }

            // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
            final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                        IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
            if(nextComponent == null) {
                return getClassGuardedInvocationComponent(compositeGetter, type);
            }
            return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
        }
        case 3: {
            // Must have exactly one argument: receiver
            assertParameterCount(callSiteDescriptor, 1);
            // Fixed name
            final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
                    CallSiteDescriptor.NAME_OPERAND));
            if(annGetter == null) {
                // We have no such property, always delegate to the next component operation
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
            }
            final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
            // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
            // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
            // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
            // we're linking against a field getter, don't make the assumption.
            // NOTE: No delegation to the next component operation if we have a property with this name, even if its
            // value is null.
            final ValidationType validationType = annGetter.validationType;
            // TODO: we aren't using the type that declares the most generic getter here!
            return new GuardedInvocationComponent(getter, getGuard(validationType,
                    callSiteDescriptor.getMethodType()), clazz, validationType);
        }
        default: {
            // Can't do anything with more than 3 name components
            return null;
        }
    }
}
 
Example 17
Source File: BeanLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private MethodHandle bindToFixedKey(final MethodHandle handle) {
    return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
}
 
Example 18
Source File: AbstractJavaLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> ops) throws Exception {
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
            // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
            // runtime might not allow coercing at that call site.
            final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
            // Must have exactly two arguments: receiver and name
            assertParameterCount(callSiteDescriptor, 2);

            // What's below is basically:
            //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
            // only with a bunch of method signature adjustments. Basically, retrieve method getter
            // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
            // or delegate to next component's invocation.

            final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
                    AnnotatedDynamicMethod.class));
            final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
                    GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup(), linkerServices);
            final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
                    callSiteBoundMethodGetter);
            // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
            final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
                    MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
            // Since it's in the target of a fold, drop the unnecessary second argument
            // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
            final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
                    type.parameterType(1));
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, ops);

            final MethodHandle fallbackFolded;
            if(nextComponent == null) {
                // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
                fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
                        type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
            } else {
                // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
                // drop the extra argument resulting from fold and to change its return type to Object.
                final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
                final MethodType nextType = nextInvocation.type();
                fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
                        nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
            }

            // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
            final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                        IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
            if(nextComponent == null) {
                return getClassGuardedInvocationComponent(compositeGetter, type);
            }
            return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
        }
        case 3: {
            // Must have exactly one argument: receiver
            assertParameterCount(callSiteDescriptor, 1);
            // Fixed name
            final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
                    CallSiteDescriptor.NAME_OPERAND));
            if(annGetter == null) {
                // We have no such property, always delegate to the next component operation
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
            }
            final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
            // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
            // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
            // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
            // we're linking against a field getter, don't make the assumption.
            // NOTE: No delegation to the next component operation if we have a property with this name, even if its
            // value is null.
            final ValidationType validationType = annGetter.validationType;
            // TODO: we aren't using the type that declares the most generic getter here!
            return new GuardedInvocationComponent(getter, getGuard(validationType,
                    callSiteDescriptor.getMethodType()), clazz, validationType);
        }
        default: {
            // Can't do anything with more than 3 name components
            return null;
        }
    }
}
 
Example 19
Source File: BoundCallableLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
Example 20
Source File: BoundCallableLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    final Operation operation = NamedOperation.getBaseOperation(descriptor.getOperation());
    // We need to distinguish NEW from CALL because CALL sites have parameter list of the form
    // "callee, this, args", while NEW sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if (operation == StandardOperation.NEW) {
        isCall = false;
    } else if (operation == StandardOperation.CALL) {
        isCall = true;
    } else {
        // Only CALL and NEW are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for NEW, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}