jdk.internal.dynalink.linker.LinkerServices Java Examples

The following examples show how to use jdk.internal.dynalink.linker.LinkerServices. 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: AbstractJavaLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #2
Source File: BeansLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();
    final int l = callSiteDescriptor.getNameTokenCount();
    // All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn"
    if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) {
        return null;
    }

    final Object receiver = request.getReceiver();
    if(receiver == null) {
        // Can't operate on null
        return null;
    }
    return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices);
}
 
Example #3
Source File: AbstractJavaLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> operations) throws Exception {
    if(operations.isEmpty()) {
        return null;
    }
    final String op = operations.get(0);
    // Either dyn:getProp:name(this) or dyn:getProp(this, name)
    if("getProp".equals(op)) {
        return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value)
    if("setProp".equals(op)) {
        return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // Either dyn:getMethod:name(this), or dyn:getMethod(this, name)
    if("getMethod".equals(op)) {
        return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    return null;
}
 
Example #4
Source File: AbstractJavaLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
protected GuardedInvocationComponent getGuardedInvocationComponent(CallSiteDescriptor callSiteDescriptor,
        LinkerServices linkerServices, List<String> operations) throws Exception {
    if(operations.isEmpty()) {
        return null;
    }
    final String op = operations.get(0);
    // Either dyn:getProp:name(this) or dyn:getProp(this, name)
    if("getProp".equals(op)) {
        return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value)
    if("setProp".equals(op)) {
        return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // Either dyn:getMethod:name(this), or dyn:getMethod(this, name)
    if("getMethod".equals(op)) {
        return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    return null;
}
 
Example #5
Source File: JSObjectLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #6
Source File: CompositeTypeBasedGuardingDynamicLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, final LinkerServices linkerServices)
        throws Exception {
    final Object obj = linkRequest.getReceiver();
    if(obj == null) {
        return null;
    }
    for(TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) {
        final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices);
        if(invocation != null) {
            return invocation;
        }
    }
    return null;
}
 
Example #7
Source File: DynamicLinker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new dynamic linker.
 *
 * @param linkerServices the linkerServices used by the linker, created by the factory.
 * @param prelinkFilter see {@link DynamicLinkerFactory#setPrelinkFilter(GuardedInvocationFilter)}
 * @param runtimeContextArgCount see {@link DynamicLinkerFactory#setRuntimeContextArgCount(int)}
 */
DynamicLinker(final LinkerServices linkerServices, final GuardedInvocationFilter prelinkFilter, final int runtimeContextArgCount,
        final boolean syncOnRelink, final int unstableRelinkThreshold) {
    if(runtimeContextArgCount < 0) {
        throw new IllegalArgumentException("runtimeContextArgCount < 0");
    }
    if(unstableRelinkThreshold < 0) {
        throw new IllegalArgumentException("unstableRelinkThreshold < 0");
    }
    this.linkerServices = linkerServices;
    this.prelinkFilter = prelinkFilter;
    this.runtimeContextArgCount = runtimeContextArgCount;
    this.syncOnRelink = syncOnRelink;
    this.unstableRelinkThreshold = unstableRelinkThreshold;
}
 
Example #8
Source File: BeanLinker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> operations) throws Exception {
    final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor,
            linkerServices, operations);
    if(superGic != null) {
        return superGic;
    }
    if(operations.isEmpty()) {
        return null;
    }
    final String op = operations.get(0);
    // dyn:getElem(this, id)
    // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide
    // conversion from call site argument type though.
    if("getElem".equals(op)) {
        return getElementGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    if("setElem".equals(op)) {
        return getElementSetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // dyn:getLength(this) (works on Java arrays, collections, and maps)
    if("getLength".equals(op)) {
        return getLengthGetter(callSiteDescriptor);
    }
    return null;
}
 
Example #9
Source File: ScriptUtils.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert the given object to the given type.
 *
 * @param obj object to be converted
 * @param type destination type to convert to. type is either a Class
 * or nashorn representation of a Java type returned by Java.type() call in script.
 * @return converted object
 */
public static Object convert(final Object obj, final Object type) {
    if (obj == null) {
        return null;
    }

    final Class<?> clazz;
    if (type instanceof Class) {
        clazz = (Class<?>)type;
    } else if (type instanceof StaticClass) {
        clazz = ((StaticClass)type).getRepresentedClass();
    } else {
        throw new IllegalArgumentException("type expected");
    }

    final LinkerServices linker = Bootstrap.getLinkerServices();
    final Object objToConvert = unwrap(obj);
    final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(),  clazz);
    if (converter == null) {
        // no supported conversion!
        throw new UnsupportedOperationException("conversion not supported");
    }

    try {
        return converter.invoke(objToConvert);
    } catch (final RuntimeException | Error e) {
        throw e;
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
Example #10
Source File: ClassString.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the supplied method is applicable to actual parameter classes represented by this ClassString
 * object.
 *
 */
private boolean isApplicable(final MethodHandle method, final LinkerServices linkerServices, final boolean varArg) {
    final Class<?>[] formalTypes = method.type().parameterArray();
    final int cl = classes.length;
    final int fl = formalTypes.length - (varArg ? 1 : 0);
    if(varArg) {
        if(cl < fl) {
            return false;
        }
    } else {
        if(cl != fl) {
            return false;
        }
    }
    // Starting from 1 as we ignore the receiver type
    for(int i = 1; i < fl; ++i) {
        if(!canConvert(linkerServices, classes[i], formalTypes[i])) {
            return false;
        }
    }
    if(varArg) {
        final Class<?> varArgType = formalTypes[fl].getComponentType();
        for(int i = fl; i < cl; ++i) {
            if(!canConvert(linkerServices, classes[i], varArgType)) {
                return false;
            }
        }
    }
    return true;
}
 
Example #11
Source File: NashornPrimitiveLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest request = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context

    final Object self = request.getReceiver();
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor) request.getCallSiteDescriptor();

    return Bootstrap.asTypeSafeReturn(Global.primitiveLookup(request, self), linkerServices, desc);
}
 
Example #12
Source File: BeanLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example #13
Source File: StaticClassLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, LinkerServices linkerServices) throws Exception {
    final Object receiver = request.getReceiver();
    if(receiver instanceof StaticClass) {
        return linkers.get(((StaticClass)receiver).getRepresentedClass()).getGuardedInvocation(request,
                linkerServices);
    }
    return null;
}
 
Example #14
Source File: CompositeTypeBasedGuardingDynamicLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices)
        throws Exception {
    final Object obj = linkRequest.getReceiver();
    if(obj == null) {
        return null;
    }
    for(final TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) {
        final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices);
        if(invocation != null) {
            return invocation;
        }
    }
    return null;
}
 
Example #15
Source File: BeanLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example #16
Source File: MaximallySpecific.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static Comparison compare(final Class<?> c1, final Class<?> c2, final Class<?>[] argTypes, final int i, final LinkerServices cmp) {
    if(cmp != null) {
        final Comparison c = cmp.compareConversion(argTypes[i], c1, c2);
        if(c != Comparison.INDETERMINATE) {
            return c;
        }
    }
    if(TypeUtilities.isSubtype(c1, c2)) {
        return Comparison.TYPE_1_BETTER;
    } if(TypeUtilities.isSubtype(c2, c1)) {
        return Comparison.TYPE_2_BETTER;
    }
    return Comparison.INDETERMINATE;
}
 
Example #17
Source File: ClassString.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns all methods that are applicable to actual parameter classes represented by this ClassString object.
 */
LinkedList<MethodHandle> getApplicables(List<MethodHandle> methods, LinkerServices linkerServices, boolean varArg) {
    final LinkedList<MethodHandle> list = new LinkedList<>();
    for(final MethodHandle member: methods) {
        if(isApplicable(member, linkerServices, varArg)) {
            list.add(member);
        }
    }
    return list;
}
 
Example #18
Source File: OverloadedMethod.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
OverloadedMethod(List<MethodHandle> methodHandles, OverloadedDynamicMethod parent, MethodType callSiteType,
        LinkerServices linkerServices) {
    this.parent = parent;
    this.callSiteType = callSiteType;
    this.linkerServices = linkerServices;

    fixArgMethods = new ArrayList<>(methodHandles.size());
    varArgMethods = new ArrayList<>(methodHandles.size());
    final int argNum = callSiteType.parameterCount();
    for(MethodHandle mh: methodHandles) {
        if(mh.isVarargsCollector()) {
            final MethodHandle asFixed = mh.asFixedArity();
            if(argNum == asFixed.type().parameterCount()) {
                fixArgMethods.add(asFixed);
            }
            varArgMethods.add(mh);
        } else {
            fixArgMethods.add(mh);
        }
    }
    fixArgMethods.trimToSize();
    varArgMethods.trimToSize();

    final MethodHandle bound = SELECT_METHOD.bindTo(this);
    final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType(
            callSiteType.changeReturnType(MethodHandle.class));
    invoker = MethodHandles.foldArguments(MethodHandles.exactInvoker(callSiteType), collecting);
}
 
Example #19
Source File: NashornBeansLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object self = linkRequest.getReceiver();
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if (self instanceof ConsString) {
        // In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
        final Object[] arguments = linkRequest.getArguments();
        arguments[0] = "";
        final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
        final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
        // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
        return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
    }

    if (self != null && "call".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // 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 String name = getFunctionalInterfaceMethodName(self.getClass());
        if (name != null) {
            final MethodType callType = desc.getMethodType();
            // drop callee (Undefined ScriptFunction) and change the request to be dyn:callMethod:<name>
            final NashornCallSiteDescriptor newDesc = NashornCallSiteDescriptor.get(desc.getLookup(),
                    "dyn:callMethod:" + name, desc.getMethodType().dropParameterTypes(1, 2),
                    NashornCallSiteDescriptor.getFlags(desc));
            final GuardedInvocation gi = getGuardedInvocation(beansLinker,
                    linkRequest.replaceArguments(newDesc, linkRequest.getArguments()),
                    new NashornBeansLinkerServices(linkerServices));

            // drop 'thiz' passed from the script.
            return gi.replaceMethods(
                MH.dropArguments(linkerServices.filterInternalObjects(gi.getInvocation()), 1, callType.parameterType(1)),
                gi.getGuard());
        }
    }
    return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
}
 
Example #20
Source File: BeanLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> operations) throws Exception {
    final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor,
            linkerServices, operations);
    if(superGic != null) {
        return superGic;
    }
    if(operations.isEmpty()) {
        return null;
    }
    final String op = operations.get(0);
    // dyn:getElem(this, id)
    // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide
    // conversion from call site argument type though.
    if("getElem".equals(op)) {
        return getElementGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    if("setElem".equals(op)) {
        return getElementSetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // dyn:getLength(this) (works on Java arrays, collections, and maps)
    if("getLength".equals(op)) {
        return getLengthGetter(callSiteDescriptor);
    }
    return null;
}
 
Example #21
Source File: ReflectionCheckLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
        throws Exception {
    checkLinkRequest(origRequest);
    // let the next linker deal with actual linking
    return null;
}
 
Example #22
Source File: NashornBottomLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices)
        throws Exception {
    final Object self = linkRequest.getReceiver();

    if (self == null) {
        return linkNull(linkRequest);
    }

    // None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below
    // this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug.
    assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName();

    return linkBean(linkRequest, linkerServices);
}
 
Example #23
Source File: BeanLinker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocationComponent getGuardedInvocationComponent(CallSiteDescriptor callSiteDescriptor,
        LinkerServices linkerServices, List<String> operations) throws Exception {
    final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor,
            linkerServices, operations);
    if(superGic != null) {
        return superGic;
    }
    if(operations.isEmpty()) {
        return null;
    }
    final String op = operations.get(0);
    // dyn:getElem(this, id)
    // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide
    // conversion from call site argument type though.
    if("getElem".equals(op)) {
        return getElementGetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    if("setElem".equals(op)) {
        return getElementSetter(callSiteDescriptor, linkerServices, pop(operations));
    }
    // dyn:getLength(this) (works on Java arrays, collections, and maps)
    if("getLength".equals(op)) {
        return getLengthGetter(callSiteDescriptor);
    }
    return null;
}
 
Example #24
Source File: StaticClassLinker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final Object receiver = request.getReceiver();
    if(receiver instanceof StaticClass) {
        return linkers.get(((StaticClass)receiver).getRepresentedClass()).getGuardedInvocation(request,
                linkerServices);
    }
    return null;
}
 
Example #25
Source File: MaximallySpecific.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static Comparison isMoreSpecific(final MethodType t1, final MethodType t2, final boolean varArgs, final Class<?>[] argTypes,
        final LinkerServices ls) {
    final int pc1 = t1.parameterCount();
    final int pc2 = t2.parameterCount();
    assert varArgs || (pc1 == pc2) && (argTypes == null || argTypes.length == pc1);
    assert (argTypes == null) == (ls == null);
    final int maxPc = Math.max(Math.max(pc1, pc2), argTypes == null ? 0 : argTypes.length);
    boolean t1MoreSpecific = false;
    boolean t2MoreSpecific = false;
    // NOTE: Starting from 1 as overloaded method resolution doesn't depend on 0th element, which is the type of
    // 'this'. We're only dealing with instance methods here, not static methods. Actually, static methods will have
    // a fake 'this' of type StaticClass.
    for(int i = 1; i < maxPc; ++i) {
        final Class<?> c1 = getParameterClass(t1, pc1, i, varArgs);
        final Class<?> c2 = getParameterClass(t2, pc2, i, varArgs);
        if(c1 != c2) {
            final Comparison cmp = compare(c1, c2, argTypes, i, ls);
            if(cmp == Comparison.TYPE_1_BETTER && !t1MoreSpecific) {
                t1MoreSpecific = true;
                if(t2MoreSpecific) {
                    return Comparison.INDETERMINATE;
                }
            }
            if(cmp == Comparison.TYPE_2_BETTER && !t2MoreSpecific) {
                t2MoreSpecific = true;
                if(t1MoreSpecific) {
                    return Comparison.INDETERMINATE;
                }
            }
        }
    }
    if(t1MoreSpecific) {
        return Comparison.TYPE_1_BETTER;
    } else if(t2MoreSpecific) {
        return Comparison.TYPE_2_BETTER;
    }
    return Comparison.INDETERMINATE;
}
 
Example #26
Source File: NashornStaticClassLinker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();
    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #27
Source File: BeanLinker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example #28
Source File: MaximallySpecific.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static Comparison compare(final Class<?> c1, final Class<?> c2, final Class<?>[] argTypes, final int i, final LinkerServices cmp) {
    if(cmp != null) {
        final Comparison c = cmp.compareConversion(argTypes[i], c1, c2);
        if(c != Comparison.INDETERMINATE) {
            return c;
        }
    }
    if(TypeUtilities.isSubtype(c1, c2)) {
        return Comparison.TYPE_1_BETTER;
    } if(TypeUtilities.isSubtype(c2, c1)) {
        return Comparison.TYPE_2_BETTER;
    }
    return Comparison.INDETERMINATE;
}
 
Example #29
Source File: CompositeTypeBasedGuardingDynamicLinker.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, final LinkerServices linkerServices)
        throws Exception {
    final Object obj = linkRequest.getReceiver();
    if(obj == null) {
        return null;
    }
    for(TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) {
        final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices);
        if(invocation != null) {
            return invocation;
        }
    }
    return null;
}
 
Example #30
Source File: ClassString.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns all methods that are applicable to actual parameter classes represented by this ClassString object.
 */
LinkedList<MethodHandle> getApplicables(final List<MethodHandle> methods, final LinkerServices linkerServices, final boolean varArg) {
    final LinkedList<MethodHandle> list = new LinkedList<>();
    for(final MethodHandle member: methods) {
        if(isApplicable(member, linkerServices, varArg)) {
            list.add(member);
        }
    }
    return list;
}