Search in sources :

Example 6 with ObjectKlass

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();
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 7 with ObjectKlass

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;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) FrameInstance(com.oracle.truffle.api.frame.FrameInstance)

Example 8 with ObjectKlass

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;
}
Also used : SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 9 with ObjectKlass

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);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) Method(com.oracle.truffle.espresso.impl.Method) NameAndTypeConstant(com.oracle.truffle.espresso.classfile.constantpool.NameAndTypeConstant) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) Field(com.oracle.truffle.espresso.impl.Field) ParserField(com.oracle.truffle.espresso.impl.ParserField) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) ConstantPool(com.oracle.truffle.espresso.classfile.ConstantPool)

Example 10 with ObjectKlass

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);
    }
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayList(java.util.ArrayList)

Aggregations

ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)35 Klass (com.oracle.truffle.espresso.impl.Klass)15 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)12 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)10 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)9 ArrayList (java.util.ArrayList)8 Method (com.oracle.truffle.espresso.impl.Method)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)5 Field (com.oracle.truffle.espresso.impl.Field)5 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)5 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)5 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)4 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)4 ClassRegistry (com.oracle.truffle.espresso.impl.ClassRegistry)4 Meta (com.oracle.truffle.espresso.meta.Meta)4 Matcher (java.util.regex.Matcher)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)3 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)3 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2