use of com.oracle.truffle.espresso.runtime.EspressoException 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.runtime.EspressoException in project graal by oracle.
the class NativeEnv method intrinsicWrapper.
private Callback intrinsicWrapper(CallableFromNative.Factory factory) {
int extraArg = (factory.prependEnv()) ? 1 : 0;
return new Callback(factory.parameterCount() + extraArg, new Callback.Function() {
@CompilerDirectives.CompilationFinal
private CallableFromNative subst = null;
@Override
public Object call(Object... args) {
boolean isJni = factory.prependEnv();
try {
if (subst == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
subst = factory.create(getMeta());
}
return subst.invoke(NativeEnv.this, args);
} catch (EspressoException | StackOverflowError | OutOfMemoryError e) {
if (isJni) {
// This will most likely SOE again. Nothing we can do about that
// unfortunately.
EspressoException wrappedError = (e instanceof EspressoException) ? (EspressoException) e : (e instanceof StackOverflowError) ? getContext().getStackOverflow() : getContext().getOutOfMemory();
jni().setPendingException(wrappedError);
return defaultValue(factory.returnType());
}
throw e;
}
}
});
}
use of com.oracle.truffle.espresso.runtime.EspressoException in project graal by oracle.
the class NativeMethodNode method maybeThrowAndClearPendingException.
private void maybeThrowAndClearPendingException(JniEnv jniEnv) {
EspressoException ex = jniEnv.getPendingEspressoException();
if (ex != null) {
enterThrowsException();
jniEnv.clearPendingException();
throw ex;
}
}
use of com.oracle.truffle.espresso.runtime.EspressoException 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;
}
Aggregations