Search in sources :

Example 1 with ConstantPool

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

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

the class LocalVariableTableWriter method writeLocalVariables.

public void writeLocalVariables(int pc, NoteKind kind) {
    ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
    // get from Options?
    String indent = space(2);
    List<LocalVariableTable_attribute.Entry> entries = pcMap.get(pc);
    if (entries != null) {
        for (ListIterator<LocalVariableTable_attribute.Entry> iter = entries.listIterator(kind == NoteKind.END ? entries.size() : 0); kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext(); ) {
            LocalVariableTable_attribute.Entry entry = kind == NoteKind.END ? iter.previous() : iter.next();
            if (kind.match(entry, pc)) {
                print(indent);
                print(kind.text);
                print(" local ");
                print(entry.index);
                print(" // ");
                Descriptor d = new Descriptor(entry.descriptor_index);
                try {
                    print(d.getFieldType(constant_pool));
                } catch (InvalidDescriptor e) {
                    print(report(e));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                print(" ");
                try {
                    print(constant_pool.getUTF8Value(entry.name_index));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                println();
            }
        }
    }
}
Also used : 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) LocalVariableTable_attribute(com.sun.tools.classfile.LocalVariableTable_attribute)

Example 3 with ConstantPool

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

the class ConstantWriter method writeConstantPool.

protected void writeConstantPool() {
    ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
    writeConstantPool(constant_pool);
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool)

Example 4 with ConstantPool

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

the class AnnotationWriter method writeDescriptor.

private void writeDescriptor(int index, boolean resolveIndices) {
    if (resolveIndices) {
        try {
            ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
            Descriptor d = new Descriptor(index);
            print(d.getFieldType(constant_pool));
            return;
        } catch (ConstantPoolException ignore) {
        } catch (InvalidDescriptor ignore) {
        }
    }
    print("#" + index);
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Example 5 with ConstantPool

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

the class LocalVariableTypeTableWriter method writeLocalVariables.

public void writeLocalVariables(int pc, NoteKind kind) {
    ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
    // get from Options?
    String indent = space(2);
    List<LocalVariableTypeTable_attribute.Entry> entries = pcMap.get(pc);
    if (entries != null) {
        for (ListIterator<LocalVariableTypeTable_attribute.Entry> iter = entries.listIterator(kind == NoteKind.END ? entries.size() : 0); kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext(); ) {
            LocalVariableTypeTable_attribute.Entry entry = kind == NoteKind.END ? iter.previous() : iter.next();
            if (kind.match(entry, pc)) {
                print(indent);
                print(kind.text);
                print(" generic local ");
                print(entry.index);
                print(" // ");
                Descriptor d = new Signature(entry.signature_index);
                try {
                    print(d.getFieldType(constant_pool).toString().replace("/", "."));
                } catch (InvalidDescriptor e) {
                    print(report(e));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                print(" ");
                try {
                    print(constant_pool.getUTF8Value(entry.name_index));
                } catch (ConstantPoolException e) {
                    print(report(e));
                }
                println();
            }
        }
    }
}
Also used : ConstantPool(com.sun.tools.classfile.ConstantPool) Signature(com.sun.tools.classfile.Signature) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) LocalVariableTypeTable_attribute(com.sun.tools.classfile.LocalVariableTypeTable_attribute) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor) Descriptor(com.sun.tools.classfile.Descriptor) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Aggregations

ConstantPool (com.sun.tools.classfile.ConstantPool)8 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)5 Descriptor (com.sun.tools.classfile.Descriptor)4 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)4 Method (com.sun.tools.classfile.Method)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassFile (com.sun.tools.classfile.ClassFile)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Field (com.sun.tools.classfile.Field)1 LocalVariableTable_attribute (com.sun.tools.classfile.LocalVariableTable_attribute)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 Signature (com.sun.tools.classfile.Signature)1 StackMapTable_attribute.verification_type_info (com.sun.tools.classfile.StackMapTable_attribute.verification_type_info)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1