jdk.internal.dynalink.beans.BeansLinker Java Examples

The following examples show how to use jdk.internal.dynalink.beans.BeansLinker. 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: Bootstrap.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #2
Source File: Bootstrap.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #3
Source File: Bootstrap.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #4
Source File: Bootstrap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) ||
            callable instanceof StaticClass ||
            isFunctionalInterfaceObject(callable)) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #5
Source File: Bootstrap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #6
Source File: Bootstrap.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) ||
            callable instanceof StaticClass ||
            isFunctionalInterfaceObject(callable)) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #7
Source File: Bootstrap.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #8
Source File: Bootstrap.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) ||
            callable instanceof StaticClass ||
            isFunctionalInterfaceObject(callable)) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #9
Source File: Bootstrap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #10
Source File: Bootstrap.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if the given object is a "callable"
 * @param obj object to be checked for callability
 * @return true if the obj is callable
 */
public static boolean isCallable(final Object obj) {
    if (obj == ScriptRuntime.UNDEFINED || obj == null) {
        return false;
    }

    return obj instanceof ScriptFunction ||
        isJSObjectFunction(obj) ||
        BeansLinker.isDynamicMethod(obj) ||
        obj instanceof BoundCallable ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #11
Source File: Bootstrap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) ||
            callable instanceof StaticClass ||
            isFunctionalInterfaceObject(callable)) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #12
Source File: Bootstrap.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given object is a strict callable
 * @param callable the callable object to be checked for strictness
 * @return true if the obj is a strict callable, false if it is a non-strict callable.
 * @throws ECMAException with {@code TypeError} if the object is not a callable.
 */
public static boolean isStrictCallable(final Object callable) {
    if (callable instanceof ScriptFunction) {
        return ((ScriptFunction)callable).isStrict();
    } else if (isJSObjectFunction(callable)) {
        return ((JSObject)callable).isStrictFunction();
    } else if (callable instanceof BoundCallable) {
        return isStrictCallable(((BoundCallable)callable).getCallable());
    } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) {
        return false;
    }
    throw notFunction(callable);
}
 
Example #13
Source File: JavaSuperAdapterLinker.java    From openjdk-8 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type R(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(invType.returnType(),
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type R(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // R(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #14
Source File: NashornBottomLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "call":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "callMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getMethod":
        // evaluate to undefined, later on Undefined will take care of throwing TypeError
        return getInvocation(MH.dropArguments(GET_UNDEFINED.get(TYPE_OBJECT_INDEX), 0, Object.class), self, linkerServices, desc);
    case "getProp":
    case "getElem":
        if(NashornCallSiteDescriptor.isOptimistic(desc)) {
            throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem": {
        final boolean strict = NashornCallSiteDescriptor.isStrict(desc);
        if (strict) {
            throw typeError("cant.set.property", getArgument(linkRequest), ScriptRuntime.safeToString(self));
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    }
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #15
Source File: CallerSensitiveTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCallerSensitive() {
    BeansLinker.getLinkerForClass(ClassLoaderAware.class);
}
 
Example #16
Source File: JavaSuperAdapterLinker.java    From openjdk-8-source 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type R(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(invType.returnType(),
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type R(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // R(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #17
Source File: BoundDynamicMethodLinker.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final Object objBoundDynamicMethod = linkRequest.getReceiver();
    if(!(objBoundDynamicMethod instanceof BoundDynamicMethod)) {
        return null;
    }

    final BoundDynamicMethod boundDynamicMethod = (BoundDynamicMethod)objBoundDynamicMethod;
    final Object dynamicMethod = boundDynamicMethod.getDynamicMethod();
    final Object boundThis = boundDynamicMethod.getBoundThis();

    // Replace arguments (boundDynamicMethod, this, ...) => (dynamicMethod, boundThis, ...) when delegating to
    // BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = dynamicMethod;
    args[1] = boundThis;

    // Use R(T0, T1, ...) => R(dynamicMethod.class, boundThis.class, ...) call site type when delegating to
    // BeansLinker.
    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    final MethodType type = descriptor.getMethodType();
    final Class<?> dynamicMethodClass = dynamicMethod.getClass();
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(
            type.changeParameterType(0, dynamicMethodClass).changeParameterType(1, boundThis.getClass()));

    // Delegate to BeansLinker
    final GuardedInvocation inv = NashornBeansLinker.getGuardedInvocation(BeansLinker.getLinkerForClass(dynamicMethodClass),
            linkRequest.replaceArguments(newDescriptor, args), linkerServices);
    if(inv == null) {
        return null;
    }

    // Bind (dynamicMethod, boundThis) to the handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0, dynamicMethod, boundThis);
    final Class<?> p0Type = type.parameterType(0);
    // Ignore incoming (boundDynamicMethod, this)
    final MethodHandle droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    // Identity guard on boundDynamicMethod object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundDynamicMethod);

    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
Example #18
Source File: JavaSuperAdapterLinker.java    From TencentKona-8 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type Object(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(Object.class,
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type Object(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // Object(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #19
Source File: BoundDynamicMethodLinker.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final Object objBoundDynamicMethod = linkRequest.getReceiver();
    if(!(objBoundDynamicMethod instanceof BoundDynamicMethod)) {
        return null;
    }

    final BoundDynamicMethod boundDynamicMethod = (BoundDynamicMethod)objBoundDynamicMethod;
    final Object dynamicMethod = boundDynamicMethod.getDynamicMethod();
    final Object boundThis = boundDynamicMethod.getBoundThis();

    // Replace arguments (boundDynamicMethod, this, ...) => (dynamicMethod, boundThis, ...) when delegating to
    // BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = dynamicMethod;
    args[1] = boundThis;

    // Use R(T0, T1, ...) => R(dynamicMethod.class, boundThis.class, ...) call site type when delegating to
    // BeansLinker.
    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    final MethodType type = descriptor.getMethodType();
    final Class<?> dynamicMethodClass = dynamicMethod.getClass();
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(
            type.changeParameterType(0, dynamicMethodClass).changeParameterType(1, boundThis.getClass()));

    // Delegate to BeansLinker
    final GuardedInvocation inv = BeansLinker.getLinkerForClass(dynamicMethodClass).getGuardedInvocation(
            linkRequest.replaceArguments(newDescriptor, args), linkerServices);
    if(inv == null) {
        return null;
    }

    // Bind (dynamicMethod, boundThis) to the handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0, dynamicMethod, boundThis);
    final Class<?> p0Type = type.parameterType(0);
    // Ignore incoming (boundDynamicMethod, this)
    final MethodHandle droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    // Identity guard on boundDynamicMethod object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundDynamicMethod);

    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
Example #20
Source File: NashornBottomLinker.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "call":
        // Support dyn:call on any object that supports some @FunctionalInterface
        // annotated interface. This way Java method, constructor references or
        // implementations of java.util.function.* interfaces can be called as though
        // those are script functions.
        final Method m = getFunctionalInterfaceMethod(self.getClass());
        if (m != null) {
            final MethodType callType = desc.getMethodType();
            // 'callee' and 'thiz' passed from script + actual arguments
            if (callType.parameterCount() != m.getParameterCount() + 2) {
                throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
            }
            return new GuardedInvocation(
                    // drop 'thiz' passed from the script.
                    MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
                    Guards.getInstanceOfGuard(m.getDeclaringClass())).asType(callType);
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "callMethod":
    case "getMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getProp":
    case "getElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #21
Source File: BoundDynamicMethodLinker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final Object objBoundDynamicMethod = linkRequest.getReceiver();
    if(!(objBoundDynamicMethod instanceof BoundDynamicMethod)) {
        return null;
    }

    final BoundDynamicMethod boundDynamicMethod = (BoundDynamicMethod)objBoundDynamicMethod;
    final Object dynamicMethod = boundDynamicMethod.getDynamicMethod();
    final Object boundThis = boundDynamicMethod.getBoundThis();

    // Replace arguments (boundDynamicMethod, this, ...) => (dynamicMethod, boundThis, ...) when delegating to
    // BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = dynamicMethod;
    args[1] = boundThis;

    // Use R(T0, T1, ...) => R(dynamicMethod.class, boundThis.class, ...) call site type when delegating to
    // BeansLinker.
    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    final MethodType type = descriptor.getMethodType();
    final Class<?> dynamicMethodClass = dynamicMethod.getClass();
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(
            type.changeParameterType(0, dynamicMethodClass).changeParameterType(1, boundThis.getClass()));

    // Delegate to BeansLinker
    final GuardedInvocation inv = NashornBeansLinker.getGuardedInvocation(BeansLinker.getLinkerForClass(dynamicMethodClass),
            linkRequest.replaceArguments(newDescriptor, args), linkerServices);
    if(inv == null) {
        return null;
    }

    // Bind (dynamicMethod, boundThis) to the handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0, dynamicMethod, boundThis);
    final Class<?> p0Type = type.parameterType(0);
    // Ignore incoming (boundDynamicMethod, this)
    final MethodHandle droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    // Identity guard on boundDynamicMethod object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundDynamicMethod);

    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
Example #22
Source File: NashornBottomLinker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "call":
        // Support dyn:call on any object that supports some @FunctionalInterface
        // annotated interface. This way Java method, constructor references or
        // implementations of java.util.function.* interfaces can be called as though
        // those are script functions.
        final Method m = getFunctionalInterfaceMethod(self.getClass());
        if (m != null) {
            final MethodType callType = desc.getMethodType();
            // 'callee' and 'thiz' passed from script + actual arguments
            if (callType.parameterCount() != m.getParameterCount() + 2) {
                throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
            }
            return new GuardedInvocation(
                    // drop 'thiz' passed from the script.
                    MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
                    Guards.getInstanceOfGuard(m.getDeclaringClass())).asType(callType);
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "callMethod":
    case "getMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getProp":
    case "getElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #23
Source File: JavaSuperAdapterLinker.java    From nashorn 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = BeansLinker.getLinkerForClass(adapterClass).getGuardedInvocation(
            linkRequest.replaceArguments(newDescriptor, args), linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type R(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(invType.returnType(),
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type R(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // R(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #24
Source File: NashornBottomLinker.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "call":
        // Support dyn:call on any object that supports some @FunctionalInterface
        // annotated interface. This way Java method, constructor references or
        // implementations of java.util.function.* interfaces can be called as though
        // those are script functions.
        final Method m = getFunctionalInterfaceMethod(self.getClass());
        if (m != null) {
            final MethodType callType = desc.getMethodType();
            // 'callee' and 'thiz' passed from script + actual arguments
            //if (callType.parameterCount() != m.getParameterCount() + 2) {
            if (callType.parameterCount() != m.getParameterTypes().length + 2) {
                throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
            }
            return new GuardedInvocation(
                    // drop 'thiz' passed from the script.
                    MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
                    Guards.getInstanceOfGuard(m.getDeclaringClass())).asType(callType);
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    case "callMethod":
    case "getMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getProp":
    case "getElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem":
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #25
Source File: JavaSuperAdapterLinker.java    From jdk8u_nashorn 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type Object(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(Object.class,
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type Object(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // Object(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #26
Source File: CallerSensitiveTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCallerSensitive() {
    BeansLinker.getLinkerForClass(ClassLoaderAware.class);
}
 
Example #27
Source File: NashornBottomLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "call":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "callMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getMethod":
        // evaluate to undefined, later on Undefined will take care of throwing TypeError
        return getInvocation(MH.dropArguments(GET_UNDEFINED.get(TYPE_OBJECT_INDEX), 0, Object.class), self, linkerServices, desc);
    case "getProp":
    case "getElem":
        if(NashornCallSiteDescriptor.isOptimistic(desc)) {
            throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem": {
        final boolean strict = NashornCallSiteDescriptor.isStrict(desc);
        if (strict) {
            throw typeError("cant.set.property", getArgument(linkRequest), ScriptRuntime.safeToString(self));
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    }
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #28
Source File: JavaSuperAdapterLinker.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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type Object(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(Object.class,
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type Object(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // Object(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}
 
Example #29
Source File: CallerSensitiveTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCallerSensitive() {
    BeansLinker.getLinkerForClass(ClassLoaderAware.class);
}
 
Example #30
Source File: NashornBottomLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final Object self = linkRequest.getReceiver();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "call":
        if(BeansLinker.isDynamicConstructor(self)) {
            throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
        }
        if(BeansLinker.isDynamicMethod(self)) {
            throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
        }
        throw typeError("not.a.function", desc.getFunctionErrorMessage(self));
    case "callMethod":
        throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
    case "getMethod":
        // evaluate to undefined, later on Undefined will take care of throwing TypeError
        return getInvocation(MH.dropArguments(GET_UNDEFINED.get(TYPE_OBJECT_INDEX), 0, Object.class), self, linkerServices, desc);
    case "getProp":
    case "getElem":
        if(NashornCallSiteDescriptor.isOptimistic(desc)) {
            throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
    case "setProp":
    case "setElem": {
        final boolean strict = NashornCallSiteDescriptor.isStrict(desc);
        if (strict) {
            throw typeError("cant.set.property", getArgument(linkRequest), ScriptRuntime.safeToString(self));
        }
        if (desc.getOperand() != null) {
            return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
    }
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}