package asteroid.nodes;

import asteroid.A;

import java.util.List;
import java.util.ArrayList;

import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.stmt.Statement;

/**
 * Builder to create instance of type {@link MethodNode}
 *
 * @since 0.1.0
 */
public class MethodNodeBuilder {

    private final String name;
    private int modifiers;
    private Statement code;
    private ClassNode returnType;
    private Parameter[] parameters;
    private ClassNode[] exceptions;

    private MethodNodeBuilder(String name) {
        this.name = name;
    }

    public static MethodNodeBuilder method(String name) {
        return new MethodNodeBuilder(name);
    }

    public MethodNodeBuilder returnType(Class returnType) {
        this.returnType = A.NODES.clazz(returnType).build();
        return this;
    }

    public MethodNodeBuilder modifiers(int modifiers) {
        this.modifiers = modifiers;
        return this;
    }

    public MethodNodeBuilder parameters(Parameter... parameters) {
        this.parameters = parameters;
        return this;
    }

    public MethodNodeBuilder exceptions(ClassNode... exceptions) {
        this.exceptions = exceptions;
        return this;
    }

    public MethodNodeBuilder code(Statement code) {
        this.code = code;
        return this;
    }

    public MethodNode build() {
        MethodNode methodNode =
            new MethodNode(name,
                           modifiers,
                           returnType,
                           parameters,
                           exceptions,
                           code);

        return methodNode;
    }

}

@@ -533,24 +533,43 @@
 %5D parameters
+ = new Parameter%5B0%5D
 ;%0A    privat
@@ -592,16 +592,35 @@
 ceptions
+ = new ClassNode%5B0%5D
 ;%0A%0A    p

Fix NPE when no adding parameters or exceptions