org.codehaus.groovy.ast.expr.SpreadExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.SpreadExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InnerClassVisitorHelper.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) {
    List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
    gStringStrings.add(new ConstantExpression(""));
    gStringStrings.add(new ConstantExpression(""));
    List<Expression> gStringValues = new ArrayList<Expression>();
    gStringValues.add(new VariableExpression(parameters[0]));
    block.addStatement(
            new ReturnStatement(
                    new MethodCallExpression(
                            thiz,
                            new GStringExpression("$name", gStringStrings, gStringValues),
                            new ArgumentListExpression(
                                    new SpreadExpression(new VariableExpression(parameters[1]))
                            )
                    )
            )
    );
}
 
Example #2
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void despreadList(final List<Expression> expressions, final boolean wrap) {
    List<Expression> spreadIndexes = new ArrayList<>();
    List<Expression> spreadExpressions = new ArrayList<>();
    List<Expression> normalArguments = new ArrayList<>();
    for (int i = 0, n = expressions.size(); i < n; i += 1) {
        Expression expr = expressions.get(i);
        if (!(expr instanceof SpreadExpression)) {
            normalArguments.add(expr);
        } else {
            spreadIndexes.add(new ConstantExpression(i - spreadExpressions.size(), true));
            spreadExpressions.add(((SpreadExpression) expr).getExpression());
        }
    }

    // load normal arguments as array
    visitTupleExpression(new ArgumentListExpression(normalArguments), wrap);
    // load spread expressions as array
    new TupleExpression(spreadExpressions).visit(this);
    // load insertion index
    new ArrayExpression(ClassHelper.int_TYPE, spreadIndexes, null).visit(this);

    controller.getOperandStack().remove(1);
    despreadList.call(controller.getMethodVisitor());
}
 
Example #3
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static boolean containsSpreadExpression(final Expression arguments) {
    List<Expression> args;
    if (arguments instanceof TupleExpression) {
        TupleExpression tupleExpression = (TupleExpression) arguments;
        args = tupleExpression.getExpressions();
    } else if (arguments instanceof ListExpression) {
        ListExpression le = (ListExpression) arguments;
        args = le.getExpressions();
    } else {
        return arguments instanceof SpreadExpression;
    }
    for (Expression arg : args) {
        if (arg instanceof SpreadExpression) return true;
    }
    return false;
}
 
Example #4
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitSpreadExpression(SpreadExpression node) {
	pushASTNode(node);
	try {
		super.visitSpreadExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #5
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(SpreadExpression node) {
    if (isInside(node, line, column)) {
        super.visitSpreadExpression(node);
    }
}
 
Example #6
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
    super.visitSpreadExpression(expression);
    tryFind(SpreadExpression.class, expression);
}
 
Example #7
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
    pushContext(expression);
    super.visitSpreadExpression(expression);
    popContext();
}
 
Example #8
Source File: ClassNodeUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the given method has a possibly matching static method with the given name and arguments.
 * Handles default arguments and optionally spread expressions.
 *
 * @param cNode     the ClassNode of interest
 * @param name      the name of the method of interest
 * @param arguments the arguments to match against
 * @param trySpread whether to try to account for SpreadExpressions within the arguments
 * @return true if a matching method was found
 */
public static boolean hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread) {
    int count = 0;
    boolean foundSpread = false;

    if (arguments instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) arguments;
        for (Expression arg : tuple.getExpressions()) {
            if (arg instanceof SpreadExpression) {
                foundSpread = true;
            } else {
                count++;
            }
        }
    } else if (arguments instanceof MapExpression) {
        count = 1;
    }

    for (MethodNode method : cNode.getMethods(name)) {
        if (method.isStatic()) {
            Parameter[] parameters = method.getParameters();
            // do fuzzy match for spread case: count will be number of non-spread args
            if (trySpread && foundSpread && parameters.length >= count) return true;

            if (parameters.length == count) return true;

            // handle varargs case
            if (parameters.length > 0 && parameters[parameters.length - 1].getType().isArray()) {
                if (count >= parameters.length - 1) return true;
                // fuzzy match any spread to a varargs
                if (trySpread && foundSpread) return true;
            }

            // handle parameters with default values
            int nonDefaultParameters = 0;
            for (Parameter parameter : parameters) {
                if (!parameter.hasInitialExpression()) {
                    nonDefaultParameters++;
                }
            }

            if (count < parameters.length && nonDefaultParameters <= count) {
                return true;
            }
            // TODO handle spread with nonDefaultParams?
        }
    }
    return false;
}
 
Example #9
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
    assertExpressionAuthorized(expression);
    expression.getExpression().visit(this);
}
 
Example #10
Source File: StaticCompilationVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
}
 
Example #11
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
    super.visitSpreadExpression(expression);
    trn.visitSpreadExpression(expression);
}
 
Example #12
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(SpreadExpression expression) {
    expression.getExpression().visit(this);
}
 
Example #13
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitSpreadExpression(final SpreadExpression expression) {
    throw new GroovyBugError("SpreadExpression should not be visited here");
}
 
Example #14
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitSpreadExpression(SpreadExpression expression);