Search in sources :

Example 1 with ConstantPool

use of com.oracle.truffle.espresso.classfile.ConstantPool 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 2 with ConstantPool

use of com.oracle.truffle.espresso.classfile.ConstantPool in project graal by oracle.

the class ClassInfo method create.

public static HotSwapClassInfo create(ObjectKlass klass, Symbol<Name> name, byte[] bytes, StaticObject definingLoader, EspressoContext context) {
    Symbol<Type> type = context.getTypes().fromName(name);
    ParserKlass parserKlass = ClassfileParser.parse(new ClassfileStream(bytes, null), definingLoader, type, context);
    StringBuilder hierarchy = new StringBuilder();
    StringBuilder methods = new StringBuilder();
    StringBuilder fields = new StringBuilder();
    StringBuilder enclosing = new StringBuilder();
    Matcher matcher = InnerClassRedefiner.ANON_INNER_CLASS_PATTERN.matcher(name.toString());
    if (matcher.matches()) {
        // fingerprints are only relevant for inner classes
        hierarchy.append(parserKlass.getSuperKlass().toString()).append(";");
        for (Symbol<Type> itf : parserKlass.getSuperInterfaces()) {
            hierarchy.append(itf.toString()).append(";");
        }
        for (ParserMethod method : parserKlass.getMethods()) {
            methods.append(method.getName().toString()).append(";");
            methods.append(method.getSignature().toString()).append(";");
        }
        for (ParserField field : parserKlass.getFields()) {
            fields.append(field.getType().toString()).append(";");
            fields.append(field.getName().toString()).append(";");
        }
        ConstantPool pool = parserKlass.getConstantPool();
        EnclosingMethodAttribute attr = (EnclosingMethodAttribute) parserKlass.getAttribute(EnclosingMethodAttribute.NAME);
        NameAndTypeConstant nmt = pool.nameAndTypeAt(attr.getMethodIndex());
        enclosing.append(nmt.getName(pool)).append(";").append(nmt.getDescriptor(pool));
    }
    return new HotSwapClassInfo(klass, name, definingLoader, hierarchy.toString(), methods.toString(), fields.toString(), enclosing.toString(), new ArrayList<>(1), bytes);
}
Also used : ClassfileStream(com.oracle.truffle.espresso.classfile.ClassfileStream) Matcher(java.util.regex.Matcher) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) NameAndTypeConstant(com.oracle.truffle.espresso.classfile.constantpool.NameAndTypeConstant) ParserField(com.oracle.truffle.espresso.impl.ParserField) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) ConstantPool(com.oracle.truffle.espresso.classfile.ConstantPool) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)

Example 3 with ConstantPool

use of com.oracle.truffle.espresso.classfile.ConstantPool in project graal by oracle.

the class BytecodeStream method printBytecode.

public void printBytecode(Klass klass, PrintStream out) {
    try {
        ConstantPool pool = klass.getConstantPool();
        int bci = 0;
        int nextBCI = 0;
        StringBuilder str = new StringBuilder();
        while (nextBCI < endBCI()) {
            str.setLength(0);
            bci = nextBCI;
            int opcode = currentBC(bci);
            str.append(bci).append(": ").append(Bytecodes.nameOf(opcode)).append(" ");
            nextBCI = nextBCI(bci);
            if (Bytecodes.isBranch(opcode)) {
                // {bci}: {branch bytecode} {target}
                str.append(readBranchDest(bci));
            } else if (opcode == Bytecodes.NEW) {
                // {bci}: new {class name}
                int cpi = readCPI(bci);
                ClassConstant cc = (ClassConstant) pool.at(cpi);
                str.append(cc.getName(pool));
            } else if (opcode == Bytecodes.INVOKEDYNAMIC) {
                // {bci}: #{bootstrap method index} -> {name}:{signature}
                int cpi = readCPI(bci);
                InvokeDynamicConstant idc = (InvokeDynamicConstant) pool.at(cpi);
                str.append("#").append(idc.getBootstrapMethodAttrIndex()).append(" -> ").append(idc.getName(pool)).append(":").append(idc.getSignature(pool));
            } else if (Bytecodes.isInvoke(opcode)) {
                // {bci}: invoke{} {class}.{method name}:{method signature}
                int cpi = readCPI(bci);
                MethodRefConstant mrc = (MethodRefConstant) pool.at(cpi);
                str.append(mrc.getHolderKlassName(pool)).append(".").append(mrc.getName(pool)).append(":").append(mrc.getDescriptor(pool));
            } else if (opcode == Bytecodes.TABLESWITCH) {
                // @formatter:off
                // checkstyle: stop
                // {bci}: tableswitch
                // {key1}: {target1}
                // ...
                // {keyN}: {targetN}
                // @formatter:on
                // Checkstyle: resume
                str.append('\n');
                BytecodeTableSwitch helper = BytecodeTableSwitch.INSTANCE;
                int low = helper.lowKey(this, bci);
                int high = helper.highKey(this, bci);
                for (int i = low; i != high + 1; i++) {
                    str.append('\t').append(i).append(": ").append(helper.targetAt(this, bci, i)).append('\n');
                }
                str.append("\tdefault: ").append(helper.defaultTarget(this, bci));
            } else if (opcode == Bytecodes.LOOKUPSWITCH) {
                // @formatter:off
                // checkstyle: stop
                // {bci}: lookupswitch
                // {key1}: {target1}
                // ...
                // {keyN}: {targetN}
                // @formatter:on
                // Checkstyle: resume
                str.append('\n');
                BytecodeLookupSwitch helper = BytecodeLookupSwitch.INSTANCE;
                int low = 0;
                int high = helper.numberOfCases(this, bci) - 1;
                for (int i = low; i <= high; i++) {
                    str.append('\t').append(helper.keyAt(this, bci, i)).append(": ").append(helper.targetAt(this, bci, i));
                }
                str.append("\tdefault: ").append(helper.defaultTarget(this, bci));
            } else if (opcode == Bytecodes.IINC) {
                str.append(" ").append(readLocalIndex(bci)).append(" ").append(readIncrement(bci));
            } else {
                // {bci}: {opcode} {corresponding value}
                if (nextBCI - bci == 2) {
                    str.append(readUByte(bci + 1));
                }
                if (nextBCI - bci == 3) {
                    str.append(readShort(bci));
                }
                if (nextBCI - bci == 5) {
                    str.append(readInt(bci + 1));
                }
            }
            out.println(str.toString());
        }
    } catch (Throwable e) {
        throw EspressoError.unexpected("Exception thrown during bytecode printing, aborting...", e);
    }
}
Also used : ConstantPool(com.oracle.truffle.espresso.classfile.ConstantPool) MethodRefConstant(com.oracle.truffle.espresso.classfile.constantpool.MethodRefConstant) InvokeDynamicConstant(com.oracle.truffle.espresso.classfile.constantpool.InvokeDynamicConstant) ClassConstant(com.oracle.truffle.espresso.classfile.constantpool.ClassConstant)

Aggregations

ConstantPool (com.oracle.truffle.espresso.classfile.ConstantPool)3 NameAndTypeConstant (com.oracle.truffle.espresso.classfile.constantpool.NameAndTypeConstant)2 ParserField (com.oracle.truffle.espresso.impl.ParserField)2 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)2 ParserMethod (com.oracle.truffle.espresso.impl.ParserMethod)2 Matcher (java.util.regex.Matcher)2 ClassfileStream (com.oracle.truffle.espresso.classfile.ClassfileStream)1 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)1 ClassConstant (com.oracle.truffle.espresso.classfile.constantpool.ClassConstant)1 InvokeDynamicConstant (com.oracle.truffle.espresso.classfile.constantpool.InvokeDynamicConstant)1 MethodRefConstant (com.oracle.truffle.espresso.classfile.constantpool.MethodRefConstant)1 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)1 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)1 Field (com.oracle.truffle.espresso.impl.Field)1 Klass (com.oracle.truffle.espresso.impl.Klass)1 Method (com.oracle.truffle.espresso.impl.Method)1 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)1 ArrayList (java.util.ArrayList)1