Search in sources :

Example 51 with Meta

use of com.oracle.truffle.espresso.meta.Meta 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 52 with Meta

use of com.oracle.truffle.espresso.meta.Meta 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 53 with Meta

use of com.oracle.truffle.espresso.meta.Meta in project graal by oracle.

the class VM method JVM_GetClassDeclaredConstructors.

// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(Constructor[].class)
public StaticObject JVM_GetClassDeclaredConstructors(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    Meta meta = getMeta();
    ArrayList<Method> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Method m : klass.getDeclaredConstructors()) {
        if (Name._init_.equals(m.getName()) && (!publicOnly || m.isPublic())) {
            collectedMethods.add(m);
        }
    }
    final Method[] constructors = collectedMethods.toArray(Method.EMPTY_ARRAY);
    EspressoContext context = meta.getContext();
    // TODO(peterssen): Cache guest j.l.reflect.Constructor constructor.
    // Calling the constructor is just for validation, manually setting the fields would be
    // faster.
    Method constructorInit = meta.java_lang_reflect_Constructor.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
    Type.java_lang_Class, /* parameterTypes */
    Type.java_lang_Class_array, /* checkedExceptions */
    Type.java_lang_Class_array, /* modifiers */
    Type._int, /* slot */
    Type._int, /* signature */
    Type.java_lang_String, /* annotations */
    Type._byte_array, /* parameterAnnotations */
    Type._byte_array));
    StaticObject arr = meta.java_lang_reflect_Constructor.allocateReferenceArray(constructors.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            final Method m = constructors[i];
            Attribute rawRuntimeVisibleAnnotations = m.getAttribute(Name.RuntimeVisibleAnnotations);
            StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleParameterAnnotations = m.getAttribute(Name.RuntimeVisibleParameterAnnotations);
            StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleTypeAnnotations = m.getAttribute(Name.RuntimeVisibleTypeAnnotations);
            StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
            final Klass[] rawParameterKlasses = m.resolveParameterKlasses();
            StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(m.getParameterCount(), new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawParameterKlasses[j].mirror();
                }
            });
            final Klass[] rawCheckedExceptions = m.getCheckedExceptions();
            StaticObject checkedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawCheckedExceptions[j].mirror();
                }
            });
            SignatureAttribute signatureAttribute = (SignatureAttribute) m.getAttribute(Name.Signature);
            StaticObject genericSignature = StaticObject.NULL;
            if (signatureAttribute != null) {
                String sig = m.getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
                genericSignature = meta.toGuestString(sig);
            }
            StaticObject instance = meta.java_lang_reflect_Constructor.allocateInstance();
            constructorInit.invokeDirect(/* this */
            instance, /* declaringKlass */
            m.getDeclaringKlass().mirror(), /* parameterTypes */
            parameterTypes, /* checkedExceptions */
            checkedExceptions, /* modifiers */
            m.getMethodModifiers(), // TODO(peterssen): Fill method slot.
            i, /* signature */
            genericSignature, /* annotations */
            runtimeVisibleAnnotations, /* parameterAnnotations */
            runtimeVisibleParameterAnnotations);
            meta.HIDDEN_CONSTRUCTOR_KEY.setHiddenObject(instance, m);
            meta.HIDDEN_CONSTRUCTOR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
            return instance;
        }
    });
    return arr;
}
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) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) 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) IntFunction(java.util.function.IntFunction) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 54 with Meta

use of com.oracle.truffle.espresso.meta.Meta 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 55 with Meta

use of com.oracle.truffle.espresso.meta.Meta in project graal by oracle.

the class VM method JVM_FindClassFromCaller.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class.class)
public StaticObject JVM_FindClassFromCaller(@Pointer TruffleObject namePtr, boolean init, @JavaType(ClassLoader.class) StaticObject loader, @JavaType(Class.class) StaticObject caller) {
    Meta meta = getMeta();
    Symbol<Type> type = namePtrToInternal(namePtr);
    Klass result;
    if (Types.isPrimitive(type)) {
        result = null;
    } else {
        StaticObject protectionDomain;
        // manager to avoid the performance cost of getting the calling class.
        if (!StaticObject.isNull(caller) && !StaticObject.isNull(loader)) {
            protectionDomain = JVM_GetProtectionDomain(caller);
        } else {
            protectionDomain = StaticObject.NULL;
        }
        result = meta.resolveSymbolOrNull(type, loader, protectionDomain);
    }
    if (result == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_ClassNotFoundException, NativeUtils.interopPointerToString(namePtr));
    }
    if (init) {
        result.safeInitialize();
    }
    return result.mirror();
}
Also used : 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) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

Meta (com.oracle.truffle.espresso.meta.Meta)82 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)29 Method (com.oracle.truffle.espresso.impl.Method)27 ExportMessage (com.oracle.truffle.api.library.ExportMessage)24 Klass (com.oracle.truffle.espresso.impl.Klass)21 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)21 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)21 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)20 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)17 ArityException (com.oracle.truffle.api.interop.ArityException)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)9 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)8 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)6 NativeType (com.oracle.truffle.espresso.ffi.NativeType)4 Field (com.oracle.truffle.espresso.impl.Field)4 ArrayList (java.util.ArrayList)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)3 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)3