use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class NewifyASTTransformation method isNewMethodStyle.
private boolean isNewMethodStyle(MethodCallExpression mce) {
final Expression obj = mce.getObjectExpression();
final Expression meth = mce.getMethod();
return (obj instanceof ClassExpression && meth instanceof ConstantExpression && ((ConstantExpression) meth).getValue().equals("new"));
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorStatementArrayOrCloneable.
private Statement createConstructorStatementArrayOrCloneable(FieldNode fNode) {
final Expression fieldExpr = varX(fNode);
Expression initExpr = fNode.getInitialValueExpression();
ClassNode fieldType = fNode.getType();
final Expression array = findArg(fNode.getName());
final Statement assignInit;
if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
} else {
assignInit = assignS(fieldExpr, cloneArrayOrCloneableExpr(initExpr, fieldType));
}
return ifElseS(equalsNullX(array), assignInit, assignS(fieldExpr, cloneArrayOrCloneableExpr(array, fieldType)));
}
use of org.codehaus.groovy.ast.expr.ConstantExpression 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.ConstantExpression 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.ConstantExpression in project groovy-core by groovy.
the class MarkupBuilderCodeTransformer method transformMethodCall.
private Expression transformMethodCall(final MethodCallExpression exp) {
String name = exp.getMethodAsString();
if (exp.isImplicitThis() && "include".equals(name)) {
return tryTransformInclude(exp);
} else if (exp.isImplicitThis() && name.startsWith(":")) {
List<Expression> args;
if (exp.getArguments() instanceof ArgumentListExpression) {
args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
} else {
args = Collections.singletonList(exp.getArguments());
}
Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
MethodCallExpression call = new MethodCallExpression(new VariableExpression("this"), "methodMissing", newArguments);
call.setImplicitThis(true);
call.setSafe(exp.isSafe());
call.setSpreadSafe(exp.isSpreadSafe());
call.setSourcePosition(exp);
return call;
} else if (name != null && name.startsWith("$")) {
MethodCallExpression reformatted = new MethodCallExpression(exp.getObjectExpression(), name.substring(1), exp.getArguments());
reformatted.setImplicitThis(exp.isImplicitThis());
reformatted.setSafe(exp.isSafe());
reformatted.setSpreadSafe(exp.isSpreadSafe());
reformatted.setSourcePosition(exp);
// wrap in a stringOf { ... } closure call
ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
clos.setVariableScope(new VariableScope());
MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"), "stringOf", clos);
stringOf.setImplicitThis(true);
stringOf.setSourcePosition(reformatted);
return stringOf;
}
return super.transform(exp);
}
Aggregations