All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.jpattern.org.cojen.classfile.BuilderStylePrinter Maven / Gradle / Ivy

Go to download

This is a copy of the good Cojen project from http://cojen.sourceforge.net/ with package name changed

The newest version!
/*
 *  Copyright 2004-2010 Brian S O'Neill
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.googlecode.jpattern.org.cojen.classfile;

import java.io.PrintWriter;

import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantClassInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantDoubleInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantFieldInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantFloatInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantIntegerInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantInterfaceMethodInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantLongInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantMethodInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantNameAndTypeInfo;
import com.googlecode.jpattern.org.cojen.classfile.constant.ConstantStringInfo;

/**
 * Disassembles a ClassFile into a Java source file, which when run, produces
 * the original class.
 *
 * @author Brian S O'Neill
 */
class BuilderStylePrinter implements DisassemblyTool.Printer {
    private PrintWriter mOut;

    private int mIndent = 0;
    private boolean mNeedIndent = true;

    public BuilderStylePrinter() {
    }

    public void disassemble(ClassFile cf, PrintWriter out) {
        mOut = out;

        println("import java.io.BufferedOutputStream;");
        println("import java.io.File;");
        println("import java.io.FileOutputStream;");
        println("import java.io.OutputStream;");
        println();
        println("import org.cojen.classfile.ClassFile;");
        println("import org.cojen.classfile.CodeBuilder;");
        println("import org.cojen.classfile.FieldInfo;");
        println("import org.cojen.classfile.Label;");
        println("import org.cojen.classfile.LocalVariable;");
        println("import org.cojen.classfile.Location;");
        println("import org.cojen.classfile.MethodInfo;");
        println("import org.cojen.classfile.Modifiers;");
        println("import org.cojen.classfile.Opcode;");
        println("import org.cojen.classfile.TypeDesc;");

        disassemble(cf, (String)null);
    }
    
    private void disassemble(ClassFile cf, String innerClassSuffix) {
        println();
        if (innerClassSuffix == null) {
            println("/**");
            println(" * Builds ClassFile for " + cf.getClassName());
            println(" *");
            println(" * @author auto-generated");
            println(" */");
            println("public class ClassFileBuilder {");
        } else {
            println("/**");
            println(" * Builds ClassFile for " + cf.getClassName());
            println(" */");
            println("private static class InnerBuilder" + innerClassSuffix + " {");
        }
        mIndent += 4;

        if (innerClassSuffix == null) {
            println("public static void main(String[] args) throws Exception {");
            mIndent += 4;
            println("// " + cf);
            println("ClassFile cf = createClassFile();");

            println();
            println("if (args.length > 0) {");
            mIndent += 4;
            println("File file = new File(args[0]);");
            println("if (file.isDirectory()) {");
            mIndent += 4;
            println("writeClassFiles(cf, file);");
            mIndent -= 4;
            println("} else {");
            mIndent += 4;
            println("OutputStream out = new BufferedOutputStream(new FileOutputStream(file));");
            println("cf.writeTo(out);");
            println("out.close();");
            mIndent -= 4;
            println("}");
            mIndent -= 4;
            println("}");
            mIndent -= 4;
            println("}");

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

            println();
            println("ClassFile[] innerClasses = cf.getInnerClasses();");
            println("for (int i=0; i")) {
            println("Initializer();");
        } else if (mi.getName().equals("")) {
            print("Constructor(");
            printModifiers(mi);
            print(", ");
            print(mi.getMethodDescriptor().getParameterTypes());
            println(");");
        } else {
            print("Method(");
            printModifiers(mi);
            print(", ");
            print("\"" + escape(mi.getName()) + "\", ");
            print(mi.getMethodDescriptor().getReturnType());
            print(", ");
            print(mi.getMethodDescriptor().getParameterTypes());
            println(");");
        }

        if (mi.isSynthetic()) {
            println("mi.markSynthetic();");
        }
        if (mi.isDeprecated()) {
            println("mi.markDeprecated();");
        }
        TypeDesc[] exceptions = mi.getExceptions();
        for (int j=0; j 0) {
                print(", ");
            }
            print(params[i]);
        }

        print("}");
    }

    private void print(String text) {
        indent();
        mOut.print(text);
    }

    private void println(String text) {
        print(text);
        println();
    }

    private void println() {
        mOut.println();
        mNeedIndent = true;
    }

    private void indent() {
        if (mNeedIndent) {
            for (int i=mIndent; --i>= 0; ) {
                mOut.print(' ');
            }
            mNeedIndent = false;
        }
    }

    private String generateIndent(int amount) {
        StringBuffer buf = new StringBuffer(amount);
        for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy