use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method visitClosureExpression.
@Override
public void visitClosureExpression(final ClosureExpression expression) {
boolean oldStaticContext = typeCheckingContext.isInStaticContext;
typeCheckingContext.isInStaticContext = false;
// collect every variable expression used in the loop body
final Map<VariableExpression, ClassNode> varOrigType = new HashMap<VariableExpression, ClassNode>();
Statement code = expression.getCode();
code.visit(new VariableExpressionTypeMemoizer(varOrigType));
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
// first, collect closure shared variables and reinitialize types
SharedVariableCollector collector = new SharedVariableCollector(getSourceUnit());
collector.visitClosureExpression(expression);
Set<VariableExpression> closureSharedExpressions = collector.getClosureSharedExpressions();
Map<VariableExpression, ListHashMap> typesBeforeVisit = null;
if (!closureSharedExpressions.isEmpty()) {
typesBeforeVisit = new HashMap<VariableExpression, ListHashMap>();
saveVariableExpressionMetadata(closureSharedExpressions, typesBeforeVisit);
}
// perform visit
typeCheckingContext.pushEnclosingClosureExpression(expression);
DelegationMetadata dmd = getDelegationMetadata(expression);
if (dmd == null) {
typeCheckingContext.delegationMetadata = new DelegationMetadata(typeCheckingContext.getEnclosingClassNode(), Closure.OWNER_FIRST, typeCheckingContext.delegationMetadata);
} else {
typeCheckingContext.delegationMetadata = new DelegationMetadata(dmd.getType(), dmd.getStrategy(), typeCheckingContext.delegationMetadata);
}
super.visitClosureExpression(expression);
typeCheckingContext.delegationMetadata = typeCheckingContext.delegationMetadata.getParent();
MethodNode node = new MethodNode("dummy", 0, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
returnAdder.visitMethod(node);
TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
if (!enclosingClosure.getReturnTypes().isEmpty()) {
ClassNode returnType = lowestUpperBound(enclosingClosure.getReturnTypes());
storeInferredReturnType(expression, returnType);
ClassNode inferredType = wrapClosureType(returnType);
storeType(enclosingClosure.getClosureExpression(), inferredType);
}
typeCheckingContext.popEnclosingClosure();
boolean typeChanged = isSecondPassNeededForControlStructure(varOrigType, oldTracker);
if (typeChanged)
visitClosureExpression(expression);
// restore original metadata
restoreVariableExpressionMetadata(typesBeforeVisit);
typeCheckingContext.isInStaticContext = oldStaticContext;
Parameter[] parameters = expression.getParameters();
if (parameters != null) {
for (Parameter parameter : parameters) {
typeCheckingContext.controlStructureVariables.remove(parameter);
}
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.
the class SynchronizedASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
String value = getMemberStringValue(node, "value");
if (parent instanceof MethodNode) {
MethodNode mNode = (MethodNode) parent;
if (mNode.isAbstract()) {
addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
return;
}
ClassNode cNode = mNode.getDeclaringClass();
String lockExpr = determineLock(value, cNode, mNode);
if (lockExpr == null)
return;
Statement origCode = mNode.getCode();
Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
mNode.setCode(newCode);
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.
the class ToStringASTTransformation method appendValue.
private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls) {
final BlockStatement thenBlock = new BlockStatement();
final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock;
appendCommaIfNotFirst(thenBlock, result, first);
appendPrefix(thenBlock, result, name, includeNames);
thenBlock.addStatement(ifElseS(sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value))));
body.addStatement(appendValue);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.
the class TupleConstructorASTTransformation method processArgsBlock.
private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
BlockStatement block = new BlockStatement();
for (PropertyNode pNode : cNode.getProperties()) {
if (pNode.isStatic())
continue;
// if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
Statement ifStatement = ifS(callX(namedArgs, "containsKey", constX(pNode.getName())), assignS(varX(pNode), propX(namedArgs, pNode.getName())));
block.addStatement(ifStatement);
}
block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
return block;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.
the class GeneralUtils method block.
public static BlockStatement block(VariableScope varScope, Statement... stmts) {
BlockStatement block = new BlockStatement();
block.setVariableScope(varScope);
for (Statement stmt : stmts) block.addStatement(stmt);
return block;
}
Aggregations