Search in sources :

Example 1 with Method

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

use of com.sun.tools.classfile.Method in project checker-framework by typetools.

the class ComparisionException method findAnnotations.

// ///////////////// Extract type annotations //////////////////
private static void findAnnotations(ClassFile cf, List<TypeAnnotation> annos) {
    findAnnotations(cf, Attribute.RuntimeVisibleTypeAnnotations, annos);
    findAnnotations(cf, Attribute.RuntimeInvisibleTypeAnnotations, annos);
    for (Field f : cf.fields) {
        findAnnotations(cf, f, annos);
    }
    for (Method m : cf.methods) {
        findAnnotations(cf, m, annos);
    }
}
Also used : Field(com.sun.tools.classfile.Field) Method(com.sun.tools.classfile.Method)

Example 3 with Method

use of com.sun.tools.classfile.Method in project ceylon by eclipse.

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)

Example 4 with Method

use of com.sun.tools.classfile.Method in project ceylon by eclipse.

the class CompareTest 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);
    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) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Method(com.sun.tools.classfile.Method)

Example 5 with Method

use of com.sun.tools.classfile.Method in project ceylon by eclipse.

the class T7042566 method verifyBytecode.

void verifyBytecode(VarargsMethod selected, JavaSource source) {
    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();
                String sig = selected.parameterTypes.bytecodeSigStr;
                if (!type.contains(sig)) {
                    throw new Error("Unexpected type method call: " + type + "" + "\nfound: " + sig + "\n" + source.getCharContent(true));
                }
                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

Method (com.sun.tools.classfile.Method)21 ClassFile (com.sun.tools.classfile.ClassFile)15 Code_attribute (com.sun.tools.classfile.Code_attribute)10 Instruction (com.sun.tools.classfile.Instruction)6 InputStream (java.io.InputStream)5 File (java.io.File)4 Field (com.sun.tools.classfile.Field)3 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)2 ConstantPool (com.sun.tools.classfile.ConstantPool)2 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)2 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)2 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attribute (com.sun.tools.classfile.Attribute)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Descriptor (com.sun.tools.classfile.Descriptor)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 DescriptorException (com.sun.tools.classfile.DescriptorException)1