@Override public void visitConstructorCallExpression(final ConstructorCallExpression call) { super.visitConstructorCallExpression(call); MethodNode target = (MethodNode) call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET); if (target == null && call.getLineNumber() > 0) { addError("Target constructor for constructor call expression hasn't been set", call); } else { if (target == null) { // try to find a target ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments()); List<Expression> expressions = argumentListExpression.getExpressions(); ClassNode[] args = new ClassNode[expressions.size()]; for (int i = 0; i < args.length; i++) { args[i] = typeChooser.resolveType(expressions.get(i), classNode); } MethodNode constructor = findMethodOrFail( call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args); call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, constructor); target = constructor; } } if (target != null) { memorizeInitialExpressions(target); } }
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(");"); }