Search in sources :

Example 1 with AccessFlags

use of com.sun.tools.classfile.AccessFlags in project jdk8u_jdk by JetBrains.

the class AnnotationsElementVisitor method readClass.

private void readClass(ClassFile c) throws IOException, ConstantPoolException, InvalidDescriptor {
    klass = new Element("Class");
    cfile.add(klass);
    String thisk = c.getName();
    klass.setAttr("name", thisk);
    AccessFlags af = new AccessFlags(c.access_flags.flags);
    klass.setAttr("flags", flagString(af, klass));
    if (!"java/lang/Object".equals(thisk)) {
        klass.setAttr("super", c.getSuperclassName());
    }
    for (int i : c.interfaces) {
        klass.add(new Element("Interface", "name", getCpString(i)));
    }
    readFields(c, klass);
    readMethods(c, klass);
    readAttributesFor(c, c.attributes, klass);
    klass.trimToSize();
}
Also used : Element(xmlkit.XMLKit.Element) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 2 with AccessFlags

use of com.sun.tools.classfile.AccessFlags in project jdk8u_jdk by JetBrains.

the class RemoveMethods method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.err.println("Usage: java RemoveMethods classfile output [method...]");
        System.exit(-1);
    }
    // class file to read
    Path input = Paths.get(args[0]);
    // class file to write, if directory then use the name of the input
    Path output = Paths.get(args[1]);
    if (Files.isDirectory(output))
        output = output.resolve(input.getFileName());
    // the methods to remove
    Set<String> methodsToRemove = new HashSet<>();
    int i = 2;
    while (i < args.length) methodsToRemove.add(args[i++]);
    // read class file
    ClassFile cf;
    try (InputStream in = Files.newInputStream(input)) {
        cf = ClassFile.read(in);
    }
    final int magic = cf.magic;
    final int major_version = cf.major_version;
    final int minor_version = cf.minor_version;
    final ConstantPool cp = cf.constant_pool;
    final AccessFlags access_flags = cf.access_flags;
    final int this_class = cf.this_class;
    final int super_class = cf.super_class;
    final int[] interfaces = cf.interfaces;
    final Field[] fields = cf.fields;
    final Attributes class_attrs = cf.attributes;
    // remove the requested methods, no signature check at this time
    Method[] methods = cf.methods;
    i = 0;
    while (i < methods.length) {
        Method m = methods[i];
        String name = m.getName(cp);
        if (methodsToRemove.contains(name)) {
            int len = methods.length;
            Method[] newMethods = new Method[len - 1];
            if (i > 0)
                System.arraycopy(methods, 0, newMethods, 0, i);
            int after = methods.length - i - 1;
            if (after > 0)
                System.arraycopy(methods, i + 1, newMethods, i, after);
            methods = newMethods;
            String paramTypes = m.descriptor.getParameterTypes(cp);
            System.out.format("Removed method %s%s from %s%n", name, paramTypes, cf.getName());
            continue;
        }
        i++;
    }
    // TBD, prune constant pool of entries that are no longer referenced
    // re-write class file
    cf = new ClassFile(magic, minor_version, major_version, cp, access_flags, this_class, super_class, interfaces, fields, methods, class_attrs);
    try (OutputStream out = Files.newOutputStream(output)) {
        new ClassWriter().write(cf, out);
    }
}
Also used : Path(java.nio.file.Path) ClassFile(com.sun.tools.classfile.ClassFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Attributes(com.sun.tools.classfile.Attributes) Method(com.sun.tools.classfile.Method) ClassWriter(com.sun.tools.classfile.ClassWriter) Field(com.sun.tools.classfile.Field) ConstantPool(com.sun.tools.classfile.ConstantPool) HashSet(java.util.HashSet) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 3 with AccessFlags

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

the class ClassWriter method writeField.

protected void writeField(Field f) {
    if (!options.checkAccess(f.access_flags))
        return;
    AccessFlags flags = f.access_flags;
    writeModifiers(flags.getFieldModifiers());
    Signature_attribute sigAttr = getSignature(f.attributes);
    if (sigAttr == null)
        print(getJavaFieldType(f.descriptor));
    else {
        try {
            Type t = sigAttr.getParsedSignature().getType(constant_pool);
            print(getJavaName(t.toString()));
        } catch (ConstantPoolException e) {
            // report error?
            // fall back on non-generic descriptor
            print(getJavaFieldType(f.descriptor));
        }
    }
    print(" ");
    print(getFieldName(f));
    if (options.showConstants && !options.compat) {
        // BUG 4111861 print static final field contents
        Attribute a = f.attributes.get(Attribute.ConstantValue);
        if (a instanceof ConstantValue_attribute) {
            print(" = ");
            ConstantValue_attribute cv = (ConstantValue_attribute) a;
            print(getConstantValue(f.descriptor, cv.constantvalue_index));
        }
    }
    print(";");
    println();
    indent(+1);
    if (options.showInternalSignatures)
        println("Signature: " + getValue(f.descriptor));
    if (options.verbose && !options.compat)
        writeList("flags: ", flags.getFieldFlags(), NEWLINE);
    if (options.showAllAttrs) {
        for (Attribute attr : f.attributes) attrWriter.write(f, attr, constant_pool);
        println();
    }
    indent(-1);
    if (options.showDisassembled || options.showLineAndLocalVariableTables)
        println();
}
Also used : 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) ConstantValue_attribute(com.sun.tools.classfile.ConstantValue_attribute) Attribute(com.sun.tools.classfile.Attribute) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Signature_attribute(com.sun.tools.classfile.Signature_attribute) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 4 with AccessFlags

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

the class AttributeWriter method visitInnerClasses.

public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) {
    boolean first = true;
    if (options.compat) {
        writeInnerClassHeader();
        first = false;
    }
    for (int i = 0; i < attr.classes.length; i++) {
        InnerClasses_attribute.Info info = attr.classes[i];
        //access
        AccessFlags access_flags = info.inner_class_access_flags;
        if (options.compat) {
            // BUG 6622215: javap ignores certain relevant access flags
            access_flags = access_flags.ignore(ACC_STATIC | ACC_PROTECTED | ACC_PRIVATE | ACC_INTERFACE | ACC_SYNTHETIC | ACC_ENUM);
            // BUG 6622232: javap gets whitespace confused
            print("   ");
        }
        if (options.checkAccess(access_flags)) {
            if (first) {
                writeInnerClassHeader();
                first = false;
            }
            print("   ");
            for (String name : access_flags.getInnerClassModifiers()) print(name + " ");
            if (info.inner_name_index != 0) {
                print("#" + info.inner_name_index + "= ");
            }
            print("#" + info.inner_class_info_index);
            if (info.outer_class_info_index != 0) {
                print(" of #" + info.outer_class_info_index);
            }
            print("; //");
            if (info.inner_name_index != 0) {
                print(getInnerName(constant_pool, info) + "=");
            }
            constantWriter.write(info.inner_class_info_index);
            if (info.outer_class_info_index != 0) {
                print(" of ");
                constantWriter.write(info.outer_class_info_index);
            }
            println();
        }
    }
    if (!first)
        indent(-1);
    return null;
}
Also used : InnerClasses_attribute(com.sun.tools.classfile.InnerClasses_attribute) AccessFlags(com.sun.tools.classfile.AccessFlags)

Example 5 with AccessFlags

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

Aggregations

AccessFlags (com.sun.tools.classfile.AccessFlags)6 Attribute (com.sun.tools.classfile.Attribute)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 Attributes (com.sun.tools.classfile.Attributes)1 ClassFile (com.sun.tools.classfile.ClassFile)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Code_attribute (com.sun.tools.classfile.Code_attribute)1 ConstantPool (com.sun.tools.classfile.ConstantPool)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