Search in sources :

Example 1 with Code_attribute

use of com.sun.tools.classfile.Code_attribute in project ceylon-compiler by ceylon.

the class TestCP method verifyMethodHandleInvocationDescriptors.

void verifyMethodHandleInvocationDescriptors(File f) {
    System.err.println("verify: " + f);
    try {
        int count = 0;
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
                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");
        }
        int instr_count = 0;
        int cp_entry = -1;
        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                instr_count++;
                if (cp_entry == -1) {
                    cp_entry = i.getUnsignedShort(1);
                } else if (cp_entry != i.getUnsignedShort(1)) {
                    throw new Error("Unexpected CP entry in polymorphic signature call");
                }
                CONSTANT_Methodref_info methRef = (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                if (!type.equals(PS_TYPE)) {
                    throw new Error("Unexpected type in polymorphic signature call: " + type);
                }
            }
        }
        if (instr_count != PS_CALLS_COUNT) {
            throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) Method(com.sun.tools.classfile.Method) Instruction(com.sun.tools.classfile.Instruction)

Example 2 with Code_attribute

use of com.sun.tools.classfile.Code_attribute in project ceylon-compiler by ceylon.

the class T7005371 method verifyLocalVariableTypeTableAttr.

void verifyLocalVariableTypeTableAttr(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Missing method: " + TEST_METHOD_NAME);
        }
        Code_attribute code = (Code_attribute) testMethod.attributes.get(Attribute.Code);
        if (code == null) {
            throw new Error("Missing Code attribute for method: " + TEST_METHOD_NAME);
        }
        LocalVariableTypeTable_attribute lvt_table = (LocalVariableTypeTable_attribute) code.attributes.get(Attribute.LocalVariableTypeTable);
        if (lvt_table == null) {
            throw new Error("Missing LocalVariableTypeTable attribute for method: " + TEST_METHOD_NAME);
        }
        if (lvt_table.local_variable_table_length != LVT_LENGTH) {
            throw new Error("LocalVariableTypeTable has wrong size" + "\nfound: " + lvt_table.local_variable_table_length + "\nrequired: " + LVT_LENGTH);
        }
        String sig = cf.constant_pool.getUTF8Value(lvt_table.local_variable_table[0].signature_index);
        if (sig == null || !sig.equals(LVT_SIG_TYPE)) {
            throw new Error("LocalVariableTypeTable has wrong signature" + "\nfound: " + sig + "\nrequired: " + LVT_SIG_TYPE);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) LocalVariableTypeTable_attribute(com.sun.tools.classfile.LocalVariableTypeTable_attribute) Method(com.sun.tools.classfile.Method)

Example 3 with Code_attribute

use of com.sun.tools.classfile.Code_attribute in project jdk8u_jdk by JetBrains.

the class AnnotationsElementVisitor method visitCode.

@Override
public Element visitCode(Code_attribute c, Element p) {
    Element e = null;
    e = new Element(x.getCpString(c.attribute_name_index), "stack", "" + c.max_stack, "local", "" + c.max_locals);
    e.add(instructions(e, c));
    for (Code_attribute.Exception_data edata : c.exception_table) {
        e.add(new Element("Handler", "start", "" + edata.start_pc, "end", "" + edata.end_pc, "catch", "" + edata.handler_pc, "class", x.getCpString(edata.catch_type)));
    }
    this.x.readAttributesFor(cf, c.attributes, e);
    e.trimToSize();
    p.add(e);
    return null;
}
Also used : Element(xmlkit.XMLKit.Element) Code_attribute(com.sun.tools.classfile.Code_attribute)

Example 4 with Code_attribute

use of com.sun.tools.classfile.Code_attribute in project ceylon-compiler by ceylon.

the class T6199075 method verifyBytecode.

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);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) Method(com.sun.tools.classfile.Method) Instruction(com.sun.tools.classfile.Instruction) File(java.io.File) ClassFile(com.sun.tools.classfile.ClassFile)

Example 5 with Code_attribute

use of com.sun.tools.classfile.Code_attribute in project ceylon-compiler by ceylon.

the class T7042566 method verifyBytecode.

void verifyBytecode(VarargsMethod selected, JavaSource source) {
    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();
                String sig = selected.parameterTypes.bytecodeSigStr;
                if (!type.contains(sig)) {
                    throw new Error("Unexpected type method call: " + type + "" + "\nfound: " + sig + "\n" + source.getCharContent(true));
                }
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + compiledTest + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) Method(com.sun.tools.classfile.Method) Instruction(com.sun.tools.classfile.Instruction) File(java.io.File) ClassFile(com.sun.tools.classfile.ClassFile)

Aggregations

Code_attribute (com.sun.tools.classfile.Code_attribute)7 ClassFile (com.sun.tools.classfile.ClassFile)5 Method (com.sun.tools.classfile.Method)5 Instruction (com.sun.tools.classfile.Instruction)3 File (java.io.File)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)1 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)1 Descriptor (com.sun.tools.classfile.Descriptor)1 Exceptions_attribute (com.sun.tools.classfile.Exceptions_attribute)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 Signature (com.sun.tools.classfile.Signature)1 Signature_attribute (com.sun.tools.classfile.Signature_attribute)1 Type (com.sun.tools.classfile.Type)1 ArrayType (com.sun.tools.classfile.Type.ArrayType)1 ClassSigType (com.sun.tools.classfile.Type.ClassSigType)1 ClassType (com.sun.tools.classfile.Type.ClassType)1 MethodType (com.sun.tools.classfile.Type.MethodType)1 SimpleType (com.sun.tools.classfile.Type.SimpleType)1