Search in sources :

Example 31 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType 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 32 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method JVM_GetMethodParameters.

@VmImpl(isJni = true)
@JavaType(Parameter[].class)
public StaticObject JVM_GetMethodParameters(@JavaType(Object.class) StaticObject executable, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    assert meta.java_lang_reflect_Executable.isAssignableFrom(executable.getKlass());
    StaticObject parameterTypes = (StaticObject) executable.getKlass().lookupMethod(Name.getParameterTypes, Signature.Class_array).invokeDirect(executable);
    int numParams = parameterTypes.length();
    if (numParams == 0) {
        return StaticObject.NULL;
    }
    Method method;
    if (meta.java_lang_reflect_Method.isAssignableFrom(executable.getKlass())) {
        method = Method.getHostReflectiveMethodRoot(executable, meta);
    } else if (meta.java_lang_reflect_Constructor.isAssignableFrom(executable.getKlass())) {
        method = Method.getHostReflectiveConstructorRoot(executable, meta);
    } else {
        throw EspressoError.shouldNotReachHere();
    }
    MethodParametersAttribute methodParameters = (MethodParametersAttribute) method.getAttribute(Name.MethodParameters);
    if (methodParameters == null) {
        return StaticObject.NULL;
    }
    // Verify first.
    /*
         * If number of entries in ParametersAttribute is inconsistent with actual parameters from
         * the signature, it will be caught in guest java code.
         */
    int cpLength = method.getConstantPool().length();
    for (MethodParametersAttribute.Entry entry : methodParameters.getEntries()) {
        int nameIndex = entry.getNameIndex();
        if (nameIndex < 0 || nameIndex >= cpLength) {
            profiler.profile(0);
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Constant pool index out of bounds");
        }
        if (nameIndex != 0 && method.getConstantPool().tagAt(nameIndex) != ConstantPool.Tag.UTF8) {
            profiler.profile(1);
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Wrong type at constant pool index");
        }
    }
    // TODO(peterssen): Cache guest j.l.reflect.Parameter constructor.
    // Calling the constructor is just for validation, manually setting the fields would
    // be faster.
    Method parameterInit = meta.java_lang_reflect_Parameter.lookupDeclaredMethod(Name._init_, getSignatures().makeRaw(Type._void, /* name */
    Type.java_lang_String, /* modifiers */
    Type._int, /* executable */
    Type.java_lang_reflect_Executable, /* index */
    Type._int));
    // Use attribute's number of parameters.
    return meta.java_lang_reflect_Parameter.allocateReferenceArray(methodParameters.getEntries().length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int index) {
            MethodParametersAttribute.Entry entry = methodParameters.getEntries()[index];
            StaticObject instance = meta.java_lang_reflect_Parameter.allocateInstance();
            // For a 0 index, give an empty name.
            StaticObject guestName;
            if (entry.getNameIndex() != 0) {
                guestName = meta.toGuestString(method.getConstantPool().symbolAt(entry.getNameIndex(), "parameter name").toString());
            } else {
                guestName = getJavaVersion().java9OrLater() ? StaticObject.NULL : meta.toGuestString("");
            }
            parameterInit.invokeDirect(/* this */
            instance, /* name */
            guestName, /* modifiers */
            entry.getAccessFlags(), /* executable */
            executable, /* index */
            index);
            return instance;
        }
    });
}
Also used : ModuleEntry(com.oracle.truffle.espresso.impl.ModuleTable.ModuleEntry) PackageEntry(com.oracle.truffle.espresso.impl.PackageTable.PackageEntry) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 33 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method JVM_DefineClass.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class.class)
public StaticObject JVM_DefineClass(@Pointer TruffleObject namePtr, @JavaType(ClassLoader.class) StaticObject loader, @Pointer TruffleObject bufPtr, int len, @JavaType(ProtectionDomain.class) StaticObject pd) {
    ByteBuffer buf = NativeUtils.directByteBuffer(bufPtr, len, JavaKind.Byte);
    final byte[] bytes = new byte[len];
    buf.get(bytes);
    // can be null
    Symbol<Type> type = namePtrToInternal(namePtr);
    StaticObject clazz = getContext().getRegistries().defineKlass(type, bytes, loader, new ClassRegistry.ClassDefinitionInfo(pd)).mirror();
    assert clazz != null;
    return clazz;
}
Also used : NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ByteBuffer(java.nio.ByteBuffer) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 34 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType 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 35 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType 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

JavaType (com.oracle.truffle.espresso.substitutions.JavaType)58 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)44 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)28 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)24 Klass (com.oracle.truffle.espresso.impl.Klass)24 Meta (com.oracle.truffle.espresso.meta.Meta)20 Method (com.oracle.truffle.espresso.impl.Method)19 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)15 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)13 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)8 NativeType (com.oracle.truffle.espresso.ffi.NativeType)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)5 Field (com.oracle.truffle.espresso.impl.Field)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)4 ArrayList (java.util.ArrayList)4 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3