use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class VM method JVM_GetDeclaringClass.
@VmImpl(isJni = true)
@JavaType(Class.class)
public static StaticObject JVM_GetDeclaringClass(@JavaType(Class.class) StaticObject self) {
// Primitives and arrays are not "enclosed".
if (!(self.getMirrorKlass() instanceof ObjectKlass)) {
return StaticObject.NULL;
}
ObjectKlass k = (ObjectKlass) self.getMirrorKlass();
Klass outerKlass = computeEnclosingClass(k);
if (outerKlass == null) {
return StaticObject.NULL;
}
return outerKlass.mirror();
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class VM method JVM_ClassLoaderDepth.
@VmImpl(isJni = true)
public int JVM_ClassLoaderDepth() {
PrivilegedStack stack = getPrivilegedStack();
Integer res = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {
int depth = 0;
@Override
public Integer visitFrame(FrameInstance frameInstance) {
Method m = getMethodFromFrame(frameInstance);
if (m != null) {
if (isTrustedFrame(frameInstance, stack)) {
return -1;
}
if (!m.isNative()) {
ObjectKlass klass = m.getDeclaringKlass();
StaticObject loader = klass.getDefiningClassLoader();
if (StaticObject.notNull(loader) && !isTrustedLoader(loader)) {
return depth;
}
depth++;
}
}
return null;
}
});
return res == null ? -1 : res;
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class VM method JVM_GetClassSignature.
@VmImpl(isJni = true)
@JavaType(String.class)
public StaticObject JVM_GetClassSignature(@JavaType(Class.class) StaticObject self) {
if (self.getMirrorKlass() instanceof ObjectKlass) {
ObjectKlass klass = (ObjectKlass) self.getMirrorKlass();
SignatureAttribute signature = (SignatureAttribute) klass.getAttribute(Name.Signature);
if (signature != null) {
String sig = klass.getConstantPool().symbolAt(signature.getSignatureIndex(), "signature").toString();
return getMeta().toGuestString(sig);
}
}
return StaticObject.NULL;
}
use of com.oracle.truffle.espresso.impl.ObjectKlass 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);
}
use of com.oracle.truffle.espresso.impl.ObjectKlass in project graal by oracle.
the class InnerClassRedefiner method matchClassInfo.
private void matchClassInfo(HotSwapClassInfo hotSwapInfo, List<ObjectKlass> removedInnerClasses, Map<StaticObject, Map<Symbol<Symbol.Name>, Symbol<Symbol.Name>>> renamingRules) throws RedefintionNotSupportedException {
Klass klass = hotSwapInfo.getKlass();
// try to fetch all direct inner classes
// based on the constant pool in the class bytes
// by means of the defining class loader
fetchMissingInnerClasses(hotSwapInfo);
if (klass == null) {
// non-mapped hotSwapInfo means it's a new inner class that didn't map
// to any previous inner class
// we need to generate a new unique name and apply to nested inner classes
HotSwapClassInfo outerInfo = hotSwapInfo.getOuterClassInfo();
String name = outerInfo.addHotClassMarker();
hotSwapInfo.rename(context.getNames().getOrCreate(name));
addRenamingRule(renamingRules, hotSwapInfo.getClassLoader(), hotSwapInfo.getName(), hotSwapInfo.getNewName(), context);
} else {
ImmutableClassInfo previousInfo = getGlobalClassInfo(klass);
ArrayList<ImmutableClassInfo> previousInnerClasses = previousInfo.getImmutableInnerClasses();
ArrayList<HotSwapClassInfo> newInnerClasses = hotSwapInfo.getHotSwapInnerClasses();
// before matching
if (hotSwapInfo.isRenamed()) {
for (HotSwapClassInfo newInnerClass : newInnerClasses) {
newInnerClass.outerRenamed(hotSwapInfo.getName().toString(), hotSwapInfo.getNewName().toString());
}
}
if (previousInnerClasses.size() > 0 || newInnerClasses.size() > 0) {
ArrayList<ImmutableClassInfo> removedClasses = new ArrayList<>(previousInnerClasses);
for (HotSwapClassInfo info : newInnerClasses) {
ImmutableClassInfo bestMatch = null;
int maxScore = 0;
for (ImmutableClassInfo removedClass : removedClasses) {
int score = info.match(removedClass);
if (score > 0 && score > maxScore) {
maxScore = score;
bestMatch = removedClass;
if (maxScore == MAX_SCORE) {
// found a perfect match, so stop iterating
break;
}
}
}
if (bestMatch != null) {
removedClasses.remove(bestMatch);
// rename class and associate with previous klass object
if (!info.getName().equals(bestMatch.getName())) {
info.rename(bestMatch.getName());
addRenamingRule(renamingRules, info.getClassLoader(), info.getName(), info.getNewName(), context);
}
info.setKlass(bestMatch.getKlass());
}
}
for (ImmutableClassInfo removedClass : removedClasses) {
if (removedClass.getKlass() != null) {
removedInnerClasses.add(removedClass.getKlass());
}
}
}
}
for (HotSwapClassInfo innerClass : hotSwapInfo.getHotSwapInnerClasses()) {
matchClassInfo(innerClass, removedInnerClasses, renamingRules);
}
}
Aggregations