Search in sources :

Example 16 with Method

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

the class CodeWriter method writeVerboseHeader.

public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
    Method method = classWriter.getMethod();
    String argCount;
    try {
        int n = method.descriptor.getParameterCount(constant_pool);
        if (!method.access_flags.is(AccessFlags.ACC_STATIC))
            // for 'this'
            ++n;
        argCount = Integer.toString(n);
    } catch (ConstantPoolException e) {
        argCount = report(e);
    } catch (DescriptorException e) {
        argCount = report(e);
    }
    println("stack=" + attr.max_stack + ", locals=" + attr.max_locals + ", args_size=" + argCount);
}
Also used : DescriptorException(com.sun.tools.classfile.DescriptorException) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Method(com.sun.tools.classfile.Method)

Example 17 with Method

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

the class StackMapWriter method setStackMap.

void setStackMap(StackMapTable_attribute attr) {
    if (attr == null) {
        map = null;
        return;
    }
    Method m = classWriter.getMethod();
    Descriptor d = m.descriptor;
    String[] args;
    try {
        ConstantPool cp = classWriter.getClassFile().constant_pool;
        String argString = d.getParameterTypes(cp);
        args = argString.substring(1, argString.length() - 1).split("[, ]+");
    } catch (ConstantPoolException e) {
        return;
    } catch (InvalidDescriptor e) {
        return;
    }
    boolean isStatic = m.access_flags.is(AccessFlags.ACC_STATIC);
    verification_type_info[] initialLocals = new verification_type_info[(isStatic ? 0 : 1) + args.length];
    if (!isStatic)
        initialLocals[0] = new CustomVerificationTypeInfo("this");
    for (int i = 0; i < args.length; i++) {
        initialLocals[(isStatic ? 0 : 1) + i] = new CustomVerificationTypeInfo(args[i].replace(".", "/"));
    }
    map = new HashMap<Integer, StackMap>();
    StackMapBuilder builder = new StackMapBuilder();
    // using -1 as the pc for the initial frame effectively compensates for
    // the difference in behavior for the first stack map frame (where the
    // pc offset is just offset_delta) compared to subsequent frames (where
    // the pc offset is always offset_delta+1).
    int pc = -1;
    map.put(pc, new StackMap(initialLocals, empty));
    for (int i = 0; i < attr.entries.length; i++) pc = attr.entries[i].accept(builder, pc);
}
Also used : Method(com.sun.tools.classfile.Method) StackMapTable_attribute.verification_type_info(com.sun.tools.classfile.StackMapTable_attribute.verification_type_info) ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Example 18 with Method

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

the class Pos05 method verifyMulticatchExceptionRanges.

void verifyMulticatchExceptionRanges(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");
        }
        Exception_data firstExceptionTable = null;
        for (int i = 0; i < ea.exception_table_langth; i++) {
            if (firstExceptionTable == null) {
                firstExceptionTable = ea.exception_table[i];
            }
            if (ea.exception_table[i].handler_pc != firstExceptionTable.handler_pc || ea.exception_table[i].start_pc != firstExceptionTable.start_pc || ea.exception_table[i].end_pc != firstExceptionTable.end_pc) {
                throw new Error("Multiple overlapping catch clause found in generated code");
            }
            count++;
        }
        if (count != TYPES_IN_MULTICATCH) {
            throw new Error("Wrong number of exception data found: " + 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) Exception_data(com.sun.tools.classfile.Code_attribute.Exception_data)

Example 19 with Method

use of com.sun.tools.classfile.Method 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 20 with Method

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

Method (com.sun.tools.classfile.Method)21 ClassFile (com.sun.tools.classfile.ClassFile)15 Code_attribute (com.sun.tools.classfile.Code_attribute)10 Instruction (com.sun.tools.classfile.Instruction)6 InputStream (java.io.InputStream)5 File (java.io.File)4 Field (com.sun.tools.classfile.Field)3 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)2 ConstantPool (com.sun.tools.classfile.ConstantPool)2 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)2 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)2 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Descriptor (com.sun.tools.classfile.Descriptor)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 DescriptorException (com.sun.tools.classfile.DescriptorException)1