Search in sources :

Example 16 with ClassFile

use of com.sun.tools.classfile.ClassFile 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

ClassFile (com.sun.tools.classfile.ClassFile)16 Method (com.sun.tools.classfile.Method)8 Code_attribute (com.sun.tools.classfile.Code_attribute)5 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)3 Instruction (com.sun.tools.classfile.Instruction)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ConstantPool (com.sun.tools.classfile.ConstantPool)2 Path (java.nio.file.Path)2 JavacTask (com.sun.source.util.JavacTask)1 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)1 Dependency (com.sun.tools.classfile.Dependency)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 Field (com.sun.tools.classfile.Field)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 SourceFile_attribute (com.sun.tools.classfile.SourceFile_attribute)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1