Search in sources :

Example 1 with SourceFile_attribute

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

the class SourceWriter method readSource.

private String readSource(ClassFile cf) {
    if (fileManager == null)
        return null;
    Location location;
    if (fileManager.hasLocation((StandardLocation.SOURCE_PATH)))
        location = StandardLocation.SOURCE_PATH;
    else
        location = StandardLocation.CLASS_PATH;
    // InnerClasses and EnclosingMethod attributes.
    try {
        String className = cf.getName();
        SourceFile_attribute sf = (SourceFile_attribute) cf.attributes.get(Attribute.SourceFile);
        if (sf == null) {
            report(messages.getMessage("err.no.SourceFile.attribute"));
            return null;
        }
        String sourceFile = sf.getSourceFile(cf.constant_pool);
        String fileBase = sourceFile.endsWith(".java") ? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile;
        int sep = className.lastIndexOf("/");
        String pkgName = (sep == -1 ? "" : className.substring(0, sep + 1));
        String topClassName = (pkgName + fileBase).replace('/', '.');
        JavaFileObject fo = fileManager.getJavaFileForInput(location, topClassName, JavaFileObject.Kind.SOURCE);
        if (fo == null) {
            report(messages.getMessage("err.source.file.not.found"));
            return null;
        }
        return fo.getCharContent(true).toString();
    } catch (ConstantPoolException e) {
        report(e);
        return null;
    } catch (IOException e) {
        report(e.getLocalizedMessage());
        return null;
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) IOException(java.io.IOException) StandardLocation(javax.tools.StandardLocation) Location(javax.tools.JavaFileManager.Location) SourceFile_attribute(com.sun.tools.classfile.SourceFile_attribute)

Example 2 with SourceFile_attribute

use of com.sun.tools.classfile.SourceFile_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 3 with SourceFile_attribute

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

Aggregations

SourceFile_attribute (com.sun.tools.classfile.SourceFile_attribute)3 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 ClassFile (com.sun.tools.classfile.ClassFile)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 TypeParamType (com.sun.tools.classfile.Type.TypeParamType)1 WildcardType (com.sun.tools.classfile.Type.WildcardType)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 Location (javax.tools.JavaFileManager.Location)1 JavaFileObject (javax.tools.JavaFileObject)1 StandardLocation (javax.tools.StandardLocation)1