Search in sources :

Example 16 with Klass

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

the class VM method JVM_GetClassDeclaredMethods.

// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Method[].class)
public StaticObject JVM_GetClassDeclaredMethods(@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.getDeclaredMethods()) {
        if ((!publicOnly || m.isPublic()) && // Filter out <init> and <clinit> from reflection.
        !Name._init_.equals(m.getName()) && !Name._clinit_.equals(m.getName())) {
            collectedMethods.add(m);
        }
    }
    final Method[] methods = collectedMethods.toArray(Method.EMPTY_ARRAY);
    return meta.java_lang_reflect_Method.allocateReferenceArray(methods.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            return methods[i].makeMirror();
        }
    });
}
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) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ArrayList(java.util.ArrayList) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 17 with Klass

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

the class Target_sun_reflect_NativeConstructorAccessorImpl method newInstance0.

@Substitution
@JavaType(Object.class)
public static StaticObject newInstance0(@JavaType(Constructor.class) StaticObject constructor, @JavaType(Object[].class) StaticObject args0, @Inject Meta meta) {
    Klass klass = meta.java_lang_reflect_Constructor_clazz.getObject(constructor).getMirrorKlass();
    klass.safeInitialize();
    if (klass.isArray() || klass.isPrimitive() || klass.isInterface() || klass.isAbstract()) {
        throw meta.throwException(meta.java_lang_InstantiationException);
    }
    Method reflectedMethod = Method.getHostReflectiveConstructorRoot(constructor, meta);
    StaticObject instance = klass.allocateInstance();
    StaticObject parameterTypes = meta.java_lang_reflect_Constructor_parameterTypes.getObject(constructor);
    Target_sun_reflect_NativeMethodAccessorImpl.callMethodReflectively(meta, instance, args0, reflectedMethod, klass, parameterTypes);
    return instance;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method)

Example 18 with Klass

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

the class Target_sun_reflect_NativeMethodAccessorImpl method invoke0.

/**
 * Invokes the underlying method represented by this {@code Method} object, on the specified
 * object with the specified parameters. Individual parameters are automatically unwrapped to
 * match primitive formal parameters, and both primitive and reference parameters are subject to
 * method invocation conversions as necessary.
 *
 * <p>
 * If the underlying method is static, then the specified {@code receiver} argument is ignored.
 * It may be null.
 *
 * <p>
 * If the number of formal parameters required by the underlying method is 0, the supplied
 * {@code args} array may be of length 0 or null.
 *
 * <p>
 * If the underlying method is an instance method, it is invoked using dynamic method lookup as
 * documented in The Java Language Specification, Second Edition, section 15.12.4.4; in
 * particular, overriding based on the runtime type of the target object will occur.
 *
 * <p>
 * If the underlying method is static, the class that declared the method is initialized if it
 * has not already been initialized.
 *
 * <p>
 * If the method completes normally, the value it returns is returned to the caller of invoke;
 * if the value has a primitive type, it is first appropriately wrapped in an object. However,
 * if the value has the type of an array of a primitive type, the elements of the array are
 * <i>not</i> wrapped in objects; in other words, an array of primitive type is returned. If the
 * underlying method return type is void, the invocation returns null.
 *
 * @param receiver the object the underlying method is invoked from
 * @param args the arguments used for the method call
 * @return the result of dispatching the method represented by this object on {@code receiver}
 *         with parameters {@code args}
 *
 *         IllegalAccessException if this {@code Method} object is enforcing Java language
 *         access control and the underlying method is inaccessible.
 *
 *         IllegalArgumentException if the method is an instance method and the specified object
 *         argument is not an instance of the class or interface declaring the underlying method
 *         (or of a subclass or implementor thereof); if the number of actual and formal
 *         parameters differ; if an unwrapping conversion for primitive arguments fails; or if,
 *         after possible unwrapping, a parameter value cannot be converted to the corresponding
 *         formal parameter type by a method invocation conversion. // @exception
 *         InvocationTargetException if the underlying method throws an exception.
 *
 *         NullPointerException if the specified object is null and the method is an instance
 *         method. exception ExceptionInInitializerError if the initialization provoked by this
 *         method fails.
 */
@Substitution
@JavaType(Object.class)
public static StaticObject invoke0(@JavaType(java.lang.reflect.Method.class) StaticObject guestMethod, @JavaType(Object.class) StaticObject receiver, @JavaType(Object[].class) StaticObject args, @Inject Meta meta) {
    StaticObject curMethod = guestMethod;
    Method reflectedMethod = null;
    while (reflectedMethod == null) {
        reflectedMethod = (Method) meta.HIDDEN_METHOD_KEY.getHiddenObject(curMethod);
        if (reflectedMethod == null) {
            curMethod = meta.java_lang_reflect_Method_root.getObject(curMethod);
        }
    }
    Klass klass = meta.java_lang_reflect_Method_clazz.getObject(guestMethod).getMirrorKlass();
    if (klass == meta.java_lang_invoke_MethodHandle && (reflectedMethod.getName() == Name.invoke || reflectedMethod.getName() == Name.invokeExact)) {
        StaticObject cause = Meta.initExceptionWithMessage(meta.java_lang_UnsupportedOperationException, "Cannot reflectively invoke MethodHandle.{invoke,invokeExact}");
        StaticObject invocationTargetException = Meta.initExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, cause);
        throw meta.throwException(invocationTargetException);
    }
    StaticObject parameterTypes = meta.java_lang_reflect_Method_parameterTypes.getObject(guestMethod);
    StaticObject result = callMethodReflectively(meta, receiver, args, reflectedMethod, klass, parameterTypes);
    return result;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method)

Example 19 with Klass

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

the class Target_sun_misc_Unsafe method arrayIndexScale.

/**
 * Report the scale factor for addressing elements in the storage allocation of a given array
 * class. However, arrays of "narrow" types will generally not work properly with accessors like
 * {@link #getByte}, so the scale factor for such classes is reported as zero.
 *
 * @see #arrayBaseOffset
 * @see #getInt
 * @see #putInt
 */
@Substitution(hasReceiver = true, nameProvider = SharedUnsafeAppend0.class)
public static int arrayIndexScale(@SuppressWarnings("unused") @JavaType(Unsafe.class) StaticObject self, @JavaType(Class.class) StaticObject clazz, @Inject Meta meta) {
    Unsafe unsafe = UnsafeAccess.getIfAllowed(meta);
    Klass klass = clazz.getMirrorKlass();
    assert klass.isArray();
    if (((ArrayKlass) klass).getComponentType().isPrimitive()) {
        Class<?> hostPrimitive = ((ArrayKlass) klass).getComponentType().getJavaKind().toJavaClass();
        return unsafe.arrayIndexScale(Array.newInstance(hostPrimitive, 0).getClass());
    } else {
        // Just a reference type.
        return unsafe.arrayIndexScale(Object[].class);
    }
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) Unsafe(sun.misc.Unsafe) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 20 with Klass

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

the class ClassInfo method create.

public static ImmutableClassInfo create(Klass klass, InnerClassRedefiner innerClassRedefiner) {
    StringBuilder hierarchy = new StringBuilder();
    StringBuilder methods = new StringBuilder();
    StringBuilder fields = new StringBuilder();
    StringBuilder enclosing = new StringBuilder();
    Symbol<Name> name = klass.getName();
    Matcher matcher = InnerClassRedefiner.ANON_INNER_CLASS_PATTERN.matcher(name.toString());
    if (matcher.matches()) {
        // fingerprints are only relevant for inner classes
        hierarchy.append(klass.getSuperClass().getTypeAsString()).append(";");
        for (Klass itf : klass.getImplementedInterfaces()) {
            hierarchy.append(itf.getTypeAsString()).append(";");
        }
        for (Method method : klass.getDeclaredMethods()) {
            methods.append(method.getNameAsString()).append(";");
            methods.append(method.getSignatureAsString()).append(";");
        }
        for (Field field : klass.getDeclaredFields()) {
            fields.append(field.getTypeAsString()).append(";");
            fields.append(field.getNameAsString()).append(";");
        }
        ObjectKlass objectKlass = (ObjectKlass) klass;
        ConstantPool pool = klass.getConstantPool();
        NameAndTypeConstant nmt = pool.nameAndTypeAt(objectKlass.getEnclosingMethod().getMethodIndex());
        enclosing.append(nmt.getName(pool)).append(";").append(nmt.getDescriptor(pool));
    }
    // find all currently loaded direct inner classes and create class infos
    ArrayList<ImmutableClassInfo> inners = new ArrayList<>(1);
    Set<ObjectKlass> loadedInnerClasses = innerClassRedefiner.findLoadedInnerClasses(klass);
    for (Klass inner : loadedInnerClasses) {
        matcher = InnerClassRedefiner.ANON_INNER_CLASS_PATTERN.matcher(inner.getNameAsString());
        // only add anonymous inner classes
        if (matcher.matches()) {
            inners.add(innerClassRedefiner.getGlobalClassInfo(inner));
        }
    }
    return new ImmutableClassInfo((ObjectKlass) klass, name, klass.getDefiningClassLoader(), hierarchy.toString(), methods.toString(), fields.toString(), enclosing.toString(), inners, null);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) Method(com.oracle.truffle.espresso.impl.Method) NameAndTypeConstant(com.oracle.truffle.espresso.classfile.constantpool.NameAndTypeConstant) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) Field(com.oracle.truffle.espresso.impl.Field) ParserField(com.oracle.truffle.espresso.impl.ParserField) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) ConstantPool(com.oracle.truffle.espresso.classfile.ConstantPool)

Aggregations

Klass (com.oracle.truffle.espresso.impl.Klass)71 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)54 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)49 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)33 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)24 Meta (com.oracle.truffle.espresso.meta.Meta)21 Method (com.oracle.truffle.espresso.impl.Method)19 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)10 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)10 ExportMessage (com.oracle.truffle.api.library.ExportMessage)8 Field (com.oracle.truffle.espresso.impl.Field)7 ArrayList (java.util.ArrayList)7 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)6 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)6 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)5 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5 NativeType (com.oracle.truffle.espresso.ffi.NativeType)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3