use of org.codehaus.groovy.ast.expr.ConstructorCallExpression 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.ConstructorCallExpression 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;
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy-core by groovy.
the class RangeExpressionTransformer method transformRangeExpression.
public Expression transformRangeExpression(RangeExpression range) {
final ClassNode inferred = range.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (INTRANGE_TYPE.equals(inferred)) {
ArgumentListExpression bounds = new ArgumentListExpression(new ConstantExpression(range.isInclusive(), true), range.getFrom(), range.getTo());
ConstructorCallExpression cce = new ConstructorCallExpression(INTRANGE_TYPE, bounds);
cce.setSourcePosition(range);
cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, INTRANGE_CTOR);
cce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, INTRANGE_TYPE);
return transformer.transform(cce);
}
return transformer.superTransform(range);
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression 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.ConstructorCallExpression in project groovy-core by groovy.
the class JavaStubGenerator method printConstructor.
private void printConstructor(PrintWriter out, ClassNode clazz, ConstructorNode constructorNode) {
printAnnotations(out, constructorNode);
// printModifiers(out, constructorNode.getModifiers());
// temporary hack
out.print("public ");
String className = clazz.getNameWithoutPackage();
if (clazz instanceof InnerClassNode)
className = className.substring(className.lastIndexOf("$") + 1);
out.println(className);
printParams(out, constructorNode);
ConstructorCallExpression constrCall = getConstructorCallExpression(constructorNode);
if (constrCall == null || !constrCall.isSpecialCall()) {
out.println(" {}");
} else {
out.println(" {");
printSpecialConstructorArgs(out, constructorNode, constrCall);
out.println("}");
}
}
Aggregations