use of com.oracle.truffle.espresso.impl.Klass 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.Klass 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.Klass in project graal by oracle.
the class StackWalk method synchronizedConstants.
private static boolean synchronizedConstants(Meta meta) {
Klass stackStreamFactory = meta.java_lang_StackStreamFactory;
StaticObject statics = stackStreamFactory.tryInitializeAndGetStatics();
assert DEFAULT_MODE == getConstantField(stackStreamFactory, statics, STRING_DEFAULT_MODE, meta);
assert FILL_CLASS_REFS_ONLY == getConstantField(stackStreamFactory, statics, STRING_FILL_CLASS_REFS_ONLY, meta);
assert GET_CALLER_CLASS == getConstantField(stackStreamFactory, statics, STRING_GET_CALLER_CLASS, meta);
assert SHOW_HIDDEN_FRAMES == getConstantField(stackStreamFactory, statics, STRING_SHOW_HIDDEN_FRAMES, meta);
assert FILL_LIVE_STACK_FRAMES == getConstantField(stackStreamFactory, statics, STRING_FILL_LIVE_STACK_FRAMES, meta);
return true;
}
use of com.oracle.truffle.espresso.impl.Klass 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.Klass in project graal by oracle.
the class VM method JVM_GetClassDeclaredConstructors.
// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(Constructor[].class)
public StaticObject JVM_GetClassDeclaredConstructors(@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.getDeclaredConstructors()) {
if (Name._init_.equals(m.getName()) && (!publicOnly || m.isPublic())) {
collectedMethods.add(m);
}
}
final Method[] constructors = collectedMethods.toArray(Method.EMPTY_ARRAY);
EspressoContext context = meta.getContext();
// TODO(peterssen): Cache guest j.l.reflect.Constructor constructor.
// Calling the constructor is just for validation, manually setting the fields would be
// faster.
Method constructorInit = meta.java_lang_reflect_Constructor.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* parameterTypes */
Type.java_lang_Class_array, /* checkedExceptions */
Type.java_lang_Class_array, /* modifiers */
Type._int, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array, /* parameterAnnotations */
Type._byte_array));
StaticObject arr = meta.java_lang_reflect_Constructor.allocateReferenceArray(constructors.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int i) {
final Method m = constructors[i];
Attribute rawRuntimeVisibleAnnotations = m.getAttribute(Name.RuntimeVisibleAnnotations);
StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleParameterAnnotations = m.getAttribute(Name.RuntimeVisibleParameterAnnotations);
StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleTypeAnnotations = m.getAttribute(Name.RuntimeVisibleTypeAnnotations);
StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
final Klass[] rawParameterKlasses = m.resolveParameterKlasses();
StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(m.getParameterCount(), new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int j) {
return rawParameterKlasses[j].mirror();
}
});
final Klass[] rawCheckedExceptions = m.getCheckedExceptions();
StaticObject checkedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int j) {
return rawCheckedExceptions[j].mirror();
}
});
SignatureAttribute signatureAttribute = (SignatureAttribute) m.getAttribute(Name.Signature);
StaticObject genericSignature = StaticObject.NULL;
if (signatureAttribute != null) {
String sig = m.getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
genericSignature = meta.toGuestString(sig);
}
StaticObject instance = meta.java_lang_reflect_Constructor.allocateInstance();
constructorInit.invokeDirect(/* this */
instance, /* declaringKlass */
m.getDeclaringKlass().mirror(), /* parameterTypes */
parameterTypes, /* checkedExceptions */
checkedExceptions, /* modifiers */
m.getMethodModifiers(), // TODO(peterssen): Fill method slot.
i, /* signature */
genericSignature, /* annotations */
runtimeVisibleAnnotations, /* parameterAnnotations */
runtimeVisibleParameterAnnotations);
meta.HIDDEN_CONSTRUCTOR_KEY.setHiddenObject(instance, m);
meta.HIDDEN_CONSTRUCTOR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
return instance;
}
});
return arr;
}
Aggregations