Search in sources :

Example 36 with Field

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

the class Target_sun_misc_Unsafe method putByte.

@Substitution(hasReceiver = true)
public static void putByte(@SuppressWarnings("unused") @JavaType(Unsafe.class) StaticObject self, @JavaType(Object.class) StaticObject holder, long offset, byte value, @Inject Meta meta) {
    if (isNullOrArray(holder)) {
        UnsafeAccess.getIfAllowed(meta).putByte(unwrapNullOrArray(holder), offset, value);
        return;
    }
    // TODO(peterssen): Current workaround assumes it's a field access, encoding is offset <->
    // field index.
    Field f = getInstanceFieldFromIndex(holder, Math.toIntExact(offset) - SAFETY_FIELD_OFFSET);
    assert f != null;
    f.setByte(holder, value);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field)

Example 37 with Field

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

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

the class InterpreterToVM method contendedMonitorEnter.

@TruffleBoundary
/*- Throwable.addSuppressed blacklisted by SVM (from try-with-resources) */
@SuppressWarnings("try")
private static void contendedMonitorEnter(StaticObject obj, Meta meta, EspressoLock lock, EspressoContext context) {
    StaticObject thread = context.getCurrentThread();
    try (Transition transition = Transition.transition(context, State.BLOCKED)) {
        if (context.EnableManagement) {
            // Locks bookkeeping.
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(thread, obj);
            Field blockedCount = meta.HIDDEN_THREAD_BLOCKED_COUNT;
            Target_java_lang_Thread.incrementThreadCounter(thread, blockedCount);
        }
        final boolean report = context.shouldReportVMEvents();
        if (report) {
            context.reportOnContendedMonitorEnter(obj);
        }
        monitorUnsafeEnter(lock);
        if (report) {
            context.reportOnContendedMonitorEntered(obj);
        }
        if (context.EnableManagement) {
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(thread, null);
        }
    }
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Transition(com.oracle.truffle.espresso.threads.Transition) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 39 with Field

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

the class JniEnv method SetStaticShortField.

@JniImpl
public void SetStaticShortField(@SuppressWarnings("unused") @JavaType(Class.class) StaticObject unused, @Handle(Field.class) long fieldId, short val) {
    Field field = fieldIds.getObject(fieldId);
    assert field.isStatic();
    field.set(field.getDeclaringKlass().tryInitializeAndGetStatics(), val);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field)

Example 40 with Field

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

Aggregations

Field (com.oracle.truffle.espresso.impl.Field)60 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)8 Klass (com.oracle.truffle.espresso.impl.Klass)7 Method (com.oracle.truffle.espresso.impl.Method)5 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)5 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)5 ExportMessage (com.oracle.truffle.api.library.ExportMessage)4 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)4 Meta (com.oracle.truffle.espresso.meta.Meta)4 ArrayList (java.util.ArrayList)4 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)3 ParserField (com.oracle.truffle.espresso.impl.ParserField)3 REF_getField (com.oracle.truffle.espresso.classfile.Constants.REF_getField)2 REF_putField (com.oracle.truffle.espresso.classfile.Constants.REF_putField)2 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)2 NativeType (com.oracle.truffle.espresso.ffi.NativeType)2 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)2 ParserMethod (com.oracle.truffle.espresso.impl.ParserMethod)2 RedefineAddedField (com.oracle.truffle.espresso.impl.RedefineAddedField)2