Search in sources :

Example 1 with Name

use of com.oracle.truffle.espresso.descriptors.Symbol.Name in project graal by oracle.

the class JniEnv method GetMethodID.

/**
 * <h3>jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);</h3>
 * <p>
 * Returns the method ID for an instance (nonstatic) method of a class or interface. The method
 * may be defined in one of the clazz’s superclasses and inherited by clazz. The method is
 * determined by its name and signature.
 * <p>
 * GetMethodID() causes an uninitialized class to be initialized.
 * <p>
 * To obtain the method ID of a constructor, supply <init> as the method name and void (V) as
 * the return type.
 *
 * @param clazz a Java class object.
 * @param namePtr the method name in a 0-terminated modified UTF-8 string.
 * @param signaturePtr the method signature in 0-terminated modified UTF-8 string.
 * @return a method ID, or NULL if the specified method cannot be found.
 * @throws NoSuchMethodError if the specified method cannot be found.
 * @throws ExceptionInInitializerError if the class initializer fails due to an exception.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@JniImpl
@Handle(Method.class)
public long GetMethodID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject signaturePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String signature = NativeUtils.interopPointerToString(signaturePtr);
    assert name != null && signature != null;
    Method method = null;
    Symbol<Name> methodName = getNames().lookup(name);
    if (methodName != null) {
        Symbol<Signature> methodSignature = getSignatures().lookupValidSignature(signature);
        if (methodSignature != null) {
            Klass klass = clazz.getMirrorKlass();
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            method = klass.lookupMethod(methodName, methodSignature, klass);
        }
    }
    if (method == null || method.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
    }
    return methodIds.handlify(method);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 2 with Name

use of com.oracle.truffle.espresso.descriptors.Symbol.Name in project graal by oracle.

the class JniEnv method RegisterNative.

// endregion DirectBuffers
// region Register/Unregister natives
@JniImpl
@TruffleBoundary
public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject methodNamePtr, @Pointer TruffleObject methodSignaturePtr, @Pointer TruffleObject closure) {
    String methodName = NativeUtils.interopPointerToString(methodNamePtr);
    String methodSignature = NativeUtils.interopPointerToString(methodSignaturePtr);
    assert methodName != null && methodSignature != null;
    Symbol<Name> name = getNames().lookup(methodName);
    Symbol<Signature> signature = getSignatures().lookupValidSignature(methodSignature);
    Meta meta = getMeta();
    if (name == null || signature == null) {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Method targetMethod = clazz.getMirrorKlass().lookupDeclaredMethod(name, signature);
    if (targetMethod != null && targetMethod.isNative()) {
        targetMethod.unregisterNative();
        getSubstitutions().removeRuntimeSubstitution(targetMethod);
    } else {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Substitutions.EspressoRootNodeFactory factory = null;
    // Lookup known VM methods to shortcut native boudaries.
    factory = lookupKnownVmMethods(closure, targetMethod);
    if (factory == null) {
        NativeSignature ns = Method.buildJniNativeSignature(targetMethod.getParsedSignature());
        final TruffleObject boundNative = getNativeAccess().bindSymbol(closure, ns);
        factory = createJniRootNodeFactory(() -> new NativeMethodNode(boundNative, targetMethod.getMethodVersion()), targetMethod);
    }
    Symbol<Type> classType = clazz.getMirrorKlass().getType();
    getSubstitutions().registerRuntimeSubstitution(classType, name, signature, factory, true);
    return JNI_OK;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Substitutions(com.oracle.truffle.espresso.substitutions.Substitutions) NativeMethodNode(com.oracle.truffle.espresso.nodes.NativeMethodNode) IntrinsifiedNativeMethodNode(com.oracle.truffle.espresso.nodes.IntrinsifiedNativeMethodNode) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 3 with Name

use of com.oracle.truffle.espresso.descriptors.Symbol.Name in project graal by oracle.

the class JniEnv method GetFieldID.

// Checkstyle: stop method name check
// region Get*ID
/**
 * <h3>jfieldID GetFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);</h3>
 * <p>
 * Returns the field ID for an instance (nonstatic) field of a class. The field is specified by
 * its name and signature. The Get<type>Field and Set<type>Field families of accessor functions
 * use field IDs to retrieve object fields. GetFieldID() causes an uninitialized class to be
 * initialized. GetFieldID() cannot be used to obtain the length field of an array. Use
 * GetArrayLength() instead.
 *
 * @param clazz a Java class object.
 * @param namePtr the field name in a 0-terminated modified UTF-8 string.
 * @param typePtr the field signature in a 0-terminated modified UTF-8 string.
 * @return a field ID, or NULL if the operation fails.
 * @throws NoSuchFieldError: if the specified field cannot be found.
 * @throws ExceptionInInitializerError: if the class initializer fails due to an exception.
 * @throws OutOfMemoryError: if the system runs out of memory.
 */
@JniImpl
@Handle(Field.class)
public long GetFieldID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject typePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String type = NativeUtils.interopPointerToString(typePtr);
    assert name != null && type != null;
    Klass klass = clazz.getMirrorKlass();
    Field field = null;
    Symbol<Name> fieldName = getNames().lookup(name);
    if (fieldName != null) {
        Symbol<Type> fieldType = getTypes().lookup(type);
        if (fieldType != null) {
            // Lookup only if name and type are known symbols.
            klass.safeInitialize();
            field = klass.lookupField(fieldName, fieldType);
            assert field == null || field.getType().equals(fieldType);
        }
    }
    if (field == null || field.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchFieldError, name);
    }
    assert !field.isStatic();
    return fieldIds.handlify(field);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) Meta(com.oracle.truffle.espresso.meta.Meta) 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) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 4 with Name

use of com.oracle.truffle.espresso.descriptors.Symbol.Name in project graal by oracle.

the class ClassfileParser method parseRecordComponentAttributes.

private Attribute[] parseRecordComponentAttributes() {
    final int size = stream.readU2();
    Attribute[] componentAttributes = new Attribute[size];
    CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Record);
    for (int j = 0; j < size; j++) {
        final Symbol<Name> attributeName = pool.symbolAt(stream.readU2(), "attribute name");
        final int attributeSize = stream.readS4();
        Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
        componentAttributes[j] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
    }
    return componentAttributes;
}
Also used : BootstrapMethodsAttribute(com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) StackMapTableAttribute(com.oracle.truffle.espresso.classfile.attributes.StackMapTableAttribute) NestHostAttribute(com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute) SourceFileAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute) SourceDebugExtensionAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceDebugExtensionAttribute) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) LineNumberTableAttribute(com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute) ExceptionsAttribute(com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) NestMembersAttribute(com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute) ConstantValueAttribute(com.oracle.truffle.espresso.classfile.attributes.ConstantValueAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) 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) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 5 with Name

use of com.oracle.truffle.espresso.descriptors.Symbol.Name in project graal by oracle.

the class ClassfileParser method parseClassAttributes.

private Attribute[] parseClassAttributes() {
    int attributeCount = stream.readU2();
    if (attributeCount == 0) {
        if (maxBootstrapMethodAttrIndex >= 0) {
            throw ConstantPool.classFormatError("BootstrapMethods attribute is missing");
        }
        return Attribute.EMPTY_ARRAY;
    }
    SourceFileAttribute sourceFileName = null;
    SourceDebugExtensionAttribute sourceDebugExtensionAttribute = null;
    NestHostAttribute nestHost = null;
    NestMembersAttribute nestMembers = null;
    EnclosingMethodAttribute enclosingMethod = null;
    BootstrapMethodsAttribute bootstrapMethods = null;
    InnerClassesAttribute innerClasses = null;
    PermittedSubclassesAttribute permittedSubclasses = null;
    RecordAttribute record = null;
    CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Class);
    final Attribute[] classAttributes = spawnAttributesArray(attributeCount);
    for (int i = 0; i < attributeCount; i++) {
        final int attributeNameIndex = stream.readU2();
        final Symbol<Name> attributeName = pool.symbolAt(attributeNameIndex, "attribute name");
        final int attributeSize = stream.readS4();
        final int startPosition = stream.getPosition();
        if (attributeName.equals(Name.SourceFile)) {
            if (sourceFileName != null) {
                throw ConstantPool.classFormatError("Duplicate SourceFile attribute");
            }
            classAttributes[i] = sourceFileName = parseSourceFileAttribute(attributeName);
        } else if (attributeName.equals(Name.SourceDebugExtension)) {
            if (sourceDebugExtensionAttribute != null) {
                throw ConstantPool.classFormatError("Duplicate SourceDebugExtension attribute");
            }
            classAttributes[i] = sourceDebugExtensionAttribute = parseSourceDebugExtensionAttribute(attributeName, attributeSize);
        } else if (attributeName.equals(Name.Synthetic)) {
            classFlags |= ACC_SYNTHETIC;
            classAttributes[i] = new Attribute(attributeName, null);
        } else if (attributeName.equals(Name.InnerClasses)) {
            if (innerClasses != null) {
                throw ConstantPool.classFormatError("Duplicate InnerClasses attribute");
            }
            classAttributes[i] = innerClasses = parseInnerClasses(attributeName);
        } else if (majorVersion >= JAVA_1_5_VERSION) {
            if (majorVersion >= JAVA_7_VERSION && attributeName.equals(Name.BootstrapMethods)) {
                if (bootstrapMethods != null) {
                    throw ConstantPool.classFormatError("Duplicate BootstrapMethods attribute");
                }
                classAttributes[i] = bootstrapMethods = parseBootstrapMethods(attributeName);
            } else if (attributeName.equals(Name.EnclosingMethod)) {
                if (enclosingMethod != null) {
                    throw ConstantPool.classFormatError("Duplicate EnclosingMethod attribute");
                }
                classAttributes[i] = enclosingMethod = parseEnclosingMethodAttribute(attributeName);
            } else if (majorVersion >= JAVA_11_VERSION && attributeName.equals(Name.NestHost)) {
                if (nestHost != null) {
                    throw ConstantPool.classFormatError("Duplicate NestHost attribute");
                }
                if (nestMembers != null) {
                    throw ConstantPool.classFormatError("Classfile cannot have both a nest members and a nest host attribute.");
                }
                if (attributeSize != 2) {
                    throw ConstantPool.classFormatError("Attribute length of NestHost must be 2");
                }
                classAttributes[i] = nestHost = parseNestHostAttribute(attributeName);
            } else if (majorVersion >= JAVA_11_VERSION && attributeName.equals(Name.NestMembers)) {
                if (nestMembers != null) {
                    throw ConstantPool.classFormatError("Duplicate NestMembers attribute");
                }
                if (nestHost != null) {
                    throw ConstantPool.classFormatError("Classfile cannot have both a nest members and a nest host attribute.");
                }
                classAttributes[i] = nestMembers = parseNestMembers(attributeName);
            } else if (majorVersion >= JAVA_14_VERSION && attributeName.equals(Name.Record)) {
                if (record != null) {
                    throw ConstantPool.classFormatError("Duplicate Record attribute");
                }
                classAttributes[i] = record = parseRecord(attributeName);
            } else if (majorVersion >= JAVA_17_VERSION && attributeName.equals(Name.PermittedSubclasses)) {
                if (permittedSubclasses != null) {
                    throw ConstantPool.classFormatError("Duplicate PermittedSubclasses attribute");
                }
                classAttributes[i] = permittedSubclasses = parsePermittedSubclasses(attributeName);
            } else {
                Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
                // stream.skip(attributeSize);
                classAttributes[i] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
            }
        } else {
            // stream.skip(attributeSize);
            classAttributes[i] = new Attribute(attributeName, stream.readByteArray(attributeSize));
        }
        if (attributeSize != stream.getPosition() - startPosition) {
            throw ConstantPool.classFormatError("Invalid attribute length for " + attributeName + " attribute");
        }
    }
    if (maxBootstrapMethodAttrIndex >= 0 && bootstrapMethods == null) {
        throw ConstantPool.classFormatError("BootstrapMethods attribute is missing");
    }
    return classAttributes;
}
Also used : NestHostAttribute(com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute) BootstrapMethodsAttribute(com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) BootstrapMethodsAttribute(com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) StackMapTableAttribute(com.oracle.truffle.espresso.classfile.attributes.StackMapTableAttribute) NestHostAttribute(com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute) SourceFileAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute) SourceDebugExtensionAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceDebugExtensionAttribute) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) LineNumberTableAttribute(com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute) ExceptionsAttribute(com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) NestMembersAttribute(com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute) ConstantValueAttribute(com.oracle.truffle.espresso.classfile.attributes.ConstantValueAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) 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) SourceDebugExtensionAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceDebugExtensionAttribute) NestMembersAttribute(com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) SourceFileAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)

Aggregations

Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)27 Klass (com.oracle.truffle.espresso.impl.Klass)10 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)9 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)9 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)9 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)8 Meta (com.oracle.truffle.espresso.meta.Meta)8 Method (com.oracle.truffle.espresso.impl.Method)7 BootstrapMethodsAttribute (com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute)6 CodeAttribute (com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)6 ConstantValueAttribute (com.oracle.truffle.espresso.classfile.attributes.ConstantValueAttribute)6 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)6 ExceptionsAttribute (com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute)6 LineNumberTableAttribute (com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute)6 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)6 NestHostAttribute (com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute)6 NestMembersAttribute (com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute)6 PermittedSubclassesAttribute (com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute)6 RecordAttribute (com.oracle.truffle.espresso.classfile.attributes.RecordAttribute)6 SignatureAttribute (com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute)6