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;
}
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;
}
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();
}
});
}
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));
}
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;
}
Aggregations