jdk.internal.dynalink.beans.StaticClass Java Examples

The following examples show how to use jdk.internal.dynalink.beans.StaticClass. 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: NativeJavaImporter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private Object createProperty(final String name) {
    final int len = args.length;

    for (int i = len - 1; i > -1; i--) {
        final Object obj = args[i];

        if (obj instanceof StaticClass) {
            if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
                return obj;
            }
        } else if (obj instanceof NativeJavaPackage) {
            final String pkgName  = ((NativeJavaPackage)obj).getName();
            final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
            final Context context = Global.instance().getContext();
            try {
                return StaticClass.forClass(context.findClass(fullName));
            } catch (final ClassNotFoundException e) {
                // IGNORE
            }
        }
    }
    return null;
}
 
Example #2
Source File: NativeJavaImporter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Object createProperty(final String name) {
    final int len = args.length;

    for (int i = len - 1; i > -1; i--) {
        final Object obj = args[i];

        if (obj instanceof StaticClass) {
            if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
                return obj;
            }
        } else if (obj instanceof NativeJavaPackage) {
            final String pkgName  = ((NativeJavaPackage)obj).getName();
            final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
            final Context context = Global.instance().getContext();
            try {
                return StaticClass.forClass(context.findClass(fullName));
            } catch (final ClassNotFoundException e) {
                // IGNORE
            }
        }
    }
    return null;
}
 
Example #3
Source File: JavaAdapterFactory.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example #4
Source File: JavaAdapterFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example #5
Source File: NativeJavaPackage.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Handle creation of new attribute.
 * @param desc the call site descriptor
 * @param request the link request
 * @return Link to be invoked at call site.
 */
@Override
public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
    final String propertyName = desc.getNameToken(2);
    final String fullName     = name.isEmpty() ? propertyName : name + "." + propertyName;

    final Context context = Context.getContextTrusted();

    Class<?> javaClass = null;
    try {
        javaClass = context.findClass(fullName);
    } catch (final NoClassDefFoundError | ClassNotFoundException e) {
        //ignored
    }

    if (javaClass == null) {
        set(propertyName, new NativeJavaPackage(fullName, getProto()), false);
    } else {
        set(propertyName, StaticClass.forClass(javaClass), false);
    }

    return super.lookup(desc, request);
}
 
Example #6
Source File: ScriptRuntime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 *
 * @param obj first object to compare
 * @param clazz type to check against
 *
 * @return true if {@code obj} is an instanceof {@code clazz}
 */
public static boolean INSTANCEOF(final Object obj, final Object clazz) {
    if (clazz instanceof ScriptFunction) {
        if (obj instanceof ScriptObject) {
            return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
        }
        return false;
    }

    if (clazz instanceof StaticClass) {
        return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
    }

    if (clazz instanceof JSObject) {
        return ((JSObject)clazz).isInstance(obj);
    }

    // provide for reverse hook
    if (obj instanceof JSObject) {
        return ((JSObject)obj).isInstanceOf(clazz);
    }

    throw typeError("instanceof.on.non.object");
}
 
Example #7
Source File: JavaAdapterFactory.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example #8
Source File: ScriptRuntime.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 *
 * @param obj first object to compare
 * @param clazz type to check against
 *
 * @return true if {@code obj} is an instanceof {@code clazz}
 */
public static boolean INSTANCEOF(final Object obj, final Object clazz) {
    if (clazz instanceof ScriptFunction) {
        if (obj instanceof ScriptObject) {
            return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
        }
        return false;
    }

    if (clazz instanceof StaticClass) {
        return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
    }

    if (clazz instanceof JSObject) {
        return ((JSObject)clazz).isInstance(obj);
    }

    // provide for reverse hook
    if (obj instanceof JSObject) {
        return ((JSObject)obj).isInstanceOf(clazz);
    }

    throw typeError("instanceof.on.non.object");
}
 
Example #9
Source File: ScriptRuntime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 *
 * @param obj first object to compare
 * @param clazz type to check against
 *
 * @return true if {@code obj} is an instanceof {@code clazz}
 */
public static boolean INSTANCEOF(final Object obj, final Object clazz) {
    if (clazz instanceof ScriptFunction) {
        if (obj instanceof ScriptObject) {
            return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
        }
        return false;
    }

    if (clazz instanceof StaticClass) {
        return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
    }

    if (clazz instanceof JSObject) {
        return ((JSObject)clazz).isInstance(obj);
    }

    // provide for reverse hook
    if (obj instanceof JSObject) {
        return ((JSObject)obj).isInstanceOf(clazz);
    }

    throw typeError("instanceof.on.non.object");
}
 
Example #10
Source File: JavaAdapterFactory.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example #11
Source File: JavaAdapterFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example #12
Source File: NativeJavaImporter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Object createProperty(final String name) {
    final int len = args.length;

    for (int i = len - 1; i > -1; i--) {
        final Object obj = args[i];

        if (obj instanceof StaticClass) {
            if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
                return obj;
            }
        } else if (obj instanceof NativeJavaPackage) {
            final String pkgName  = ((NativeJavaPackage)obj).getName();
            final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
            final Context context = Global.instance().getContext();
            try {
                return StaticClass.forClass(context.findClass(fullName));
            } catch (final ClassNotFoundException e) {
                // IGNORE
            }
        }
    }
    return null;
}
 
Example #13
Source File: ScriptRuntime.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 *
 * @param obj first object to compare
 * @param clazz type to check against
 *
 * @return true if {@code obj} is an instanceof {@code clazz}
 */
public static boolean INSTANCEOF(final Object obj, final Object clazz) {
    if (clazz instanceof ScriptFunction) {
        if (obj instanceof ScriptObject) {
            return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
        }
        return false;
    }

    if (clazz instanceof StaticClass) {
        return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
    }

    if (clazz instanceof JSObject) {
        return ((JSObject)clazz).isInstance(obj);
    }

    // provide for reverse hook
    if (obj instanceof JSObject) {
        return ((JSObject)obj).isInstanceOf(clazz);
    }

    throw typeError("instanceof.on.non.object");
}
 
Example #14
Source File: NativeJavaImporter.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private Object createProperty(final String name) {
    final int len = args.length;

    for (int i = len - 1; i > -1; i--) {
        final Object obj = args[i];

        if (obj instanceof StaticClass) {
            if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
                return obj;
            }
        } else if (obj instanceof NativeJavaPackage) {
            final String pkgName  = ((NativeJavaPackage)obj).getName();
            final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
            final Context context = Global.instance().getContext();
            try {
                return StaticClass.forClass(context.findClass(fullName));
            } catch (final ClassNotFoundException e) {
                // IGNORE
            }
        }
    }
    return null;
}
 
Example #15
Source File: ScriptRuntime.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 *
 * @param obj first object to compare
 * @param clazz type to check against
 *
 * @return true if {@code obj} is an instanceof {@code clazz}
 */
public static boolean INSTANCEOF(final Object obj, final Object clazz) {
    if (clazz instanceof ScriptFunction) {
        if (obj instanceof ScriptObject) {
            return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
        }
        return false;
    }

    if (clazz instanceof StaticClass) {
        return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
    }

    if (clazz instanceof JSObject) {
        return ((JSObject)clazz).isInstance(obj);
    }

    // provide for reverse hook
    if (obj instanceof JSObject) {
        return ((JSObject)obj).isInstanceOf(clazz);
    }

    throw typeError("instanceof.on.non.object");
}
 
Example #16
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 #17
Source File: NashornStaticClassLinker.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 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))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // 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 #18
Source File: NashornStaticClassLinker.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) 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))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // 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 #19
Source File: Bootstrap.java    From openjdk-8-source 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 ||
        ((obj instanceof JSObject) && ((JSObject)obj).isFunction()) ||
        isDynamicMethod(obj) ||
        isFunctionalInterfaceObject(obj) ||
        obj instanceof StaticClass;
}
 
Example #20
Source File: NativeJava.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns name of a java type {@link StaticClass}.
 * @param self not used
 * @param type the type whose name is returned
 * @return name of the given type
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object typeName(final Object self, final Object type) {
    if (type instanceof StaticClass) {
        return ((StaticClass)type).getRepresentedClass().getName();
    } else if (type instanceof Class) {
        return ((Class<?>)type).getName();
    } else {
        return UNDEFINED;
    }
}
 
Example #21
Source File: JavaAdapterFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
StaticClass getAdapterClass(final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    if(adaptationResult.getOutcome() != AdaptationResult.Outcome.SUCCESS) {
        throw adaptationResult.typeError();
    }
    return classOverrides == null ? getInstanceAdapterClass(protectionDomain) :
        getClassAdapterClass(classOverrides, protectionDomain);
}
 
Example #22
Source File: ScriptUtils.java    From hottub 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 #23
Source File: JavaAdapterFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static StaticClass getAdapterClassFor(final Class<?>[] types, final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    assert types != null && types.length > 0;
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        for (final Class<?> type : types) {
            // check for restricted package access
            Context.checkPackageAccess(type);
            // check for classes, interfaces in reflection
            ReflectionCheckLinker.checkReflectionAccess(type, true);
        }
    }
    return getAdapterInfo(types).getAdapterClass(classOverrides, protectionDomain);
}
 
Example #24
Source File: JavaAdapterFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
StaticClass getAdapterClass(final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    if(adaptationResult.getOutcome() != AdaptationResult.Outcome.SUCCESS) {
        throw adaptationResult.typeError();
    }
    return classOverrides == null ? getInstanceAdapterClass(protectionDomain) :
        getClassAdapterClass(classOverrides, protectionDomain);
}
 
Example #25
Source File: JavaAdapterFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private StaticClass getClassAdapterClass(final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    JavaAdapterServices.setClassOverrides(classOverrides);
    try {
        return classAdapterGenerator.generateClass(commonLoader, protectionDomain);
    } finally {
        JavaAdapterServices.setClassOverrides(null);
    }
}
 
Example #26
Source File: JavaAdapterFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
StaticClass getAdapterClass(final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    if(adaptationResult.getOutcome() != AdaptationResult.Outcome.SUCCESS) {
        throw adaptationResult.typeError();
    }
    return classOverrides == null ? getInstanceAdapterClass(protectionDomain) :
        getClassAdapterClass(classOverrides, protectionDomain);
}
 
Example #27
Source File: JavaAdapterClassLoader.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Loads the generated adapter class into the JVM.
 * @param parentLoader the parent class loader for the generated class loader
 * @param protectionDomain the protection domain for the generated class
 * @return the generated adapter class
 */
StaticClass generateClass(final ClassLoader parentLoader, final ProtectionDomain protectionDomain) {
    assert protectionDomain != null;
    return AccessController.doPrivileged(new PrivilegedAction<StaticClass>() {
        @Override
        public StaticClass run() {
            try {
                return StaticClass.forClass(Class.forName(className, true, createClassLoader(parentLoader, protectionDomain)));
            } catch (final ClassNotFoundException e) {
                throw new AssertionError(e); // cannot happen
            }
        }
    }, CREATE_LOADER_ACC_CTXT);
}
 
Example #28
Source File: ScriptUtils.java    From TencentKona-8 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 #29
Source File: NativeJava.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns name of a java type {@link StaticClass}.
 * @param self not used
 * @param type the type whose name is returned
 * @return name of the given type
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object typeName(final Object self, final Object type) {
    if (type instanceof StaticClass) {
        return ((StaticClass)type).getRepresentedClass().getName();
    } else if (type instanceof Class) {
        return ((Class<?>)type).getName();
    } else {
        return UNDEFINED;
    }
}
 
Example #30
Source File: JavaAdapterClassLoader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Loads the generated adapter class into the JVM.
 * @param parentLoader the parent class loader for the generated class loader
 * @param protectionDomain the protection domain for the generated class
 * @return the generated adapter class
 */
StaticClass generateClass(final ClassLoader parentLoader, final ProtectionDomain protectionDomain) {
    assert protectionDomain != null;
    return AccessController.doPrivileged(new PrivilegedAction<StaticClass>() {
        @Override
        public StaticClass run() {
            try {
                return StaticClass.forClass(Class.forName(className, true, createClassLoader(parentLoader, protectionDomain)));
            } catch (final ClassNotFoundException e) {
                throw new AssertionError(e); // cannot happen
            }
        }
    }, CREATE_LOADER_ACC_CTXT);
}