use of org.codehaus.groovy.ast.stmt.EmptyStatement in project groovy-core by groovy.
the class FinalVariableAnalyzer method visitIfElse.
@Override
public void visitIfElse(final IfStatement ifElse) {
visitStatement(ifElse);
ifElse.getBooleanExpression().visit(this);
Map<Variable, VariableState> ifState = pushState();
ifElse.getIfBlock().visit(this);
popState();
Statement elseBlock = ifElse.getElseBlock();
Map<Variable, VariableState> elseState = pushState();
if (elseBlock instanceof EmptyStatement) {
// dispatching to EmptyStatement will not call back visitor,
// must call our visitEmptyStatement explicitly
visitEmptyStatement((EmptyStatement) elseBlock);
} else {
elseBlock.visit(this);
}
popState();
// merge if/else branches
Map<Variable, VariableState> curState = getState();
Set<Variable> allVars = new HashSet<Variable>();
allVars.addAll(curState.keySet());
allVars.addAll(ifState.keySet());
allVars.addAll(elseState.keySet());
for (Variable var : allVars) {
VariableState beforeValue = curState.get(var);
VariableState ifValue = ifState.get(var);
VariableState elseValue = elseState.get(var);
// merge if and else values
VariableState mergedIfElse;
mergedIfElse = ifValue != null && elseValue != null && ifValue.isFinal && elseValue.isFinal ? VariableState.is_final : VariableState.is_var;
if (beforeValue == null) {
curState.put(var, mergedIfElse);
} else {
if (beforeValue == VariableState.is_uninitialized) {
curState.put(var, mergedIfElse);
} else if (ifValue != null || elseValue != null) {
curState.put(var, VariableState.is_var);
}
}
}
}
use of org.codehaus.groovy.ast.stmt.EmptyStatement in project groovy by apache.
the class TupleConstructorASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(anno.getClassNode()))
return;
if (parent instanceof ClassNode) {
ClassNode cNode = (ClassNode) parent;
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
boolean includeFields = memberHasValue(anno, "includeFields", true);
boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
boolean callSuper = memberHasValue(anno, "callSuper", true);
boolean force = memberHasValue(anno, "force", true);
boolean defaults = !memberHasValue(anno, "defaults", false);
boolean useSetters = memberHasValue(anno, "useSetters", true);
List<String> excludes = getMemberStringList(anno, "excludes");
List<String> includes = getMemberStringList(anno, "includes");
boolean allNames = memberHasValue(anno, "allNames", true);
if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
return;
if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, false, includeSuperFields))
return;
if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, false, includeSuperFields))
return;
// if @Immutable is found, let it pick up options and do work so we'll skip
if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE))
return;
Expression pre = anno.getMember("pre");
if (pre != null && !(pre instanceof ClosureExpression)) {
addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode);
return;
}
Expression post = anno.getMember("post");
if (post != null && !(post instanceof ClosureExpression)) {
addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode);
return;
}
createConstructor(this, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters, defaults, allNames, sourceUnit, (ClosureExpression) pre, (ClosureExpression) post);
if (pre != null) {
anno.setMember("pre", new ClosureExpression(new Parameter[0], new EmptyStatement()));
}
if (post != null) {
anno.setMember("post", new ClosureExpression(new Parameter[0], new EmptyStatement()));
}
}
}
use of org.codehaus.groovy.ast.stmt.EmptyStatement in project gradle by gradle.
the class RulesVisitor method visitMethodCallExpression.
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
ClosureExpression closureExpression = AstUtils.getSingleClosureArg(call);
if (closureExpression != null) {
// path { ... }
rewriteAction(call, extractModelPathFromMethodTarget(call), closureExpression, RuleVisitor.displayName(call));
return;
}
Pair<ClassExpression, ClosureExpression> args = AstUtils.getClassAndClosureArgs(call);
if (args != null) {
// path(Type) { ... }
rewriteCreator(call, extractModelPathFromMethodTarget(call), args.getRight(), args.getLeft(), RuleVisitor.displayName(call));
return;
}
ClassExpression classArg = AstUtils.getClassArg(call);
if (classArg != null) {
// path(Type)
String displayName = RuleVisitor.displayName(call);
List<Statement> statements = Lists.newLinkedList();
statements.add(new EmptyStatement());
BlockStatement block = new BlockStatement(statements, new VariableScope());
closureExpression = new ClosureExpression(Parameter.EMPTY_ARRAY, block);
closureExpression.setVariableScope(block.getVariableScope());
String modelPath = extractModelPathFromMethodTarget(call);
rewriteCreator(call, modelPath, closureExpression, classArg, displayName);
return;
}
restrict(call, INVALID_RULE_SIGNATURE);
}
Aggregations