Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Disassembly

Brian S. O'Neill edited this page Jun 2, 2013 · 2 revisions

Class disassembly

A

    private static java.util.Map cPropertiesCache&#59;

    static {
        // max stack: 2
        // max locals: 0
        // line 35
        new org.cojen.util.WeakIdentityMap
        dup
        invokespecial void org.cojen.util.WeakIdentityMap.&lt&#59;init&gt&#59;()
        putstatic java.util.Map org.cojen.util.BeanIntrospector.cPropertiesCache
        return
    }

    public static void main(java.lang.String[])
        throws java.lang.Exception
    {
        // max stack: 3
        // max locals: 1
        // line 38
        getstatic java.io.PrintStream java.lang.System.out
        aload_0
        iconst_0
        aaload
        invokestatic java.lang.Class java.lang.Class.forName(java.lang.String)
        invokestatic java.util.Map org.cojen.util.BeanIntrospector.getAllProperties(java.lang.Class)
        invokevirtual void java.io.PrintStream.println(java.lang.Object)
        // line 39
        return
    }

    ...

}

When disassembled using the builder format, a valid Java source file is generated which when compiled and run, rebuilds the original class. This demonstrates how to use Cojen, and it also allows Java classes to be modified when source code is not available.

Even if the generated class file builder is run without modification, the emitted class file will likely produce a different binary representation than the original class. This is because Cojen re-orders the class constant pool and reduces local variable usage.

java org.cojen.classfile.DisassemblyTool -f builder org.cojen.util.BeanIntrospector
import org.cojen.classfile.ClassFile&#59;
import org.cojen.classfile.CodeBuilder&#59;
import org.cojen.classfile.FieldInfo&#59;
import org.cojen.classfile.Label&#59;
import org.cojen.classfile.LocalVariable&#59;
import org.cojen.classfile.Location&#59;
import org.cojen.classfile.MethodInfo&#59;
import org.cojen.classfile.Modifiers&#59;
import org.cojen.classfile.Opcode&#59;
import org.cojen.classfile.TypeDesc&#59;

/**
 * Builds ClassFile for org.cojen.util.BeanIntrospector
 *
 * @author auto-generated
 */
public class ClassFileBuilder {
    public static void main(String[] args) throws Exception {
        // public class org.cojen.util.BeanIntrospector
        ClassFile cf = createClassFile()&#59;

        if (args.length &gt&#59; 0) {
            File file = new File(args[0])&#59;
            if (file.isDirectory()) {
                writeClassFiles(cf, file)&#59;
            } else {
                OutputStream out = new BufferedOutputStream(new FileOutputStream(file))&#59;
                cf.writeTo(out)&#59;
                out.close()&#59;
            }
        }
    }

    private static void writeClassFiles(ClassFile cf, File dir) throws Exception {
        File file = new File(dir, cf.getClassName().replace('.', '/') + ".class")&#59;
        file.getParentFile().mkdirs()&#59;
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file))&#59;
        cf.writeTo(out)&#59;
        out.close()&#59;

        ClassFile[] innerClasses = cf.getInnerClasses()&#59;
        for (int i=0&#59; i&lt&#59;innerClasses.length&#59; i++) {
            writeClassFiles(innerClasses[i], dir)&#59;
        }
    }

    public static ClassFile createClassFile() {
        ClassFile cf = new ClassFile("org.cojen.util.BeanIntrospector", "java.lang.Object")&#59;
        cf.setTarget("1.0")&#59;
        cf.setSourceFile("BeanIntrospector.java")&#59;

        createStaticInitializer(cf)&#59;

        //
        // Create fields
        //

        cf.addField(Modifiers.PRIVATE.toStatic(true),
                    "cPropertiesCache",
                    TypeDesc.forClass("java.util.Map"))&#59;

        FieldInfo fi = cf.addField(Modifiers.NONE.toStatic(true),
                                   "class$java$lang$Object",
                                   TypeDesc.forClass("java.lang.Class"))&#59;
        fi.markSynthetic()&#59;

        //
        // Create constructors
        //

        // public void &lt&#59;init&gt&#59;()
        createConstructor_1(cf)&#59;

        //
        // Create methods
        //

        // public static void main(java.lang.String[])
        createMethod_1(cf)&#59;

        // public static java.util.Map getAllProperties(java.lang.Class)
        createMethod_2(cf)&#59;

        ...

        return cf&#59;
    }

    private static void createStaticInitializer(ClassFile cf) {
        MethodInfo mi = cf.addInitializer()&#59;
        CodeBuilder b = new CodeBuilder(mi)&#59;

        b.mapLineNumber(35)&#59;
        TypeDesc type_1 = TypeDesc.forClass("org.cojen.util.WeakIdentityMap")&#59;
        b.newObject(type_1)&#59;
        b.dup()&#59;
        b.invokeConstructor("org.cojen.util.WeakIdentityMap", null)&#59;
        TypeDesc type_2 = TypeDesc.forClass("java.util.Map")&#59;
        b.storeStaticField("cPropertiesCache", type_2)&#59;
        b.returnVoid()&#59;
    }

    // public void &lt&#59;init&gt&#59;()
    private static void createConstructor_1(ClassFile cf) {
        MethodInfo mi = cf.addConstructor(Modifiers.PUBLIC, null)&#59;
        CodeBuilder b = new CodeBuilder(mi)&#59;

        b.mapLineNumber(33)&#59;
        b.loadThis()&#59;
        b.invokeSuperConstructor(null)&#59;

        b.mapLineNumber(307)&#59;
        b.returnVoid()&#59;
    }

    // public static void main(java.lang.String[])
    private static void createMethod_1(ClassFile cf) {
        MethodInfo mi = cf.addMethod(Modifiers.PUBLIC_STATIC,
                                     "main",
                                     null,
                                     new TypeDesc[] {TypeDesc.STRING.toArrayType()})&#59;
        mi.addException(TypeDesc.forClass("java.lang.Exception"))&#59;
        CodeBuilder b = new CodeBuilder(mi)&#59;

        LocalVariable var_1 = b.getParameter(0)&#59;

        b.mapLineNumber(38)&#59;
        TypeDesc type_1 = TypeDesc.forClass("java.io.PrintStream")&#59;
        b.loadStaticField("java.lang.System", "out", type_1)&#59;
        b.loadLocal(var_1)&#59;
        b.loadConstant(0)&#59;
        b.loadFromArray(TypeDesc.OBJECT)&#59;
        TypeDesc type_2 = TypeDesc.forClass("java.lang.Class")&#59;
        TypeDesc[] params_1 = new TypeDesc[] {TypeDesc.STRING}&#59;
        b.invokeStatic("java.lang.Class", "forName", type_2, params_1)&#59;
        TypeDesc type_3 = TypeDesc.forClass("java.util.Map")&#59;
        TypeDesc[] params_2 = new TypeDesc[] {type_2}&#59;
        b.invokeStatic("getAllProperties", type_3, params_2)&#59;
        TypeDesc[] params_3 = new TypeDesc[] {TypeDesc.OBJECT}&#59;
        b.invokeVirtual("java.io.PrintStream", "println", null, params_3)&#59;

        b.mapLineNumber(39)&#59;
        b.returnVoid()&#59;
    }

    ...

}
Clone this wiki locally