use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class DefaultStrategy method initializeInstance.
private static Expression initializeInstance(ClassNode buildee, List<FieldNode> fields, BlockStatement body) {
Expression instance = varX("_the" + buildee.getNameWithoutPackage(), buildee);
body.addStatement(declS(instance, ctorX(buildee)));
for (FieldNode field : fields) {
body.addStatement(stmt(assignX(propX(instance, field.getName()), varX(field))));
}
return instance;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class ExternalStrategy method initializeInstance.
private static Expression initializeInstance(ClassNode sourceClass, List<PropertyInfo> props, BlockStatement body) {
Expression instance = varX("_the" + sourceClass.getNameWithoutPackage(), sourceClass);
body.addStatement(declS(instance, ctorX(sourceClass)));
for (PropertyInfo prop : props) {
body.addStatement(stmt(assignX(propX(instance, prop.getName()), varX(prop.getName().equals("class") ? "clazz" : prop.getName(), newClass(prop.getType())))));
}
return instance;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class ExternalStrategy method createBuildMethod.
private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<PropertyInfo> fields) {
String buildMethodName = transform.getMemberStringValue(anno, "buildMethodName", "build");
final BlockStatement body = new BlockStatement();
Expression sourceClassInstance = initializeInstance(sourceClass, fields, body);
body.addStatement(returnS(sourceClassInstance));
return new MethodNode(buildMethodName, ACC_PUBLIC, sourceClass, NO_PARAMS, NO_EXCEPTIONS, body);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class ConstructorCallTransformer method transformConstructorCall.
Expression transformConstructorCall(final ConstructorCallExpression expr) {
ConstructorNode node = (ConstructorNode) expr.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
if (node == null)
return expr;
if (node.getParameters().length == 1 && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(node.getParameters()[0].getType(), ClassHelper.MAP_TYPE) && node.getCode() == StaticTypeCheckingVisitor.GENERATED_EMPTY_STATEMENT) {
Expression arguments = expr.getArguments();
if (arguments instanceof TupleExpression) {
TupleExpression tupleExpression = (TupleExpression) arguments;
List<Expression> expressions = tupleExpression.getExpressions();
if (expressions.size() == 1) {
Expression expression = expressions.get(0);
if (expression instanceof MapExpression) {
MapExpression map = (MapExpression) expression;
// check that the node doesn't belong to the list of declared constructors
ClassNode declaringClass = node.getDeclaringClass();
for (ConstructorNode constructorNode : declaringClass.getDeclaredConstructors()) {
if (constructorNode == node) {
return staticCompilationTransformer.superTransform(expr);
}
}
// replace this call with a call to <init>() + appropriate setters
// for example, foo(x:1, y:2) is replaced with:
// { def tmp = new Foo(); tmp.x = 1; tmp.y = 2; return tmp }()
MapStyleConstructorCall result = new MapStyleConstructorCall(staticCompilationTransformer, declaringClass, map, expr);
return result;
}
}
}
}
return staticCompilationTransformer.superTransform(expr);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class ListExpressionTransformer method transformRegularConstructor.
private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
// can be replaced with a direct constructor call
List<Expression> transformedArgs = transformArguments(expr);
ConstructorCallExpression cce = new ConstructorCallExpression(target.getDeclaringClass(), new ArgumentListExpression(transformedArgs));
cce.setSourcePosition(expr);
cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
return cce;
}
Aggregations