Java Code Examples for com.sun.tools.classfile.Instruction

The following examples show how to use com.sun.tools.classfile.Instruction. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
void verifyDefaultBody(String classFile) {
    String workDir = System.getProperty("test.classes");
    File file = new File(workDir, classFile);
    try {
        final ClassFile cf = ClassFile.read(file);
        for (Method m : cf.methods) {
            Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Instruction instr : codeAttr.getInstructions()) {
                if (instr.getOpcode() == Opcode.INVOKESPECIAL) {
                    int pc_index = instr.getShort(1);
                    CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index);
                    String className = ref.getClassName();
                    if (className.equals("BaseInterface"))
                        throw new IllegalStateException("Must not directly refer to TestedInterface");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + file +": " + e);
    }
}
 
Example 2
Source Project: openjdk-8   Source File: SourceWriter.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void writeDetails(Instruction instr) {
    String indent = space(40); // could get from Options?
    Set<Integer> lines = lineMap.get(instr.getPC());
    if (lines != null) {
        for (int line: lines) {
            print(indent);
            print(String.format(" %4d ", line));
            if (line < sourceLines.length)
                print(sourceLines[line]);
            println();
            int nextLine = nextLine(line);
            for (int i = line + 1; i < nextLine; i++) {
                print(indent);
                print(String.format("(%4d)", i));
                if (i < sourceLines.length)
                    print(sourceLines[i]);
                println();
            }
        }
    }
}
 
Example 3
void verifyDefaultBody(String classFile) {
    String workDir = System.getProperty("test.classes");
    File file = new File(workDir, classFile);
    try {
        final ClassFile cf = ClassFile.read(file);
        for (Method m : cf.methods) {
            Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
            for (Instruction instr : codeAttr.getInstructions()) {
                if (instr.getOpcode() == Opcode.INVOKESPECIAL) {
                    int pc_index = instr.getShort(1);
                    CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index);
                    String className = ref.getClassName();
                    if (className.equals("BaseInterface"))
                        throw new IllegalStateException("Must not directly refer to TestedInterface");
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + file +": " + e);
    }
}
 
Example 4
Source Project: jdk8u-jdk   Source File: LambdaAsm.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void checkMethod(String cname, String mname, ConstantPool cp,
        Code_attribute code) throws ConstantPool.InvalidIndex {
    for (Instruction i : code.getInstructions()) {
        String iname = i.getMnemonic();
        if ("invokespecial".equals(iname)
                || "invokestatic".equals(iname)) {
            int idx = i.getByte(2);
            System.out.println("Verifying " + cname + ":" + mname +
                    " instruction:" + iname + " index @" + idx);
            CPInfo cpinfo = cp.get(idx);
            if (cpinfo instanceof ConstantPool.CONSTANT_Methodref_info) {
                throw new RuntimeException("unexpected CP type expected "
                        + "InterfaceMethodRef, got MethodRef, " + cname
                        + ", " + mname);
            }
        }
    }
}
 
Example 5
Source Project: hottub   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitTableSwitch(Instruction i, int i1, int i2, int i3,
                                int[] ints, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k : ints) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (k + i2));
        c.setAttr("lab", "" + (pc + k));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}
 
Example 6
Source Project: dragonwell8_jdk   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitNoOperands(Instruction i, Void p) {
    Opcode o = i.getOpcode();
    Element e = new Element(i.getMnemonic());
    if (o.opcode > 0xab && o.opcode <= 0xb1) {
        e.setAttr("pc", "" + i.getPC());
    }
    return e;
}
 
Example 7
Source Project: jdk8u-dev-jdk   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitLocalAndValue(Instruction i, int i1, int i2, Void p) {
    Element ie = new Element(i.getMnemonic());
    ie.setAttr("loc", "" + i1);
    ie.setAttr("num", "" + i2);
    return ie;
}
 
Example 8
@Override
void writeDetails(Instruction instr) {
    String indent = space(2); // get from Options?
    int pc = instr.getPC();
    List<Note> notes = pcMap.get(pc);
    if (notes != null) {
        for (Note n: notes) {
            print(indent);
            print("@");
            annotationWriter.write(n.anno, false, true);
            print(", ");
            println(StringUtils.toLowerCase(n.kind.toString()));
        }
    }
}
 
Example 9
Source Project: dragonwell8_jdk   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitTableSwitch(Instruction i, int i1, int i2, int i3,
                                int[] ints, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k : ints) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (k + i2));
        c.setAttr("lab", "" + (pc + k));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}
 
Example 10
Source Project: dragonwell8_jdk   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitUnknown(Instruction i, Void p) {
    Element e = new Element(i.getMnemonic());
    e.setAttr("pc", "" + i.getPC());
    e.setAttr("opcode", "" + i.getOpcode().opcode);
    return e;
}
 
Example 11
Source Project: openjdk-jdk9   Source File: CodeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Void visitLookupSwitch(Instruction instr,
        int default_, int npairs, int[] matches, int[] offsets, Integer indent) {
    int pc = instr.getPC();
    print("{ // " + npairs);
    indent(indent);
    for (int i = 0; i < npairs; i++) {
        print(String.format("%n%12d: %d", matches[i], (pc + offsets[i])));
    }
    print("\n     default: " + (pc + default_) + "\n}");
    indent(-indent);
    return null;
}
 
Example 12
Source Project: hottub   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitUnknown(Instruction i, Void p) {
    Element e = new Element(i.getMnemonic());
    e.setAttr("pc", "" + i.getPC());
    e.setAttr("opcode", "" + i.getOpcode().opcode);
    return e;
}
 
Example 13
public void writeInstr(Instruction instr) {
    print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));
    // compute the number of indentations for the body of multi-line instructions
    // This is 6 (the width of "%4d: "), divided by the width of each indentation level,
    // and rounded up to the next integer.
    int indentWidth = options.indentWidth;
    int indent = (6 + indentWidth - 1) / indentWidth;
    instr.accept(instructionPrinter, indent);
    println();
}
 
Example 14
Source Project: openjdk-8   Source File: CodeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void writeInstr(Instruction instr) {
    print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));
    // compute the number of indentations for the body of multi-line instructions
    // This is 6 (the width of "%4d: "), divided by the width of each indentation level,
    // and rounded up to the next integer.
    int indentWidth = options.indentWidth;
    int indent = (6 + indentWidth - 1) / indentWidth;
    instr.accept(instructionPrinter, indent);
    println();
}
 
Example 15
Source Project: hottub   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitNoOperands(Instruction i, Void p) {
    Opcode o = i.getOpcode();
    Element e = new Element(i.getMnemonic());
    if (o.opcode > 0xab && o.opcode <= 0xb1) {
        e.setAttr("pc", "" + i.getPC());
    }
    return e;
}
 
Example 16
Source Project: jdk8u60   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitNoOperands(Instruction i, Void p) {
    Opcode o = i.getOpcode();
    Element e = new Element(i.getMnemonic());
    if (o.opcode > 0xab && o.opcode <= 0xb1) {
        e.setAttr("pc", "" + i.getPC());
    }
    return e;
}
 
Example 17
Source Project: TencentKona-8   Source File: CodeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Integer indent) {
    print("#" + index + ",  " + value);
    tab();
    print("// ");
    printConstant(index);
    return null;
}
 
Example 18
Source Project: openjdk-8-source   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitLookupSwitch(Instruction i, int i1, int i2, int[] ints,
                                 int[] ints1, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k = 0 ; k < i2 ; k++) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (ints[k]));
        c.setAttr("lab", "" + (pc + ints1[k]));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}
 
Example 19
Source Project: jdk8u-jdk   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitNoOperands(Instruction i, Void p) {
    Opcode o = i.getOpcode();
    Element e = new Element(i.getMnemonic());
    if (o.opcode > 0xab && o.opcode <= 0xb1) {
        e.setAttr("pc", "" + i.getPC());
    }
    return e;
}
 
Example 20
Source Project: TencentKona-8   Source File: CodeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Void visitTableSwitch(Instruction instr,
        int default_, int low, int high, int[] offsets, Integer indent) {
    int pc = instr.getPC();
    print("{ // " + low + " to " + high);
    indent(indent);
    for (int i = 0; i < offsets.length; i++) {
        print(String.format("%n%12d: %d", (low + i), (pc + offsets[i])));
    }
    print("\n     default: " + (pc + default_) + "\n}");
    indent(-indent);
    return null;
}
 
Example 21
Source Project: openjdk-8-source   Source File: T6199075.java    License: GNU General Public License v2.0 5 votes vote down vote up
void verifyBytecode(VarargsMethod selected) {
    bytecodeCheckCount++;
    File compiledTest = new File("Test.class");
    try {
        ClassFile cf = ClassFile.read(compiledTest);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals("test")) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Test method not found");
        }
        Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
        if (testMethod == null) {
            throw new Error("Code attribute for test() method not found");
        }

        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                int cp_entry = i.getUnsignedShort(1);
                CONSTANT_Methodref_info methRef =
                        (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                if (!type.contains(selected.varargsElement.bytecodeString)) {
                    throw new Error("Unexpected type method call: " + type);
                }
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest +": " + e);
    }
}
 
Example 22
@Override
public Element visitLookupSwitch(Instruction i, int i1, int i2, int[] ints,
                                 int[] ints1, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k = 0 ; k < i2 ; k++) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (ints[k]));
        c.setAttr("lab", "" + (pc + ints1[k]));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}
 
Example 23
Source Project: hottub   Source File: CodeWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void writeInstr(Instruction instr) {
    print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));
    // compute the number of indentations for the body of multi-line instructions
    // This is 6 (the width of "%4d: "), divided by the width of each indentation level,
    // and rounded up to the next integer.
    int indentWidth = options.indentWidth;
    int indent = (6 + indentWidth - 1) / indentWidth;
    instr.accept(instructionPrinter, indent);
    println();
}
 
Example 24
Source Project: jdk8u60   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Element instructions(Element code, Code_attribute c) {
    Element ielement = new Element("Instructions");
    for (Instruction ins : c.getInstructions()) {
        ielement.add(iv.visit(ins));
    }
    ielement.trimToSize();
    return ielement;
}
 
Example 25
Source Project: openjdk-8-source   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitTableSwitch(Instruction i, int i1, int i2, int i3,
                                int[] ints, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k : ints) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (k + i2));
        c.setAttr("lab", "" + (pc + k));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}
 
Example 26
Source Project: openjdk-jdk9   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitUnknown(Instruction i, Void p) {
    Element e = new Element(i.getMnemonic());
    e.setAttr("pc", "" + i.getPC());
    e.setAttr("opcode", "" + i.getOpcode().opcode);
    return e;
}
 
Example 27
Source Project: openjdk-jdk8u   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Element instructions(Element code, Code_attribute c) {
    Element ielement = new Element("Instructions");
    for (Instruction ins : c.getInstructions()) {
        ielement.add(iv.visit(ins));
    }
    ielement.trimToSize();
    return ielement;
}
 
Example 28
Source Project: TencentKona-8   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitLocalAndValue(Instruction i, int i1, int i2, Void p) {
    Element ie = new Element(i.getMnemonic());
    ie.setAttr("loc", "" + i1);
    ie.setAttr("num", "" + i2);
    return ie;
}
 
Example 29
@Override
void writeDetails(Instruction instr) {
    String indent = space(2); // get from Options?
    int pc = instr.getPC();
    List<Note> notes = pcMap.get(pc);
    if (notes != null) {
        for (Note n: notes) {
            print(indent);
            print("@");
            annotationWriter.write(n.anno, false, true);
            print(", ");
            println(n.kind.toString().toLowerCase());
        }
    }
}
 
Example 30
Source Project: TencentKona-8   Source File: ClassReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Element visitTableSwitch(Instruction i, int i1, int i2, int i3,
                                int[] ints, Void p) {
    Element ie = new Element(i.getMnemonic());
    int pc = i.getPC();
    ie.setAttr("lab", "" + (pc + i1));
    for (int k : ints) {
        Element c = new Element("Case");
        c.setAttr("num", "" + (k + i2));
        c.setAttr("lab", "" + (pc + k));
        c.trimToSize();
        ie.add(c);
    }
    return ie;
}