Search in sources :

Example 36 with Klass

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

the class JDKProxyRedefinitionPlugin method collectProxyArguments.

public synchronized void collectProxyArguments(@JavaType(String.class) StaticObject proxyName, @JavaType(Class[].class) StaticObject interfaces, int classModifier, DirectCallNode generatorMethodCallNode) {
    if (proxyGeneratorMethodCallNode == null) {
        proxyGeneratorMethodCallNode = generatorMethodCallNode;
    }
    // register onLoad action that will give us
    // the klass object for the generated proxy
    registerClassLoadAction(getContext().getMeta().toHostString(proxyName), klass -> {
        // store guest-world arguments that we can use when
        // invoking the call node later on re-generation
        ProxyCache proxyCache = new ProxyCache(klass, proxyName, interfaces, classModifier);
        Klass[] proxyInterfaces = new Klass[interfaces.length()];
        for (int i = 0; i < proxyInterfaces.length; i++) {
            proxyInterfaces[i] = (Klass) getContext().getMeta().HIDDEN_MIRROR_KLASS.getHiddenObject(interfaces.get(i));
        }
        // when they change we can re-generate the proxy bytes
        for (KlassRef proxyInterface : proxyInterfaces) {
            addCacheEntry(proxyCache, proxyInterface);
        }
    });
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) KlassRef(com.oracle.truffle.espresso.jdwp.api.KlassRef)

Example 37 with Klass

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

the class VM method JVM_FindLoadedClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_FindLoadedClass(@JavaType(ClassLoader.class) StaticObject loader, @JavaType(String.class) StaticObject name) {
    Symbol<Type> type = getTypes().fromClassGetName(getMeta().toHostString(name));
    // HotSpot skips reflection (DelegatingClassLoader) class loaders.
    Klass klass = getRegistries().findLoadedClass(type, nonReflectionClassLoader(loader));
    if (klass == null) {
        return StaticObject.NULL;
    }
    return klass.mirror();
}
Also used : NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 38 with Klass

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

the class VM method JVM_GetClassDeclaredFields.

@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Field[].class)
public StaticObject JVM_GetClassDeclaredFields(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    // TODO(peterssen): From Hostpot: 4496456 We need to filter out
    // java.lang.Throwable.backtrace.
    Meta meta = getMeta();
    ArrayList<Field> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Field f : klass.getDeclaredFields()) {
        if (!publicOnly || f.isPublic()) {
            collectedMethods.add(f);
        }
    }
    final Field[] fields = collectedMethods.toArray(Field.EMPTY_ARRAY);
    EspressoContext context = meta.getContext();
    // TODO(peterssen): Cache guest j.l.reflect.Field constructor.
    // Calling the constructor is just for validation, manually setting the fields would be
    // faster.
    Method fieldInit;
    if (meta.getJavaVersion().java15OrLater()) {
        fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
        Type.java_lang_Class, /* name */
        Type.java_lang_String, /* type */
        Type.java_lang_Class, /* modifiers */
        Type._int, /* trustedFinal */
        Type._boolean, /* slot */
        Type._int, /* signature */
        Type.java_lang_String, /* annotations */
        Type._byte_array));
    } else {
        fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
        Type.java_lang_Class, /* name */
        Type.java_lang_String, /* type */
        Type.java_lang_Class, /* modifiers */
        Type._int, /* slot */
        Type._int, /* signature */
        Type.java_lang_String, /* annotations */
        Type._byte_array));
    }
    StaticObject fieldsArray = meta.java_lang_reflect_Field.allocateReferenceArray(fields.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            final Field f = fields[i];
            StaticObject instance = meta.java_lang_reflect_Field.allocateInstance();
            Attribute rawRuntimeVisibleAnnotations = f.getAttribute(Name.RuntimeVisibleAnnotations);
            StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleTypeAnnotations = f.getAttribute(Name.RuntimeVisibleTypeAnnotations);
            StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
            if (meta.getJavaVersion().java15OrLater()) {
                fieldInit.invokeDirect(/* this */
                instance, /* declaringKlass */
                f.getDeclaringKlass().mirror(), /* name */
                context.getStrings().intern(f.getName()), /* type */
                f.resolveTypeKlass().mirror(), /* modifiers */
                f.getModifiers(), /* trustedFinal */
                f.isTrustedFinal(), /* slot */
                f.getSlot(), /* signature */
                meta.toGuestString(f.getGenericSignature()), /* annotations */
                runtimeVisibleAnnotations);
            } else {
                fieldInit.invokeDirect(/* this */
                instance, /* declaringKlass */
                f.getDeclaringKlass().mirror(), /* name */
                context.getStrings().intern(f.getName()), /* type */
                f.resolveTypeKlass().mirror(), /* modifiers */
                f.getModifiers(), /* slot */
                f.getSlot(), /* signature */
                meta.toGuestString(f.getGenericSignature()), /* annotations */
                runtimeVisibleAnnotations);
            }
            meta.HIDDEN_FIELD_KEY.setHiddenObject(instance, f);
            meta.HIDDEN_FIELD_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
            return instance;
        }
    });
    return fieldsArray;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) Attribute(com.oracle.truffle.espresso.runtime.Attribute) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) ArrayList(java.util.ArrayList) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Field(com.oracle.truffle.espresso.impl.Field) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 39 with Klass

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

the class VM method cloneForeignArray.

private static StaticObject cloneForeignArray(StaticObject array, Meta meta, InteropLibrary interop, ToEspressoNode toEspressoNode, SubstitutionProfiler profiler, char exceptionBranch) {
    assert array.isForeignObject();
    assert array.isArray();
    int length;
    try {
        long longLength = interop.getArraySize(array.rawForeignObject());
        if (longLength > Integer.MAX_VALUE) {
            profiler.profile(exceptionBranch);
            throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Cannot clone a foreign array whose length does not fit in int");
        }
        if (longLength < 0) {
            profiler.profile(exceptionBranch);
            throw meta.throwExceptionWithMessage(meta.java_lang_NegativeArraySizeException, "Cannot clone a foreign array with negative length");
        }
        length = (int) longLength;
    } catch (UnsupportedMessageException e) {
        profiler.profile(exceptionBranch);
        throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Cannot clone a non-array foreign object as an array");
    }
    ArrayKlass arrayKlass = (ArrayKlass) array.getKlass();
    Klass componentType = arrayKlass.getComponentType();
    if (componentType.isPrimitive()) {
        try {
            switch(componentType.getJavaKind()) {
                case Boolean:
                    boolean[] booleanArray = new boolean[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        booleanArray[i] = (boolean) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, booleanArray);
                case Byte:
                    byte[] byteArray = new byte[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        byteArray[i] = (byte) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, byteArray);
                case Short:
                    short[] shortArray = new short[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        shortArray[i] = (short) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, shortArray);
                case Char:
                    char[] charArray = new char[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        charArray[i] = (char) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, charArray);
                case Int:
                    int[] intArray = new int[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        intArray[i] = (int) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, intArray);
                case Float:
                    float[] floatArray = new float[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        floatArray[i] = (float) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, floatArray);
                case Long:
                    long[] longArray = new long[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        longArray[i] = (long) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, longArray);
                case Double:
                    double[] doubleArray = new double[length];
                    for (int i = 0; i < length; ++i) {
                        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
                        doubleArray[i] = (double) toEspressoNode.execute(foreignElement, componentType);
                    }
                    return StaticObject.createArray(arrayKlass, doubleArray);
                case Object:
                case Void:
                case ReturnAddress:
                case Illegal:
                    CompilerDirectives.transferToInterpreter();
                    throw EspressoError.shouldNotReachHere("Unexpected primitive kind: " + componentType.getJavaKind());
            }
        } catch (UnsupportedTypeException e) {
            profiler.profile(exceptionBranch);
            throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "Cannot cast an element of a foreign array to the declared component type");
        }
    }
    StaticObject[] newArray = new StaticObject[length];
    for (int i = 0; i < length; ++i) {
        Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
        try {
            newArray[i] = (StaticObject) toEspressoNode.execute(foreignElement, componentType);
        } catch (UnsupportedTypeException e) {
            profiler.profile(exceptionBranch);
            throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "Cannot cast an element of a foreign array to the declared component type");
        }
    }
    return StaticObject.createArray(arrayKlass, newArray);
}
Also used : ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 40 with Klass

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

the class VM method JVM_GetEnclosingMethodInfo.

@VmImpl(isJni = true)
@JavaType(Object[].class)
public StaticObject JVM_GetEnclosingMethodInfo(@JavaType(Class.class) StaticObject self) {
    Meta meta = getMeta();
    InterpreterToVM vm = meta.getInterpreterToVM();
    if (self.getMirrorKlass() instanceof ObjectKlass) {
        ObjectKlass klass = (ObjectKlass) self.getMirrorKlass();
        EnclosingMethodAttribute enclosingMethodAttr = klass.getEnclosingMethod();
        if (enclosingMethodAttr == null) {
            return StaticObject.NULL;
        }
        int classIndex = enclosingMethodAttr.getClassIndex();
        if (classIndex == 0) {
            return StaticObject.NULL;
        }
        StaticObject arr = meta.java_lang_Object.allocateReferenceArray(3);
        RuntimeConstantPool pool = klass.getConstantPool();
        Klass enclosingKlass = pool.resolvedKlassAt(klass, classIndex);
        vm.setArrayObject(enclosingKlass.mirror(), 0, arr);
        int methodIndex = enclosingMethodAttr.getMethodIndex();
        if (methodIndex != 0) {
            NameAndTypeConstant nmt = pool.nameAndTypeAt(methodIndex);
            StaticObject name = meta.toGuestString(nmt.getName(pool));
            StaticObject desc = meta.toGuestString(nmt.getDescriptor(pool));
            vm.setArrayObject(name, 1, arr);
            vm.setArrayObject(desc, 2, arr);
        }
        return arr;
    }
    return StaticObject.NULL;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NameAndTypeConstant(com.oracle.truffle.espresso.classfile.constantpool.NameAndTypeConstant) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Aggregations

Klass (com.oracle.truffle.espresso.impl.Klass)71 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)54 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)49 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)33 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)24 Meta (com.oracle.truffle.espresso.meta.Meta)21 Method (com.oracle.truffle.espresso.impl.Method)19 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)10 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)10 ExportMessage (com.oracle.truffle.api.library.ExportMessage)8 Field (com.oracle.truffle.espresso.impl.Field)7 ArrayList (java.util.ArrayList)7 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)6 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)6 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)5 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5 NativeType (com.oracle.truffle.espresso.ffi.NativeType)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3