use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorStatementMapSpecial.
private static Statement createConstructorStatementMapSpecial(FieldNode fNode) {
final Expression fieldExpr = varX(fNode);
final ClassNode fieldType = fieldExpr.getType();
final Expression initExpr = fNode.getInitialValueExpression();
final Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
} else {
assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
}
Expression namedArgs = findArg(fNode.getName());
Expression baseArgs = varX("args");
return ifElseS(equalsNullX(baseArgs), assignInit, ifElseS(equalsNullX(namedArgs), ifElseS(isTrueX(callX(baseArgs, "containsKey", constX(fNode.getName()))), assignS(fieldExpr, namedArgs), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType))), ifElseS(isOneX(callX(baseArgs, "size")), assignS(fieldExpr, cloneCollectionExpr(namedArgs, fieldType)), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType)))));
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorStatementCollection.
private static Statement createConstructorStatementCollection(FieldNode fNode) {
final Expression fieldExpr = varX(fNode);
ClassNode fieldType = fieldExpr.getType();
Expression initExpr = fNode.getInitialValueExpression();
final Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
} else {
assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
}
Expression collection = findArg(fNode.getName());
return ifElseS(equalsNullX(collection), assignInit, ifElseS(isInstanceOfX(collection, CLONEABLE_TYPE), assignS(fieldExpr, cloneCollectionExpr(cloneArrayOrCloneableExpr(collection, fieldType), fieldType)), assignS(fieldExpr, cloneCollectionExpr(collection, fieldType))));
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
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 by apache.
the class ImmutableASTTransformation method createConstructorStatement.
private Statement createConstructorStatement(ClassNode cNode, PropertyNode pNode, List<String> knownImmutableClasses, List<String> knownImmutables) {
FieldNode fNode = pNode.getField();
final ClassNode fieldType = fNode.getType();
Statement statement = null;
if (fieldType.isArray() || isOrImplements(fieldType, CLONEABLE_TYPE)) {
statement = createConstructorStatementArrayOrCloneable(fNode);
} else if (isKnownImmutableClass(fieldType, knownImmutableClasses) || isKnownImmutable(pNode.getName(), knownImmutables)) {
statement = createConstructorStatementDefault(fNode);
} else if (fieldType.isDerivedFrom(DATE_TYPE)) {
statement = createConstructorStatementDate(fNode);
} else if (isOrImplements(fieldType, COLLECTION_TYPE) || fieldType.isDerivedFrom(COLLECTION_TYPE) || isOrImplements(fieldType, MAP_TYPE) || fieldType.isDerivedFrom(MAP_TYPE)) {
statement = createConstructorStatementCollection(fNode);
} else if (fieldType.isResolved()) {
addError(createErrorMessage(cNode.getName(), fNode.getName(), fieldType.getName(), "compiling"), fNode);
statement = EmptyStatement.INSTANCE;
} else {
statement = createConstructorStatementGuarded(cNode, fNode);
}
return statement;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AutoNewLineTransformer method visitClosureExpression.
@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);
}
}
Aggregations