Search in sources :

Example 46 with Method

use of com.oracle.truffle.espresso.impl.Method 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 47 with Method

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

the class VM method JVM_InitStackTraceElement.

@VmImpl(isJni = true)
@SuppressWarnings("unused")
public void JVM_InitStackTraceElement(@JavaType(StackTraceElement.class) StaticObject element, @JavaType(internalName = "Ljava/lang/StackFrameInfo;") StaticObject info, @Inject Meta meta) {
    if (StaticObject.isNull(element) || StaticObject.isNull(info)) {
        throw meta.throwNullPointerException();
    }
    StaticObject mname = meta.java_lang_StackFrameInfo_memberName.getObject(info);
    if (StaticObject.isNull(mname)) {
        throw meta.throwExceptionWithMessage(meta.java_lang_InternalError, "uninitialized StackFrameInfo !");
    }
    StaticObject clazz = meta.java_lang_invoke_MemberName_clazz.getObject(mname);
    Method m = (Method) meta.HIDDEN_VMTARGET.getHiddenObject(mname);
    if (m == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_InternalError, "uninitialized StackFrameInfo !");
    }
    int bci = meta.java_lang_StackFrameInfo_bci.getInt(info);
    fillInElement(element, new VM.StackElement(m, bci), getMeta().java_lang_Class_getName);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint)

Example 48 with Method

use of com.oracle.truffle.espresso.impl.Method 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 49 with Method

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

the class VM method JVM_GetStackTraceElement.

@VmImpl(isJni = true)
@JavaType(StackTraceElement.class)
public StaticObject JVM_GetStackTraceElement(@JavaType(Throwable.class) StaticObject self, int index, @Inject SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    if (index < 0) {
        profiler.profile(0);
        throw meta.throwException(meta.java_lang_IndexOutOfBoundsException);
    }
    StaticObject ste = meta.java_lang_StackTraceElement.allocateInstance();
    StackTrace frames = EspressoException.getFrames(self, meta);
    if (frames == null || index >= frames.size) {
        profiler.profile(1);
        throw meta.throwException(meta.java_lang_IndexOutOfBoundsException);
    }
    StackElement stackElement = frames.trace[index];
    Method method = stackElement.getMethod();
    if (method == null) {
        return StaticObject.NULL;
    }
    int bci = stackElement.getBCI();
    getMeta().java_lang_StackTraceElement_init.invokeDirect(/* this */
    ste, /* declaringClass */
    meta.toGuestString(MetaUtil.internalNameToJava(method.getDeclaringKlass().getType().toString(), true, true)), /* methodName */
    meta.toGuestString(method.getName()), /* fileName */
    meta.toGuestString(method.getSourceFile()), /* lineNumber */
    method.bciToLineNumber(bci));
    return ste;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 50 with Method

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

the class VM method getMethodFromFrame.

@TruffleBoundary
public Method getMethodFromFrame(FrameInstance frameInstance) {
    EspressoRootNode root = getRawEspressoRootFromFrame(frameInstance);
    if (root == null) {
        return null;
    }
    Method method = root.getMethod();
    if (method.getContext() != getContext()) {
        return null;
    }
    return method;
}
Also used : EspressoRootNode(com.oracle.truffle.espresso.nodes.EspressoRootNode) Method(com.oracle.truffle.espresso.impl.Method) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

Method (com.oracle.truffle.espresso.impl.Method)91 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)57 Meta (com.oracle.truffle.espresso.meta.Meta)27 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)22 Klass (com.oracle.truffle.espresso.impl.Klass)19 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)19 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ExportMessage (com.oracle.truffle.api.library.ExportMessage)12 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)8 ArityException (com.oracle.truffle.api.interop.ArityException)8 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 ArrayList (java.util.ArrayList)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)7 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)5 Field (com.oracle.truffle.espresso.impl.Field)4 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3 Signature (com.oracle.truffle.espresso.descriptors.Symbol.Signature)3