Java Code Examples for org.codehaus.groovy.ast.expr.ClosureExpression#getParameters()

The following examples show how to use org.codehaus.groovy.ast.expr.ClosureExpression#getParameters() . 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: VariableScopeVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClosureExpression(final ClosureExpression expression) {
    pushState();
    expression.setVariableScope(currentScope);
    inClosure = !isAnonymous(currentScope.getParent().getClassScope());

    if (expression.isParameterSpecified()) {
        for (Parameter parameter : expression.getParameters()) {
            parameter.setInStaticContext(currentScope.isInStaticContext());
            if (parameter.hasInitialExpression()) {
                parameter.getInitialExpression().visit(this);
            }
            declare(parameter, expression);
        }
    } else if (expression.getParameters() != null) {
        Parameter var = new Parameter(ClassHelper.OBJECT_TYPE, "it");
        var.setInStaticContext(currentScope.isInStaticContext());
        currentScope.putDeclaredVariable(var);
    }

    super.visitClosureExpression(expression);
    markClosureSharedVariables();

    popState();
}
 
Example 2
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClosureExpression(ClosureExpression node) {
    if (isInside(node, line, column)) {
        super.visitClosureExpression(node);
        if (node.isParameterSpecified()) {
            for (Parameter parameter : node.getParameters()) {
                isInside(parameter, line, column);
            }
        }
    }
}
 
Example 3
Source File: VariableFinderVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClosureExpression(ClosureExpression expression) {
    if (!blocks.remove(expression)) {
        return;
    }

    if (expression.isParameterSpecified()) {
        for (Parameter param : expression.getParameters()) {
            variables.put(param.getName(), param);
        }
    } else {
        variables.put("it", new VariableExpression("it")); // NOI18N
    }
    super.visitClosureExpression(expression);
}
 
Example 4
Source File: MacroGroovyMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Macro
public static Expression macro(MacroContext macroContext, PropertyExpression phaseExpression, ConstantExpression asIsConstantExpression, ClosureExpression closureExpression) {
    if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
        macroContext.getSourceUnit().addError(new SyntaxException("Macro closure arguments are not allowed" + '\n', closureExpression));
        return macroContext.getCall();
    }

    final String source;
    try {
        source = ClosureUtils.convertClosureToSource(macroContext.getSourceUnit().getSource(), closureExpression);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();

    Boolean asIs = (Boolean) asIsConstantExpression.getValue();

    return callX(
            propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"),
            "macro",
            args(
                    phaseExpression != null ? phaseExpression : constX(null),
                    asIsConstantExpression,
                    constX(source),
                    buildSubstitutions(macroContext.getSourceUnit(), closureExpression),
                    classX(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false))
            )
    );
}
 
Example 5
Source File: ClosureUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * @return true if the ClosureExpression has an implicit 'it' parameter
 */
public static boolean hasImplicitParameter(ClosureExpression ce) {
    return ce.getParameters() != null && ce.getParameters().length == 0;
}
 
Example 6
Source File: ClosureUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * @return the parameters for the ClosureExpression
 */
public static Parameter[] getParametersSafe(ClosureExpression ce) {
    return ce.getParameters() != null ? ce.getParameters() : Parameter.EMPTY_ARRAY;
}
 
Example 7
Source File: ClosureWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected ClassNode createClosureClass(final ClosureExpression expression, final int modifiers) {
    ClassNode classNode = controller.getClassNode();
    ClassNode outerClass = controller.getOutermostClass();
    String name = genClosureClassName();
    boolean staticMethodOrInStaticClass = controller.isStaticMethod() || classNode.isStaticClass();

    Parameter[] parameters = expression.getParameters();
    if (parameters == null) {
        parameters = Parameter.EMPTY_ARRAY;
    } else if (parameters.length == 0) {
        // let's create a default 'it' parameter
        Parameter it = new Parameter(ClassHelper.OBJECT_TYPE, "it", ConstantExpression.NULL);
        parameters = new Parameter[]{it};
        Variable ref = expression.getVariableScope().getDeclaredVariable("it");
        if (ref!=null) it.setClosureSharedVariable(ref.isClosureSharedVariable());
    }

    Parameter[] localVariableParams = getClosureSharedVariables(expression);
    removeInitialValues(localVariableParams);

    InnerClassNode answer = new InnerClassNode(classNode, name, modifiers, ClassHelper.CLOSURE_TYPE.getPlainNodeReference());
    answer.setEnclosingMethod(controller.getMethodNode());
    answer.setSynthetic(true);
    answer.setUsingGenerics(outerClass.isUsingGenerics());
    answer.setSourcePosition(expression);

    if (staticMethodOrInStaticClass) {
        answer.setStaticClass(true);
    }
    if (controller.isInScriptBody()) {
        answer.setScriptBody(true);
    }
    MethodNode method =
            answer.addMethod("doCall", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, parameters, ClassNode.EMPTY_ARRAY, expression.getCode());
    method.setSourcePosition(expression);

    VariableScope varScope = expression.getVariableScope();
    if (varScope == null) {
        throw new RuntimeException(
                "Must have a VariableScope by now! for expression: " + expression + " class: " + name);
    } else {
        method.setVariableScope(varScope.copy());
    }
    if (parameters.length > 1
            || (parameters.length == 1
            && parameters[0].getType() != null
            && parameters[0].getType() != ClassHelper.OBJECT_TYPE
            && !ClassHelper.OBJECT_TYPE.equals(parameters[0].getType().getComponentType())))
    {

        // let's add a typesafe call method
        MethodNode call = answer.addMethod(
                "call",
                ACC_PUBLIC,
                ClassHelper.OBJECT_TYPE,
                parameters,
                ClassNode.EMPTY_ARRAY,
                new ReturnStatement(
                        new MethodCallExpression(
                                VariableExpression.THIS_EXPRESSION,
                                "doCall",
                                new ArgumentListExpression(parameters))));
        call.setSourcePosition(expression);
    }

    // let's make the constructor
    BlockStatement block = createBlockStatementForConstructor(expression, outerClass, classNode);

    // let's assign all the parameter fields from the outer context
    addFieldsAndGettersForLocalVariables(answer, localVariableParams);

    addConstructor(expression, localVariableParams, answer, block);

    correctAccessedVariable(answer,expression);

    return answer;
}