Search in sources :

Example 6 with Attribute

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

the class ClassWriter method write.

public void write(ClassFile cf) {
    setClassFile(cf);
    if ((options.sysInfo || options.verbose) && !options.compat) {
        if (uri != null) {
            if (uri.getScheme().equals("file"))
                println("Classfile " + uri.getPath());
            else
                println("Classfile " + uri);
        }
        indent(+1);
        if (lastModified != -1) {
            Date lm = new Date(lastModified);
            DateFormat df = DateFormat.getDateInstance();
            if (size > 0) {
                println("Last modified " + df.format(lm) + "; size " + size + " bytes");
            } else {
                println("Last modified " + df.format(lm));
            }
        } else if (size > 0) {
            println("Size " + size + " bytes");
        }
        if (digestName != null && digest != null) {
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) sb.append(String.format("%02x", b));
            println(digestName + " checksum " + sb);
        }
    }
    Attribute sfa = cf.getAttribute(Attribute.SourceFile);
    if (sfa instanceof SourceFile_attribute) {
        println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
    }
    if ((options.sysInfo || options.verbose) && !options.compat) {
        indent(-1);
    }
    String name = getJavaName(classFile);
    AccessFlags flags = cf.access_flags;
    writeModifiers(flags.getClassModifiers());
    if (classFile.isClass())
        print("class ");
    else if (classFile.isInterface())
        print("interface ");
    print(name);
    Signature_attribute sigAttr = getSignature(cf.attributes);
    if (sigAttr == null) {
        // use info from class file header
        if (classFile.isClass() && classFile.super_class != 0) {
            String sn = getJavaSuperclassName(cf);
            if (!sn.equals("java.lang.Object")) {
                print(" extends ");
                print(sn);
            }
        }
        for (int i = 0; i < classFile.interfaces.length; i++) {
            print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ",");
            print(getJavaInterfaceName(classFile, i));
        }
    } else {
        try {
            Type t = sigAttr.getParsedSignature().getType(constant_pool);
            JavaTypePrinter p = new JavaTypePrinter(classFile.isInterface());
            // FieldType and a ClassSignatureType that only contains a superclass type.
            if (t instanceof Type.ClassSigType) {
                print(p.print(t));
            } else if (options.verbose || !t.isObject()) {
                print(" extends ");
                print(p.print(t));
            }
        } catch (ConstantPoolException e) {
            print(report(e));
        }
    }
    if (options.verbose) {
        println();
        indent(+1);
        attrWriter.write(cf, cf.attributes, constant_pool);
        println("minor version: " + cf.minor_version);
        println("major version: " + cf.major_version);
        if (!options.compat)
            writeList("flags: ", flags.getClassFlags(), NEWLINE);
        indent(-1);
        constantWriter.writeConstantPool();
    } else {
        print(" ");
    }
    println("{");
    indent(+1);
    writeFields();
    writeMethods();
    indent(-1);
    println("}");
}
Also used : Attribute(com.sun.tools.classfile.Attribute) Signature_attribute(com.sun.tools.classfile.Signature_attribute) ClassSigType(com.sun.tools.classfile.Type.ClassSigType) Date(java.util.Date) SourceFile_attribute(com.sun.tools.classfile.SourceFile_attribute) Type(com.sun.tools.classfile.Type) ArrayType(com.sun.tools.classfile.Type.ArrayType) ClassType(com.sun.tools.classfile.Type.ClassType) MethodType(com.sun.tools.classfile.Type.MethodType) WildcardType(com.sun.tools.classfile.Type.WildcardType) TypeParamType(com.sun.tools.classfile.Type.TypeParamType) SimpleType(com.sun.tools.classfile.Type.SimpleType) ClassSigType(com.sun.tools.classfile.Type.ClassSigType) DateFormat(java.text.DateFormat) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 7 with Attribute

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

the class ClassWriter method writeMethod.

protected void writeMethod(Method m) {
    if (!options.checkAccess(m.access_flags))
        return;
    method = m;
    AccessFlags flags = m.access_flags;
    Descriptor d;
    Type.MethodType methodType;
    List<? extends Type> methodExceptions;
    Signature_attribute sigAttr = getSignature(m.attributes);
    if (sigAttr == null) {
        d = m.descriptor;
        methodType = null;
        methodExceptions = null;
    } else {
        Signature methodSig = sigAttr.getParsedSignature();
        d = methodSig;
        try {
            methodType = (Type.MethodType) methodSig.getType(constant_pool);
            methodExceptions = methodType.throwsTypes;
            if (methodExceptions != null && methodExceptions.isEmpty())
                methodExceptions = null;
        } catch (ConstantPoolException e) {
            // report error?
            // fall back on standard descriptor
            methodType = null;
            methodExceptions = null;
        }
    }
    writeModifiers(flags.getMethodModifiers());
    if (methodType != null) {
        writeListIfNotEmpty("<", methodType.typeParamTypes, "> ");
    }
    if (getName(m).equals("<init>")) {
        print(getJavaName(classFile));
        print(getJavaParameterTypes(d, flags));
    } else if (getName(m).equals("<clinit>")) {
        print("{}");
    } else {
        print(getJavaReturnType(d));
        print(" ");
        print(getName(m));
        print(getJavaParameterTypes(d, flags));
    }
    Attribute e_attr = m.attributes.get(Attribute.Exceptions);
    if (e_attr != null) {
        // if there are generic exceptions, there must be erased exceptions
        if (e_attr instanceof Exceptions_attribute) {
            Exceptions_attribute exceptions = (Exceptions_attribute) e_attr;
            print(" throws ");
            if (methodExceptions != null) {
                // use generic list if available
                writeList("", methodExceptions, "");
            } else {
                for (int i = 0; i < exceptions.number_of_exceptions; i++) {
                    if (i > 0)
                        print(", ");
                    print(getJavaException(exceptions, i));
                }
            }
        } else {
            report("Unexpected or invalid value for Exceptions attribute");
        }
    }
    println(";");
    indent(+1);
    if (options.showInternalSignatures) {
        println("Signature: " + getValue(m.descriptor));
    }
    if (options.verbose && !options.compat) {
        writeList("flags: ", flags.getMethodFlags(), NEWLINE);
    }
    Code_attribute code = null;
    Attribute c_attr = m.attributes.get(Attribute.Code);
    if (c_attr != null) {
        if (c_attr instanceof Code_attribute)
            code = (Code_attribute) c_attr;
        else
            report("Unexpected or invalid value for Code attribute");
    }
    if (options.showDisassembled && !options.showAllAttrs) {
        if (code != null) {
            println("Code:");
            codeWriter.writeInstrs(code);
            codeWriter.writeExceptionTable(code);
        }
    }
    if (options.showLineAndLocalVariableTables) {
        if (code != null) {
            attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool);
            attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool);
        }
    }
    if (options.showAllAttrs) {
        Attribute[] attrs = m.attributes.attrs;
        for (Attribute attr : attrs) attrWriter.write(m, attr, constant_pool);
    }
    indent(-1);
    // set pendingNewline to write a newline before the next method (if any)
    // if a separator is desired
    setPendingNewline(options.showDisassembled || options.showAllAttrs || options.showInternalSignatures || options.showLineAndLocalVariableTables || options.verbose);
}
Also used : MethodType(com.sun.tools.classfile.Type.MethodType) Attribute(com.sun.tools.classfile.Attribute) Signature_attribute(com.sun.tools.classfile.Signature_attribute) Code_attribute(com.sun.tools.classfile.Code_attribute) Exceptions_attribute(com.sun.tools.classfile.Exceptions_attribute) Type(com.sun.tools.classfile.Type) ArrayType(com.sun.tools.classfile.Type.ArrayType) ClassType(com.sun.tools.classfile.Type.ClassType) MethodType(com.sun.tools.classfile.Type.MethodType) WildcardType(com.sun.tools.classfile.Type.WildcardType) TypeParamType(com.sun.tools.classfile.Type.TypeParamType) SimpleType(com.sun.tools.classfile.Type.SimpleType) ClassSigType(com.sun.tools.classfile.Type.ClassSigType) Signature(com.sun.tools.classfile.Signature) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Descriptor(com.sun.tools.classfile.Descriptor) AccessFlags(com.sun.tools.classfile.AccessFlags)

Aggregations

Attribute (com.sun.tools.classfile.Attribute)7 AccessFlags (com.sun.tools.classfile.AccessFlags)3 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)3 Signature_attribute (com.sun.tools.classfile.Signature_attribute)3 Type (com.sun.tools.classfile.Type)3 ArrayType (com.sun.tools.classfile.Type.ArrayType)3 ClassSigType (com.sun.tools.classfile.Type.ClassSigType)3 ClassType (com.sun.tools.classfile.Type.ClassType)3 MethodType (com.sun.tools.classfile.Type.MethodType)3 SimpleType (com.sun.tools.classfile.Type.SimpleType)3 TypeParamType (com.sun.tools.classfile.Type.TypeParamType)3 WildcardType (com.sun.tools.classfile.Type.WildcardType)3 DefaultAttribute (com.sun.tools.classfile.DefaultAttribute)2 Code_attribute (com.sun.tools.classfile.Code_attribute)1 ConstantValue_attribute (com.sun.tools.classfile.ConstantValue_attribute)1 Descriptor (com.sun.tools.classfile.Descriptor)1 Exceptions_attribute (com.sun.tools.classfile.Exceptions_attribute)1 LineNumberTable_attribute (com.sun.tools.classfile.LineNumberTable_attribute)1 Method (com.sun.tools.classfile.Method)1 Signature (com.sun.tools.classfile.Signature)1