ce.getVariableScope().putReferencedLocalVariable((Parameter) parameter.get()); Parameter[] params = ce.getParameters(); if (params == null) { params = Parameter.EMPTY_ARRAY; ce.getCode().visit(this); varStack.removeLast();
public static ClosureExpression closureX(Parameter[] params, Statement code) { return new ClosureExpression(params, code); }
public void visitClosureExpression(ClosureExpression expression) { pushState(); expression.setVariableScope(currentScope); if (expression.isParameterSpecified()) { Parameter[] parameters = expression.getParameters(); for (Parameter parameter : parameters) { 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(); }
@Override public void visitClosureExpression(ClosureExpression expression) { if (expression.isSynthetic()) { return; } Parameter[] origParams = expression.getParameters(); for (Parameter p : origParams) { p.setModifiers(p.getModifiers() | Modifier.FINAL); } super.visitClosureExpression(expression); }
private ClassNode getTypeFromClosureArguments(Parameter parameter, TypeCheckingContext.EnclosingClosure enclosingClosure) { ClosureExpression closureExpression = enclosingClosure.getClosureExpression(); ClassNode[] closureParamTypes = (ClassNode[]) closureExpression.getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS); if (closureParamTypes == null) return null; final Parameter[] parameters = closureExpression.getParameters(); String name = parameter.getName(); if (parameters != null) { if (parameters.length == 0) { return "it".equals(name) && closureParamTypes.length != 0 ? closureParamTypes[0] : null; } for (int index = 0; index < parameters.length; index++) { if (name.equals(parameters[index].getName())) { return closureParamTypes.length > index ? closureParamTypes[index] : null; } } } return null; }
protected Expression transformClosureExpression(ClosureExpression ce) { boolean oldInClosure = inClosure; inClosure = true; if (ce.getParameters() != null) { for (Parameter p : ce.getParameters()) { if (p.hasInitialExpression()) { p.setInitialExpression(transform(p.getInitialExpression())); } } } Statement code = ce.getCode(); if (code != null) code.visit(this); inClosure = oldInClosure; return ce; }
validator.secondPass(closureExpression); List<Parameter> parameters = new ArrayList<Parameter>(Arrays.asList(closureExpression.getParameters())); if (booleanExpressions == null || booleanExpressions.isEmpty()) return; BlockStatement closureBlockStatement = (BlockStatement) closureExpression.getCode(); ClosureExpression rewrittenClosureExpression = new ClosureExpression(parameters.toArray(new Parameter[parameters.size()]), newClosureBlockStatement); rewrittenClosureExpression.setSourcePosition(closureExpression); rewrittenClosureExpression.setDeclaringClass(closureExpression.getDeclaringClass()); rewrittenClosureExpression.setSynthetic(true); rewrittenClosureExpression.setVariableScope(correctVariableScope(closureExpression.getVariableScope(), methodNode)); rewrittenClosureExpression.setType(closureExpression.getType());
@Override public void visitClosureExpression(final ClosureExpression expression) { super.visitClosureExpression(expression); if (inBuilderMethod) { Statement oldCode = expression.getCode(); BlockStatement block = oldCode instanceof BlockStatement? ((BlockStatement)oldCode): new BlockStatement(Arrays.asList(oldCode), new VariableScope()); List<Statement> statements = block.getStatements(); if (!statements.isEmpty()) { Statement first = statements.get(0); Statement last = statements.get(statements.size()-1); if (expression.getLineNumber()<first.getLineNumber()) { // there's a new line between { -> ... and the first statement statements.add(0,createNewLine(expression)); } if (expression.getLastLineNumber()>last.getLastLineNumber()) { // there's a new line between { -> ... and the first statement statements.add(createNewLine(expression)); } } expression.setCode(block); } }
reformatted.setSourcePosition(exp); ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted)); clos.setVariableScope(new VariableScope()); MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"), "stringOf",
@Override public void visitClosureExpression(ClosureExpression expression) { secondPass = false; if (expression.getCode() == null || expression.getCode() instanceof EmptyStatement) { addError("[GContracts] Annotation does not contain any expressions (e.g. use '@Requires({ argument1 })').", expression); } if (expression.getCode() instanceof BlockStatement && ((BlockStatement) expression.getCode()).getStatements().isEmpty()) { addError("[GContracts] Annotation does not contain any expressions (e.g. use '@Requires({ argument1 })').", expression); } if (expression.isParameterSpecified() && !AnnotationUtils.hasAnnotationOfType(annotationNode.getClassNode(), POSTCONDITION_TYPE_NAME)) { addError("[GContracts] Annotation does not support parameters (the only exception are postconditions).", expression); } if (expression.isParameterSpecified()) { for (Parameter param : expression.getParameters()) { if (!("result".equals(param.getName()) || "old".equals(param.getName()))) { addError("[GContracts] Postconditions only allow 'old' and 'result' closure parameters.", expression); } if (!param.isDynamicTyped()) { addError("[GContracts] Postconditions do not support explicit types.", expression); } } } super.visitClosureExpression(expression); }
public static boolean hasImplicitParameter(ClosureExpression expr) { return expr.getParameters() != null && expr.getParameters().length == 0; }
public void transformClosureExpression(ClassNode classNode, ClosureExpression closureExpression) { if (closureExpression.getNodeMetaData(TRANSFORMED_MARKER) != null) return; ClassNode previousClassNode = this.currentClassNode; try { this.currentClassNode = classNode; List<String> propertyNames = AstPropertyResolveUtils.getPropertyNames(classNode); Statement code = closureExpression.getCode(); BlockStatement newCode = new BlockStatement(); boolean addAll = false; if (code instanceof BlockStatement) { BlockStatement bs = (BlockStatement) code; addBlockStatementToNewQuery(bs, newCode, addAll, propertyNames, closureExpression.getVariableScope()); newCode.setVariableScope(bs.getVariableScope()); } if (!newCode.getStatements().isEmpty()) { closureExpression.putNodeMetaData(TRANSFORMED_MARKER, Boolean.TRUE); closureExpression.setCode(newCode); } } finally { this.currentClassNode = previousClassNode; } }
@Override public void visitClosureExpression(ClosureExpression expression) { if (expression.isParameterSpecified() && (leaf instanceof Variable)) { visitParameters(expression.getParameters(), (Variable) leaf); } super.visitClosureExpression(expression); }
private ClosureAndArguments build() { currentBody = new BlockStatement(); closureExpression = new ClosureExpression(new Parameter[0], currentBody); closureExpression.setVariableScope(variableScope); closureExpression.setCode(currentBody); arguments = new ArgumentListExpression(); arguments.addExpression(closureExpression); return this; }
ASTNode scope = it.next(); if (scope instanceof ClosureExpression) { VariableScope variableScope = ((ClosureExpression) scope).getVariableScope(); if (variableScope.getDeclaredVariable(variable.getName()) != null) { return scope; Statement statement = ((ClosureExpression) scope).getCode(); if (statement instanceof BlockStatement) { variableScope = ((BlockStatement) statement).getVariableScope();
@Override public void visitClosureExpression(ClosureExpression expr) { super.visitClosureExpression(expr); AstUtil.fixUpLocalVariables(dataProcessorVars, expr.getVariableScope(), true); }
protected Statement filterStatement(Statement statement) { if (statement instanceof ExpressionStatement) { ExpressionStatement expStmt = (ExpressionStatement) statement; Expression expression = expStmt.getExpression(); if (expression instanceof ClosureExpression) { ClosureExpression closureExp = (ClosureExpression) expression; if (!closureExp.isParameterSpecified()) { return closureExp.getCode(); } } } return statement; }
if (stage.getBranches().size() == 1) { ModelASTBranch branch = stage.getBranches().get(0); body.setCode(handleBranch(branch)); } else { MethodCallExpression methExpr = getParallelMethod(body.getCode()); if (methExpr != null) { TupleExpression parallelArgs = (TupleExpression)methExpr.getArguments(); body.setCode( block( stmt(
/** * @param callArguments * @param receiver * @deprecated this method is unused, replaced with {@link DelegatesTo} inference. */ @Deprecated protected void checkClosureParameters(final Expression callArguments, final ClassNode receiver) { if (callArguments instanceof ArgumentListExpression) { ArgumentListExpression argList = (ArgumentListExpression) callArguments; ClosureExpression closure = (ClosureExpression) argList.getExpression(0); Parameter[] parameters = closure.getParameters(); if (parameters.length > 1) { addStaticTypeError("Unexpected number of parameters for a with call", argList); } else if (parameters.length == 1) { Parameter param = parameters[0]; if (!param.isDynamicTyped() && !isAssignableTo(receiver, param.getType().redirect())) { addStaticTypeError("Expected parameter type: " + receiver.toString(false) + " but was: " + param.getType().redirect().toString(false), param); } } closure.putNodeMetaData(StaticTypesMarker.DELEGATION_METADATA, new DelegationMetadata( receiver, Closure.DELEGATE_FIRST, typeCheckingContext.delegationMetadata )); } }