use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class VM method JVM_GetPermittedSubclasses.
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class[].class)
public StaticObject JVM_GetPermittedSubclasses(@JavaType(Class.class) StaticObject self) {
Klass k = self.getMirrorKlass();
if (!(k instanceof ObjectKlass)) {
return StaticObject.NULL;
}
ObjectKlass klass = (ObjectKlass) k;
if (!klass.isSealed()) {
return StaticObject.NULL;
}
char[] classes = ((PermittedSubclassesAttribute) klass.getAttribute(PermittedSubclassesAttribute.NAME)).getClasses();
StaticObject[] permittedSubclasses = new StaticObject[classes.length];
RuntimeConstantPool pool = klass.getConstantPool();
int nClasses = 0;
for (int index : classes) {
Klass permitted;
try {
permitted = pool.resolvedKlassAt(klass, index);
} catch (EspressoException e) {
/* Suppress and continue */
continue;
}
if (permitted instanceof ObjectKlass) {
permittedSubclasses[nClasses++] = permitted.mirror();
}
}
if (nClasses == permittedSubclasses.length) {
return StaticObject.createArray(getMeta().java_lang_Class_array, permittedSubclasses);
}
return getMeta().java_lang_Class.allocateReferenceArray(nClasses, (i) -> permittedSubclasses[i]);
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class VM method JVM_CurrentLoadedClass.
@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_CurrentLoadedClass() {
PrivilegedStack stack = getPrivilegedStack();
StaticObject mirrorKlass = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<StaticObject>() {
@Override
public StaticObject visitFrame(FrameInstance frameInstance) {
Method m = getMethodFromFrame(frameInstance);
if (m != null) {
if (isTrustedFrame(frameInstance, stack)) {
return StaticObject.NULL;
}
if (!m.isNative()) {
ObjectKlass klass = m.getDeclaringKlass();
StaticObject loader = klass.getDefiningClassLoader();
if (StaticObject.notNull(loader) && !isTrustedLoader(loader)) {
return klass.mirror();
}
}
}
return null;
}
});
return mirrorKlass == null ? StaticObject.NULL : mirrorKlass;
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class Meta method knownKlass.
private ObjectKlass knownKlass(Symbol<Type> type, StaticObject classLoader) {
CompilerAsserts.neverPartOfCompilation();
assert !Types.isArray(type);
assert !Types.isPrimitive(type);
ObjectKlass k = loadKlassOrNull(type, classLoader);
if (k == null) {
throw EspressoError.shouldNotReachHere("Failed loading known class: ", type, ", discovered java version: ", getJavaVersion());
}
return k;
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class Target_sun_reflect_NativeMethodAccessorImpl method callMethodReflectively.
@JavaType(Object.class)
public static StaticObject callMethodReflectively(Meta meta, @JavaType(Object.class) StaticObject receiver, @JavaType(Object[].class) StaticObject args, Method m, Klass klass, @JavaType(Class[].class) StaticObject parameterTypes) {
// Klass should be initialized if method is static, and could be delayed until method
// invocation, according to specs. However, JCK tests that it is indeed always initialized
// before doing anything, even if the method to be invoked is from another class.
klass.safeInitialize();
Method reflectedMethod = m;
if (reflectedMethod.isRemovedByRedefition()) {
reflectedMethod = m.getContext().getClassRedefinition().handleRemovedMethod(reflectedMethod, reflectedMethod.isStatic() ? reflectedMethod.getDeclaringKlass() : receiver.getKlass());
}
// actual method to invoke
Method method;
// target klass, receiver's klass for non-static
Klass targetKlass;
if (reflectedMethod.isStatic()) {
// Ignore receiver argument;.
method = reflectedMethod;
targetKlass = klass;
} else {
if (StaticObject.isNull(receiver)) {
throw meta.throwNullPointerException();
}
// Check class of receiver against class declaring method.
if (!klass.isAssignableFrom(receiver.getKlass())) {
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "object is not an instance of declaring class");
}
// target klass is receiver's klass
targetKlass = receiver.getKlass();
// no need to resolve if method is private or <init>
if (reflectedMethod.isPrivate() || Name._init_.equals(reflectedMethod.getName())) {
method = reflectedMethod;
} else {
// resolve based on the receiver
if (reflectedMethod.getDeclaringKlass().isInterface()) {
// resolve interface call
// Match resolution errors with those thrown due to reflection inlining
// Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
method = reflectedMethod;
assert targetKlass instanceof ObjectKlass;
method = ((ObjectKlass) targetKlass).itableLookup(method.getDeclaringKlass(), method.getITableIndex());
if (method != null) {
// Check for abstract methods as well
if (!method.hasCode()) {
// new default: 65315
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
}
}
} else {
// if the method can be overridden, we resolve using the vtable index.
method = reflectedMethod;
// VTable is live, use it
method = targetKlass.vtableLookup(method.getVTableIndex());
if (method != null) {
// Check for abstract methods as well
if (method.isAbstract()) {
// new default: 65315
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
}
}
}
}
}
// an internal vtable bug. If you ever get this please let Karen know.
if (method == null) {
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, "please let Karen know");
}
int argsLen = StaticObject.isNull(args) ? 0 : args.length();
final Symbol<Type>[] signature = method.getParsedSignature();
// Check number of arguments.
if (Signatures.parameterCount(signature, false) != argsLen) {
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "wrong number of arguments!");
}
Object[] adjustedArgs = new Object[argsLen];
for (int i = 0; i < argsLen; ++i) {
StaticObject arg = args.get(i);
StaticObject paramTypeMirror = parameterTypes.get(i);
Klass paramKlass = paramTypeMirror.getMirrorKlass();
// Throws guest IllegallArgumentException if the parameter cannot be casted or widened.
adjustedArgs[i] = checkAndWiden(meta, arg, paramKlass);
}
Object result;
try {
result = method.invokeDirect(receiver, adjustedArgs);
} catch (EspressoException e) {
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, e.getGuestException());
}
if (reflectedMethod.getReturnKind() == JavaKind.Void) {
return StaticObject.NULL;
}
if (reflectedMethod.getReturnKind().isPrimitive()) {
return Meta.box(meta, result);
}
// Result is not void nor primitive, pass through.
return (StaticObject) result;
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class Target_sun_misc_Unsafe method defineAnonymousClass.
@TruffleBoundary
@Substitution(hasReceiver = true, nameProvider = SharedUnsafeAppend0.class)
@SuppressWarnings("unused")
@JavaType(Class.class)
public static StaticObject defineAnonymousClass(@JavaType(Unsafe.class) StaticObject self, @JavaType(Class.class) StaticObject hostClass, @JavaType(byte[].class) StaticObject data, @JavaType(Object[].class) StaticObject constantPoolPatches, @Inject Meta meta) {
if (StaticObject.isNull(hostClass) || StaticObject.isNull(data)) {
throw meta.throwNullPointerException();
}
if (hostClass.getMirrorKlass().isArray() || hostClass.getMirrorKlass().isPrimitive()) {
throw meta.throwException(meta.java_lang_IllegalArgumentException);
}
byte[] bytes = data.unwrap();
ObjectKlass hostKlass = (ObjectKlass) hostClass.getMirrorKlass();
StaticObject pd = (StaticObject) meta.HIDDEN_PROTECTION_DOMAIN.getHiddenObject(hostClass);
StaticObject[] patches = StaticObject.isNull(constantPoolPatches) ? null : constantPoolPatches.unwrap();
// Inherit host class's protection domain.
ClassRegistry.ClassDefinitionInfo info = new ClassRegistry.ClassDefinitionInfo(pd, hostKlass, patches);
ObjectKlass k = meta.getRegistries().defineKlass(null, bytes, hostKlass.getDefiningClassLoader(), info);
// Initialize, because no one else will.
k.safeInitialize();
return k.mirror();
}
Aggregations