Search in sources :

Example 16 with Name

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

the class VM method JVM_GetDeclaredClasses.

@VmImpl(isJni = true)
@JavaType(Class[].class)
public StaticObject JVM_GetDeclaredClasses(@JavaType(Class.class) StaticObject self) {
    Meta meta = getMeta();
    Klass klass = self.getMirrorKlass();
    if (klass.isPrimitive() || klass.isArray()) {
        return meta.java_lang_Class.allocateReferenceArray(0);
    }
    ObjectKlass instanceKlass = (ObjectKlass) klass;
    InnerClassesAttribute innerClasses = (InnerClassesAttribute) instanceKlass.getAttribute(InnerClassesAttribute.NAME);
    if (innerClasses == null || innerClasses.entries().length == 0) {
        return meta.java_lang_Class.allocateReferenceArray(0);
    }
    RuntimeConstantPool pool = instanceKlass.getConstantPool();
    List<Klass> innerKlasses = new ArrayList<>();
    for (InnerClassesAttribute.Entry entry : innerClasses.entries()) {
        if (entry.innerClassIndex != 0 && entry.outerClassIndex != 0) {
            // Check to see if the name matches the class we're looking for
            // before attempting to find the class.
            Symbol<Name> outerDescriptor = pool.classAt(entry.outerClassIndex).getName(pool);
            // Check decriptors/names before resolving.
            if (outerDescriptor.equals(instanceKlass.getName())) {
                Klass outerKlass = pool.resolvedKlassAt(instanceKlass, entry.outerClassIndex);
                if (outerKlass == instanceKlass) {
                    Klass innerKlass = pool.resolvedKlassAt(instanceKlass, entry.innerClassIndex);
                    // HotSpot:
                    // Throws an exception if outer klass has not declared k as
                    // an inner klass
                    // Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
                    // TODO(peterssen): The check in HotSpot is redundant.
                    innerKlasses.add(innerKlass);
                }
            }
        }
    }
    return meta.java_lang_Class.allocateReferenceArray(innerKlasses.size(), new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int index) {
            return innerKlasses.get(index).mirror();
        }
    });
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ArrayList(java.util.ArrayList) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) 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) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 17 with Name

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

the class VM method JVM_ClassDepth.

@VmImpl(isJni = true)
@TruffleBoundary
public int JVM_ClassDepth(@JavaType(String.class) StaticObject name) {
    Symbol<Name> className = getContext().getNames().lookup(getMeta().toHostString(name).replace('.', '/'));
    if (className == null) {
        return -1;
    }
    Integer res = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {

        int depth = 0;

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            Method m = getMethodFromFrame(frameInstance);
            if (m != null) {
                if (className.equals(m.getDeclaringKlass().getName())) {
                    return depth;
                }
                depth++;
            }
            return null;
        }
    });
    return res == null ? -1 : res;
}
Also used : Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 18 with Name

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

the class VM method defineModule.

@SuppressWarnings("try")
public void defineModule(StaticObject module, String moduleName, boolean is_open, String[] packages, SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    StaticObject loader = meta.java_lang_Module_loader.getObject(module);
    if (loader != nonReflectionClassLoader(loader)) {
        profiler.profile(15);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Class loader is an invalid delegating class loader");
    }
    // Prepare variables
    ClassRegistry registry = getRegistries().getClassRegistry(loader);
    assert registry != null;
    PackageTable packageTable = registry.packages();
    ModuleTable moduleTable = registry.modules();
    assert moduleTable != null && packageTable != null;
    boolean loaderIsBootOrPlatform = ClassRegistry.loaderIsBootOrPlatform(loader, meta);
    ArrayList<Symbol<Name>> pkgSymbols = new ArrayList<>();
    try (EntryTable.BlockLock block = packageTable.write()) {
        for (String str : packages) {
            // Extract the package symbols. Also checks for duplicates.
            if (!loaderIsBootOrPlatform && (str.equals("java") || str.startsWith("java/"))) {
                // Only modules defined to either the boot or platform class loader, can define
                // a "java/" package.
                profiler.profile(14);
                throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Class loader (", loader.getKlass().getType(), ") tried to define prohibited package name: ", str));
            }
            Symbol<Name> symbol = getNames().getOrCreate(str);
            if (packageTable.lookup(symbol) != null) {
                profiler.profile(13);
                throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Package ", str, " is already defined."));
            }
            pkgSymbols.add(symbol);
        }
        Symbol<Name> moduleSymbol = getNames().getOrCreate(moduleName);
        // Try define module
        ModuleEntry moduleEntry = moduleTable.createAndAddEntry(moduleSymbol, registry, is_open, module);
        if (moduleEntry == null) {
            // Module already defined
            profiler.profile(12);
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Module ", moduleName, " is already defined"));
        }
        // Register packages
        for (Symbol<Name> pkgSymbol : pkgSymbols) {
            PackageEntry pkgEntry = packageTable.createAndAddEntry(pkgSymbol, moduleEntry);
            // should have been checked before
            assert pkgEntry != null;
        }
        // Link guest module to its host representation
        meta.HIDDEN_MODULE_ENTRY.setHiddenObject(module, moduleEntry);
    }
    if (StaticObject.isNull(loader) && getContext().getVmProperties().bootClassPathType().isExplodedModule()) {
        profiler.profile(11);
        // If we have an exploded build, and the module is defined to the bootloader, prepend a
        // class path entry for this module.
        prependModuleClasspath(moduleName);
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ClassRegistry(com.oracle.truffle.espresso.impl.ClassRegistry) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ModuleEntry(com.oracle.truffle.espresso.impl.ModuleTable.ModuleEntry) ArrayList(java.util.ArrayList) ModuleTable(com.oracle.truffle.espresso.impl.ModuleTable) PackageTable(com.oracle.truffle.espresso.impl.PackageTable) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) EntryTable(com.oracle.truffle.espresso.impl.EntryTable) PackageEntry(com.oracle.truffle.espresso.impl.PackageTable.PackageEntry)

Example 19 with Name

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

the class JniEnv method GetStaticFieldID.

/**
 * <h3>jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
 * </h3>
 * <p>
 * Returns the field ID for a static field of a class. The field is specified by its name and
 * signature. The GetStatic<type>Field and SetStatic<type>Field families of accessor functions
 * use field IDs to retrieve static fields.
 * <p>
 * GetStaticFieldID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static 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 specified static field cannot be found.
 * @throws NoSuchFieldError if the specified static 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 GetStaticFieldID(@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;
    Field field = null;
    Symbol<Name> fieldName = getNames().lookup(name);
    if (fieldName != null) {
        Symbol<Type> fieldType = getTypes().lookup(type);
        if (fieldType != null) {
            Klass klass = clazz.getMirrorKlass();
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            field = klass.lookupField(fieldName, fieldType, Klass.LookupMode.STATIC_ONLY);
            assert field == null || field.getType().equals(fieldType);
        }
    }
    if (field == null || !field.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchFieldError, name);
    }
    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 20 with Name

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

the class JniEnv method GetStaticMethodID.

/**
 * <h3>jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char
 * *sig);</h3>
 * <p>
 * Returns the method ID for a static method of a class. The method is specified by its name and
 * signature.
 * <p>
 * GetStaticMethodID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static method name in a 0-terminated modified UTF-8 string.
 * @param signaturePtr the method signature in a 0-terminated modified UTF-8 string.
 * @return a method ID, or NULL if the operation fails.
 * @throws NoSuchMethodError if the specified static 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 GetStaticMethodID(@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) {
            // Throw a NoSuchMethodError exception if we have an instance of a
            // primitive java.lang.Class
            Klass klass = clazz.getMirrorKlass();
            if (klass.isPrimitive()) {
                Meta meta = getMeta();
                throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
            }
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            if (Name._clinit_.equals(methodName)) {
                // Never search superclasses for static initializers.
                method = klass.lookupDeclaredMethod(methodName, methodSignature);
            } else {
                method = klass.lookupMethod(methodName, methodSignature);
            }
        }
    }
    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)

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