package asteroid.utils;

import static org.codehaus.groovy.runtime.DefaultGroovyMethods.last;
import static org.codehaus.groovy.runtime.DefaultGroovyMethods.first;

import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;

/**
 * Utility classes to deal with {@link MethodCallExpression} instances
 *
 * @since 0.1.4
 * @see asteroid.Utils
 */
public final class MethodCallExpressionUtils {

    /**
     * Given a {@link MethodCallExpression} it returns a list of arguments
     *
     * @param methodCallExpr a method call we want the arguments from
     * @return a list of expressions within a {@link ArgumentListExpression}
     * @since 0.1.4
     */
    public ArgumentListExpression getArgs(final MethodCallExpression methodCallExpr) {
        return (ArgumentListExpression) methodCallExpr.getArguments();
    }

    /**
     * Return the first element of the {@link ArgumentListExpression}
     * passed as parameters as the expected type.
     *
     * @param args the list of arguments
     * @param asType the expected type
     * @return the first argument casted as the expected type
     * @since 0.1.4
     */
    public <U extends Expression> U getFirstArgumentAs(final ArgumentListExpression args, final Class<U> asType) {
        return asType.cast(first(args.getExpressions()));
    }

    /**
     * Return the last element of the {@link ArgumentListExpression}
     * passed as parameters as the expected type.
     *
     * @param args the list of arguments
     * @param asType the expected type
     * @return the last argument casted as the expected type
     * @since 0.1.4
     */
    public <U extends Expression> U getLastArgumentAs(final ArgumentListExpression args, final Class<U> asType) {
        return asType.cast(last(args.getExpressions()));
    }
}

@@ -325,16 +325,40 @@
 ssion;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * U
@@ -1425,32 +1425,925 @@
 ons()));%0A    %7D%0A%0A
+    /**%0A     * Return the element in the specified index from the %7B@link%0A     * ArgumentListExpression%7D passed as argument as the expected%0A     * type.%0A     *%0A     * @param args the argument list expression we want the argument from%0A     * @param index the index where the expression is located%0A     * @param asType the %7B@link Class%7D of the expected expression%0A     * @return the expression found in the expression list using the%0A     * provided index or null if it couldn't be found%0A     * @since 0.2.8%0A     */%0A    public %3CU extends Expression%3E U getArgumentByIndexAs(final ArgumentListExpression args, final Integer index, final Class%3CU%3E asType) %7B%0A        final List%3CExpression%3E expressionList = args.getExpressions();%0A%0A        if (expressionList.isEmpty() %7C%7C expressionList.size() %3C index) %7B%0A            return null;%0A        %7D%0A%0A        return asType.cast(expressionList.get(index));%0A    %7D%0A%0A
     /**%0A     * R

Add method to get any argument from a list expression by index