Search in sources :

Example 11 with ClassFile

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

the class ConstantWriter method write.

protected void write(int cpx) {
    ClassFile classFile = classWriter.getClassFile();
    if (cpx == 0) {
        print("#0");
        return;
    }
    CPInfo cpInfo;
    try {
        cpInfo = classFile.constant_pool.get(cpx);
    } catch (ConstantPoolException e) {
        print("#" + cpx);
        return;
    }
    int tag = cpInfo.getTag();
    switch(tag) {
        case CONSTANT_Methodref:
        case CONSTANT_InterfaceMethodref:
        case CONSTANT_Fieldref:
            // simplify references within this class
            CPRefInfo ref = (CPRefInfo) cpInfo;
            try {
                if (ref.class_index == classFile.this_class)
                    cpInfo = classFile.constant_pool.get(ref.name_and_type_index);
            } catch (ConstantPool.InvalidIndex e) {
            // ignore, for now
            }
    }
    print(tagName(tag) + " " + stringValue(cpInfo));
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException)

Example 12 with ClassFile

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

the class T4241573 method verifySourceFileAttribute.

/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
        String found = sfa.getSourceFile(cf.constant_pool);
        String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
        if (!expect.equals(found)) {
            error("bad value found: " + found + ", expected: " + expect);
        }
    } catch (Exception e) {
        error("error reading " + f + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) SourceFile_attribute(com.sun.tools.classfile.SourceFile_attribute)

Example 13 with ClassFile

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

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

the class FindNativeFiles method isNativeClass.

boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException {
    String name = entry.getName();
    if (name.startsWith("META-INF") || !name.endsWith(".class"))
        return false;
    //String className = name.substring(0, name.length() - 6).replace("/", ".");
    //System.err.println("check " + className);
    InputStream in = jar.getInputStream(entry);
    ClassFile cf = ClassFile.read(in);
    in.close();
    for (int i = 0; i < cf.methods.length; i++) {
        Method m = cf.methods[i];
        if (m.access_flags.is(AccessFlags.ACC_NATIVE)) {
            // System.err.println(className);
            return true;
        }
    }
    return false;
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) InputStream(java.io.InputStream) Method(com.sun.tools.classfile.Method)

Example 15 with ClassFile

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

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