Search in sources :

Example 21 with ObjectKlass

use of com.oracle.truffle.espresso.impl.ObjectKlass 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)

Example 22 with ObjectKlass

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

the class VM method computeEnclosingClass.

/**
 * Return the enclosing class; or null for: primitives, arrays, anonymous classes (declared
 * inside methods).
 */
private static Klass computeEnclosingClass(ObjectKlass klass) {
    InnerClassesAttribute innerClasses = (InnerClassesAttribute) klass.getAttribute(InnerClassesAttribute.NAME);
    if (innerClasses == null) {
        return null;
    }
    RuntimeConstantPool pool = klass.getConstantPool();
    boolean found = false;
    Klass outerKlass = null;
    for (InnerClassesAttribute.Entry entry : innerClasses.entries()) {
        if (entry.innerClassIndex != 0) {
            Symbol<Name> innerDescriptor = pool.classAt(entry.innerClassIndex).getName(pool);
            // Check decriptors/names before resolving.
            if (innerDescriptor.equals(klass.getName())) {
                Klass innerKlass = pool.resolvedKlassAt(klass, entry.innerClassIndex);
                found = (innerKlass == klass);
                if (found && entry.outerClassIndex != 0) {
                    outerKlass = pool.resolvedKlassAt(klass, entry.outerClassIndex);
                }
            }
        }
        if (found) {
            break;
        }
    }
    // the system could allow a spoof of an inner class to gain access rights.
    return outerKlass;
}
Also used : 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) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 23 with ObjectKlass

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

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

the class VM method JVM_GetRecordComponents.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(internalName = "[Ljava/lang/reflect/RecordComponent;")
public StaticObject JVM_GetRecordComponents(@JavaType(Class.class) StaticObject self) {
    Klass k = self.getMirrorKlass();
    if (!(k instanceof ObjectKlass)) {
        return StaticObject.NULL;
    }
    ObjectKlass klass = (ObjectKlass) k;
    RecordAttribute record = (RecordAttribute) klass.getAttribute(RecordAttribute.NAME);
    if (record == null) {
        return StaticObject.NULL;
    }
    RecordAttribute.RecordComponentInfo[] components = record.getComponents();
    return getMeta().java_lang_reflect_RecordComponent.allocateReferenceArray(components.length, (i) -> components[i].toGuestComponent(getMeta(), klass));
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 25 with ObjectKlass

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

the class VM method JVM_Clone.

@VmImpl(isJni = true)
@JavaType(Object.class)
public static StaticObject JVM_Clone(@JavaType(Object.class) StaticObject self, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    assert StaticObject.notNull(self);
    char exceptionBranch = 3;
    if (self.isArray()) {
        // Arrays are always cloneable.
        if (self.isForeignObject()) {
            return cloneForeignArray(self, meta, InteropLibrary.getUncached(self.rawForeignObject()), ToEspressoNodeGen.getUncached(), profiler, exceptionBranch);
        }
        return self.copy();
    }
    if (self.isForeignObject()) {
        profiler.profile(exceptionBranch);
        throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Clone not supported for non-array foreign objects");
    }
    if (!meta.java_lang_Cloneable.isAssignableFrom(self.getKlass())) {
        profiler.profile(0);
        throw meta.throwException(meta.java_lang_CloneNotSupportedException);
    }
    if (InterpreterToVM.instanceOf(self, meta.java_lang_ref_Reference)) {
        // Non-strong references are not cloneable.
        if (// 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_WeakReference) || // 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_SoftReference) || // 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_FinalReference) || InterpreterToVM.instanceOf(self, meta.java_lang_ref_PhantomReference)) {
            profiler.profile(1);
            throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, self.getKlass().getName().toString());
        }
    }
    final StaticObject clone = self.copy();
    // If the original object is finalizable, so is the copy.
    assert self.getKlass() instanceof ObjectKlass;
    if (((ObjectKlass) self.getKlass()).hasFinalizer()) {
        profiler.profile(2);
        meta.java_lang_ref_Finalizer_register.invokeDirect(null, clone);
    }
    return clone;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Aggregations

ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)35 Klass (com.oracle.truffle.espresso.impl.Klass)15 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)12 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)10 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)9 ArrayList (java.util.ArrayList)8 Method (com.oracle.truffle.espresso.impl.Method)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)5 Field (com.oracle.truffle.espresso.impl.Field)5 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)5 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)5 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)4 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)4 ClassRegistry (com.oracle.truffle.espresso.impl.ClassRegistry)4 Meta (com.oracle.truffle.espresso.meta.Meta)4 Matcher (java.util.regex.Matcher)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)3 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)3 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2