Search in sources :

Example 26 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.

the class VM method JVM_GetPermittedSubclasses.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class[].class)
public StaticObject JVM_GetPermittedSubclasses(@JavaType(Class.class) StaticObject self) {
    Klass k = self.getMirrorKlass();
    if (!(k instanceof ObjectKlass)) {
        return StaticObject.NULL;
    }
    ObjectKlass klass = (ObjectKlass) k;
    if (!klass.isSealed()) {
        return StaticObject.NULL;
    }
    char[] classes = ((PermittedSubclassesAttribute) klass.getAttribute(PermittedSubclassesAttribute.NAME)).getClasses();
    StaticObject[] permittedSubclasses = new StaticObject[classes.length];
    RuntimeConstantPool pool = klass.getConstantPool();
    int nClasses = 0;
    for (int index : classes) {
        Klass permitted;
        try {
            permitted = pool.resolvedKlassAt(klass, index);
        } catch (EspressoException e) {
            /* Suppress and continue */
            continue;
        }
        if (permitted instanceof ObjectKlass) {
            permittedSubclasses[nClasses++] = permitted.mirror();
        }
    }
    if (nClasses == permittedSubclasses.length) {
        return StaticObject.createArray(getMeta().java_lang_Class_array, permittedSubclasses);
    }
    return getMeta().java_lang_Class.allocateReferenceArray(nClasses, (i) -> permittedSubclasses[i]);
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 27 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.

the class VM method JVM_CurrentLoadedClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_CurrentLoadedClass() {
    PrivilegedStack stack = getPrivilegedStack();
    StaticObject mirrorKlass = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<StaticObject>() {

        @Override
        public StaticObject visitFrame(FrameInstance frameInstance) {
            Method m = getMethodFromFrame(frameInstance);
            if (m != null) {
                if (isTrustedFrame(frameInstance, stack)) {
                    return StaticObject.NULL;
                }
                if (!m.isNative()) {
                    ObjectKlass klass = m.getDeclaringKlass();
                    StaticObject loader = klass.getDefiningClassLoader();
                    if (StaticObject.notNull(loader) && !isTrustedLoader(loader)) {
                        return klass.mirror();
                    }
                }
            }
            return null;
        }
    });
    return mirrorKlass == null ? StaticObject.NULL : mirrorKlass;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 28 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.

the class Meta method knownKlass.

private ObjectKlass knownKlass(Symbol<Type> type, StaticObject classLoader) {
    CompilerAsserts.neverPartOfCompilation();
    assert !Types.isArray(type);
    assert !Types.isPrimitive(type);
    ObjectKlass k = loadKlassOrNull(type, classLoader);
    if (k == null) {
        throw EspressoError.shouldNotReachHere("Failed loading known class: ", type, ", discovered java version: ", getJavaVersion());
    }
    return k;
}
Also used : ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass)

Example 29 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.

the class Target_sun_reflect_NativeMethodAccessorImpl method callMethodReflectively.

@JavaType(Object.class)
public static StaticObject callMethodReflectively(Meta meta, @JavaType(Object.class) StaticObject receiver, @JavaType(Object[].class) StaticObject args, Method m, Klass klass, @JavaType(Class[].class) StaticObject parameterTypes) {
    // Klass should be initialized if method is static, and could be delayed until method
    // invocation, according to specs. However, JCK tests that it is indeed always initialized
    // before doing anything, even if the method to be invoked is from another class.
    klass.safeInitialize();
    Method reflectedMethod = m;
    if (reflectedMethod.isRemovedByRedefition()) {
        reflectedMethod = m.getContext().getClassRedefinition().handleRemovedMethod(reflectedMethod, reflectedMethod.isStatic() ? reflectedMethod.getDeclaringKlass() : receiver.getKlass());
    }
    // actual method to invoke
    Method method;
    // target klass, receiver's klass for non-static
    Klass targetKlass;
    if (reflectedMethod.isStatic()) {
        // Ignore receiver argument;.
        method = reflectedMethod;
        targetKlass = klass;
    } else {
        if (StaticObject.isNull(receiver)) {
            throw meta.throwNullPointerException();
        }
        // Check class of receiver against class declaring method.
        if (!klass.isAssignableFrom(receiver.getKlass())) {
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "object is not an instance of declaring class");
        }
        // target klass is receiver's klass
        targetKlass = receiver.getKlass();
        // no need to resolve if method is private or <init>
        if (reflectedMethod.isPrivate() || Name._init_.equals(reflectedMethod.getName())) {
            method = reflectedMethod;
        } else {
            // resolve based on the receiver
            if (reflectedMethod.getDeclaringKlass().isInterface()) {
                // resolve interface call
                // Match resolution errors with those thrown due to reflection inlining
                // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
                method = reflectedMethod;
                assert targetKlass instanceof ObjectKlass;
                method = ((ObjectKlass) targetKlass).itableLookup(method.getDeclaringKlass(), method.getITableIndex());
                if (method != null) {
                    // Check for abstract methods as well
                    if (!method.hasCode()) {
                        // new default: 65315
                        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
                    }
                }
            } else {
                // if the method can be overridden, we resolve using the vtable index.
                method = reflectedMethod;
                // VTable is live, use it
                method = targetKlass.vtableLookup(method.getVTableIndex());
                if (method != null) {
                    // Check for abstract methods as well
                    if (method.isAbstract()) {
                        // new default: 65315
                        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
                    }
                }
            }
        }
    }
    // an internal vtable bug. If you ever get this please let Karen know.
    if (method == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, "please let Karen know");
    }
    int argsLen = StaticObject.isNull(args) ? 0 : args.length();
    final Symbol<Type>[] signature = method.getParsedSignature();
    // Check number of arguments.
    if (Signatures.parameterCount(signature, false) != argsLen) {
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "wrong number of arguments!");
    }
    Object[] adjustedArgs = new Object[argsLen];
    for (int i = 0; i < argsLen; ++i) {
        StaticObject arg = args.get(i);
        StaticObject paramTypeMirror = parameterTypes.get(i);
        Klass paramKlass = paramTypeMirror.getMirrorKlass();
        // Throws guest IllegallArgumentException if the parameter cannot be casted or widened.
        adjustedArgs[i] = checkAndWiden(meta, arg, paramKlass);
    }
    Object result;
    try {
        result = method.invokeDirect(receiver, adjustedArgs);
    } catch (EspressoException e) {
        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, e.getGuestException());
    }
    if (reflectedMethod.getReturnKind() == JavaKind.Void) {
        return StaticObject.NULL;
    }
    if (reflectedMethod.getReturnKind().isPrimitive()) {
        return Meta.box(meta, result);
    }
    // Result is not void nor primitive, pass through.
    return (StaticObject) result;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method)

Example 30 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.

the class Target_sun_misc_Unsafe method defineAnonymousClass.

@TruffleBoundary
@Substitution(hasReceiver = true, nameProvider = SharedUnsafeAppend0.class)
@SuppressWarnings("unused")
@JavaType(Class.class)
public static StaticObject defineAnonymousClass(@JavaType(Unsafe.class) StaticObject self, @JavaType(Class.class) StaticObject hostClass, @JavaType(byte[].class) StaticObject data, @JavaType(Object[].class) StaticObject constantPoolPatches, @Inject Meta meta) {
    if (StaticObject.isNull(hostClass) || StaticObject.isNull(data)) {
        throw meta.throwNullPointerException();
    }
    if (hostClass.getMirrorKlass().isArray() || hostClass.getMirrorKlass().isPrimitive()) {
        throw meta.throwException(meta.java_lang_IllegalArgumentException);
    }
    byte[] bytes = data.unwrap();
    ObjectKlass hostKlass = (ObjectKlass) hostClass.getMirrorKlass();
    StaticObject pd = (StaticObject) meta.HIDDEN_PROTECTION_DOMAIN.getHiddenObject(hostClass);
    StaticObject[] patches = StaticObject.isNull(constantPoolPatches) ? null : constantPoolPatches.unwrap();
    // Inherit host class's protection domain.
    ClassRegistry.ClassDefinitionInfo info = new ClassRegistry.ClassDefinitionInfo(pd, hostKlass, patches);
    ObjectKlass k = meta.getRegistries().defineKlass(null, bytes, hostKlass.getDefiningClassLoader(), info);
    // Initialize, because no one else will.
    k.safeInitialize();
    return k.mirror();
}
Also used : ClassRegistry(com.oracle.truffle.espresso.impl.ClassRegistry) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)35 Klass (com.oracle.truffle.espresso.impl.Klass)15 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)12 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)10 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)9 ArrayList (java.util.ArrayList)8 Method (com.oracle.truffle.espresso.impl.Method)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)5 Field (com.oracle.truffle.espresso.impl.Field)5 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)5 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)5 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)4 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)4 ClassRegistry (com.oracle.truffle.espresso.impl.ClassRegistry)4 Meta (com.oracle.truffle.espresso.meta.Meta)4 Matcher (java.util.regex.Matcher)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)3 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)3 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2