Search in sources :

Example 1 with ClassFile

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

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

the class CheckDeps method analyzeDependencies.

/**
     * Analyze the dependencies of all classes in the given JAR file. The
     * method updates knownTypes and unknownRefs as part of the analysis.
     */
static void analyzeDependencies(Path jarpath) throws Exception {
    System.out.format("Analyzing %s%n", jarpath);
    try (JarFile jf = new JarFile(jarpath.toFile())) {
        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry e = entries.nextElement();
            String name = e.getName();
            if (name.endsWith(".class")) {
                ClassFile cf = ClassFile.read(jf.getInputStream(e));
                for (Dependency d : finder.findDependencies(cf)) {
                    String origin = toClassName(d.getOrigin().getName());
                    String target = toClassName(d.getTarget().getName());
                    // origin is now known
                    unknownRefs.remove(origin);
                    knownTypes.add(origin);
                    // if the target is not known then record the reference
                    if (!knownTypes.contains(target)) {
                        Set<String> refs = unknownRefs.get(target);
                        if (refs == null) {
                            // first time seeing this unknown type
                            refs = new HashSet<>();
                            unknownRefs.put(target, refs);
                        }
                        refs.add(origin);
                    }
                }
            }
        }
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Dependency(com.sun.tools.classfile.Dependency) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 3 with ClassFile

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

the class OverrideBridge method addMembers.

static void addMembers(File destDir, Map<ClassFile, List<Method>> members) {
    String[] names = { "B.class", "C.class", "D.class" };
    try {
        for (String name : names) {
            File f = new File(destDir, name);
            ClassFile cf = ClassFile.read(f);
            members.put(cf, readMethod(cf, "m"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading classes");
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) ClassFile(com.sun.tools.classfile.ClassFile) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException)

Example 4 with ClassFile

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

the class TestCP method verifyMethodHandleInvocationDescriptors.

void verifyMethodHandleInvocationDescriptors(File f) {
    System.err.println("verify: " + f);
    try {
        int count = 0;
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
                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");
        }
        int instr_count = 0;
        int cp_entry = -1;
        for (Instruction i : ea.getInstructions()) {
            if (i.getMnemonic().equals("invokevirtual")) {
                instr_count++;
                if (cp_entry == -1) {
                    cp_entry = i.getUnsignedShort(1);
                } else if (cp_entry != i.getUnsignedShort(1)) {
                    throw new Error("Unexpected CP entry in polymorphic signature call");
                }
                CONSTANT_Methodref_info methRef = (CONSTANT_Methodref_info) cf.constant_pool.get(cp_entry);
                String type = methRef.getNameAndTypeInfo().getType();
                if (!type.equals(PS_TYPE)) {
                    throw new Error("Unexpected type in polymorphic signature call: " + type);
                }
            }
        }
        if (instr_count != PS_CALLS_COUNT) {
            throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f + ": " + 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)

Example 5 with ClassFile

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

the class T7005371 method verifyLocalVariableTypeTableAttr.

void verifyLocalVariableTypeTableAttr(File f) {
    System.err.println("verify: " + f);
    try {
        ClassFile cf = ClassFile.read(f);
        Method testMethod = null;
        for (Method m : cf.methods) {
            if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
                testMethod = m;
                break;
            }
        }
        if (testMethod == null) {
            throw new Error("Missing method: " + TEST_METHOD_NAME);
        }
        Code_attribute code = (Code_attribute) testMethod.attributes.get(Attribute.Code);
        if (code == null) {
            throw new Error("Missing Code attribute for method: " + TEST_METHOD_NAME);
        }
        LocalVariableTypeTable_attribute lvt_table = (LocalVariableTypeTable_attribute) code.attributes.get(Attribute.LocalVariableTypeTable);
        if (lvt_table == null) {
            throw new Error("Missing LocalVariableTypeTable attribute for method: " + TEST_METHOD_NAME);
        }
        if (lvt_table.local_variable_table_length != LVT_LENGTH) {
            throw new Error("LocalVariableTypeTable has wrong size" + "\nfound: " + lvt_table.local_variable_table_length + "\nrequired: " + LVT_LENGTH);
        }
        String sig = cf.constant_pool.getUTF8Value(lvt_table.local_variable_table[0].signature_index);
        if (sig == null || !sig.equals(LVT_SIG_TYPE)) {
            throw new Error("LocalVariableTypeTable has wrong signature" + "\nfound: " + sig + "\nrequired: " + LVT_SIG_TYPE);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + f + ": " + e);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Code_attribute(com.sun.tools.classfile.Code_attribute) LocalVariableTypeTable_attribute(com.sun.tools.classfile.LocalVariableTypeTable_attribute) Method(com.sun.tools.classfile.Method)

Aggregations

ClassFile (com.sun.tools.classfile.ClassFile)16 Method (com.sun.tools.classfile.Method)8 Code_attribute (com.sun.tools.classfile.Code_attribute)5 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)3 Instruction (com.sun.tools.classfile.Instruction)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ConstantPool (com.sun.tools.classfile.ConstantPool)2 Path (java.nio.file.Path)2 JavacTask (com.sun.source.util.JavacTask)1 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)1 Dependency (com.sun.tools.classfile.Dependency)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 Field (com.sun.tools.classfile.Field)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 SourceFile_attribute (com.sun.tools.classfile.SourceFile_attribute)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1