use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.
the class InnerClassVisitor method insertThis0ToSuperCall.
private void insertThis0ToSuperCall(final ConstructorCallExpression call, final ClassNode cn) {
// calculate outer class which we need for this$0
ClassNode parent = classNode;
int level = 0;
for (; parent != null && parent != cn.getOuterClass(); parent = parent.getOuterClass()) {
level++;
}
// if constructor call is not in outer class, don't pass 'this' implicitly. Return.
if (parent == null)
return;
//add this parameter to node
Expression argsExp = call.getArguments();
if (argsExp instanceof TupleExpression) {
TupleExpression argsListExp = (TupleExpression) argsExp;
Expression this0 = VariableExpression.THIS_EXPRESSION;
for (int i = 0; i != level; ++i) this0 = new PropertyExpression(this0, "this$0");
argsListExp.getExpressions().add(0, this0);
}
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.
the class StaticImportVisitor method transformConstructorCallExpression.
protected Expression transformConstructorCallExpression(ConstructorCallExpression cce) {
inSpecialConstructorCall = cce.isSpecialCall();
Expression expression = cce.getArguments();
if (expression instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) expression;
if (tuple.getExpressions().size() == 1) {
expression = tuple.getExpression(0);
if (expression instanceof NamedArgumentListExpression) {
NamedArgumentListExpression namedArgs = (NamedArgumentListExpression) expression;
List<MapEntryExpression> entryExpressions = namedArgs.getMapEntryExpressions();
for (int i = 0; i < entryExpressions.size(); i++) {
entryExpressions.set(i, (MapEntryExpression) transformMapEntryExpression(entryExpressions.get(i), cce.getType()));
}
}
}
}
Expression ret = cce.transformExpression(this);
inSpecialConstructorCall = false;
return ret;
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.
the class StaticImportVisitor method transform.
public Expression transform(Expression exp) {
if (exp == null)
return null;
if (exp.getClass() == VariableExpression.class) {
return transformVariableExpression((VariableExpression) exp);
}
if (exp.getClass() == BinaryExpression.class) {
return transformBinaryExpression((BinaryExpression) exp);
}
if (exp.getClass() == PropertyExpression.class) {
return transformPropertyExpression((PropertyExpression) exp);
}
if (exp.getClass() == MethodCallExpression.class) {
return transformMethodCallExpression((MethodCallExpression) exp);
}
if (exp.getClass() == ClosureExpression.class) {
return transformClosureExpression((ClosureExpression) exp);
}
if (exp.getClass() == ConstructorCallExpression.class) {
return transformConstructorCallExpression((ConstructorCallExpression) exp);
}
if (exp.getClass() == ArgumentListExpression.class) {
Expression result = exp.transformExpression(this);
if (inPropertyExpression) {
foundArgs = result;
}
return result;
}
if (exp instanceof ConstantExpression) {
Expression result = exp.transformExpression(this);
if (inPropertyExpression) {
foundConstant = result;
}
if (inAnnotation && exp instanceof AnnotationConstantExpression) {
ConstantExpression ce = (ConstantExpression) result;
if (ce.getValue() instanceof AnnotationNode) {
// replicate a little bit of AnnotationVisitor here
// because we can't wait until later to do this
AnnotationNode an = (AnnotationNode) ce.getValue();
Map<String, Expression> attributes = an.getMembers();
for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
Expression attrExpr = transform(entry.getValue());
entry.setValue(attrExpr);
}
}
}
return result;
}
return exp.transformExpression(this);
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.
the class JavaStubGenerator method printSpecialConstructorArgs.
private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) {
// Select a constructor from our class, or super-class which is legal to call,
// then write out an invoke w/nulls using casts to avoid ambiguous crapo
Parameter[] params = selectAccessibleConstructorFromSuper(node);
if (params != null) {
out.print("super (");
for (int i = 0; i < params.length; i++) {
printDefaultValue(out, params[i].getType());
if (i + 1 < params.length) {
out.print(", ");
}
}
out.println(");");
return;
}
// Otherwise try the older method based on the constructor's call expression
Expression arguments = constrCall.getArguments();
if (constrCall.isSuperCall()) {
out.print("super(");
} else {
out.print("this(");
}
// Else try to render some arguments
if (arguments instanceof ArgumentListExpression) {
ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments;
List<Expression> args = argumentListExpression.getExpressions();
for (Expression arg : args) {
if (arg instanceof ConstantExpression) {
ConstantExpression expression = (ConstantExpression) arg;
Object o = expression.getValue();
if (o instanceof String) {
out.print("(String)null");
} else {
out.print(expression.getText());
}
} else {
ClassNode type = getConstructorArgumentType(arg, node);
printDefaultValue(out, type);
}
if (arg != args.get(args.size() - 1)) {
out.print(", ");
}
}
}
out.println(");");
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.
the class JavaStubGenerator method getConstructorCallExpression.
private static ConstructorCallExpression getConstructorCallExpression(ConstructorNode constructorNode) {
Statement code = constructorNode.getCode();
if (!(code instanceof BlockStatement))
return null;
BlockStatement block = (BlockStatement) code;
List stats = block.getStatements();
if (stats == null || stats.isEmpty())
return null;
Statement stat = (Statement) stats.get(0);
if (!(stat instanceof ExpressionStatement))
return null;
Expression expr = ((ExpressionStatement) stat).getExpression();
if (!(expr instanceof ConstructorCallExpression))
return null;
return (ConstructorCallExpression) expr;
}
Aggregations