package com.supermathie.sourcegen.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

import com.supermathie.sourcegen.ClassSource;
import com.supermathie.sourcegen.FormattingRules;
import com.supermathie.sourcegen.JavaSourceFile;
import com.supermathie.sourcegen.MethodSource;

/**
 * This class illustrates the basic usage of SourceGen.
 */
public class SourceGenExample {

        public static void main(String[] args) throws IOException {
                // The JavaSourceFile object represents a Java source file, which
                // could be an interface or a class.  In this case, we will be
                // creating a new class.
                JavaSourceFile sourceFile = new JavaSourceFile("com.example.foo");

                ArrayList imports = new ArrayList();
                imports.add("com.example.bar.*");
                sourceFile.setImports(imports);

                // Create the public class for the Java source file.  Each Java
                // source file must contain exactly one public class.  It is not
                // allowed to change a class from public to non-public or
                // vice-versa.  An attempt to change the modifiers in this way
                // results in an IllegalArgumentException.
                boolean publicClass = true;
                ClassSource classSource = new ClassSource(publicClass, "Foo");
                sourceFile.setPublicClass(classSource);

                // If we wanted to create an interface, we would change the class
                // modifiers like so:
                // classSource.setModifiers(Modifier.INTERFACE);

                // This class will implement the IFoo interface.
                classSource.setImplements("IFoo");

                // Foo will extend Bar.
                classSource.setSuperClass("Bar");

                // Create a private field "baz" of type "Baz" and add a getter
                // and setter method.
                classSource.addProperty("Baz", "baz");

                // Create a toString method that is not a constructor and returns a
                // String.
                MethodSource methodSource = new MethodSource(false, "toString", "String");
                classSource.addMethod(methodSource);

                // Just for fun, we'll make it public and synchronized.
                methodSource.setModifiers(Modifier.PUBLIC | Modifier.SYNCHRONIZED);

                ArrayList codeLines = new ArrayList();
                codeLines.add("StringBuffer sb = new StringBuffer();");
                codeLines.add("sb.append(\"Foo:  \\\"\");");
                codeLines.add("sb.append(baz.toString());");
                codeLines.add("sb.append(\"\\\"\");");
                codeLines.add("");
                codeLines.add("return sb.toString()");

                methodSource.setCodeLines(codeLines);

                // Use 4 spaces for the indent.
                FormattingRules rules = new FormattingRules(true, 4);

                // Create the file on disk.
                StringBuffer sb = new StringBuffer();
                sourceFile.output(rules, sb);
                BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\temp\\Foo.java"));
                bw.write(sb.toString());
                bw.close();
        }

}