use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorStatementCollection.
private 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.expr.Expression in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorStatementMapSpecial.
private 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.expr.Expression in project groovy-core by groovy.
the class JavaAwareResolveVisitor method getConstructorCall.
private static Expression getConstructorCall(Statement code) {
if (code == null)
return null;
if (code instanceof BlockStatement) {
BlockStatement bs = (BlockStatement) code;
if (bs.isEmpty())
return null;
return getConstructorCall(bs.getStatements().get(0));
}
if (!(code instanceof ExpressionStatement))
return null;
ExpressionStatement es = (ExpressionStatement) code;
Expression exp = es.getExpression();
if (!(exp instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) exp;
if (!cce.isSpecialCall())
return null;
return cce;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class JavaAwareResolveVisitor method visitConstructor.
@Override
public void visitConstructor(ConstructorNode node) {
super.visitConstructor(node);
Statement code = node.getCode();
Expression cce = getConstructorCall(code);
if (cce == null)
return;
cce.visit(this);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class PackageScopeASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
return;
Expression value = node.getMember("value");
if (parent instanceof ClassNode) {
List<groovy.transform.PackageScopeTarget> targets;
if (value == null)
targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
else
targets = determineTargets(value);
visitClassNode((ClassNode) parent, targets);
parent.getAnnotations();
} else {
if (value != null) {
addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
return;
}
if (parent instanceof MethodNode) {
visitMethodNode((MethodNode) parent);
} else if (parent instanceof FieldNode) {
visitFieldNode((FieldNode) parent);
}
}
}
Aggregations