boolean hasExistingToString = hasDeclaredMethod(cNode, "toString", 0);
if (hasExistingToString && hasDeclaredMethod(cNode, "_toString", 0)) return;
final BlockStatement body = new BlockStatement();
// def _result = new StringBuffer()
final Expression result = new VariableExpression("_result");
final Expression init = new ConstructorCallExpression(STRINGBUFFER_TYPE, MethodCallExpression.NO_ARGUMENTS);
body.addStatement(new ExpressionStatement(new DeclarationExpression(result, ASSIGN, init)));
body.addStatement(append(result, new ConstantExpression(cNode.getName())));
body.addStatement(append(result, new ConstantExpression("(")));
boolean first = true;
List<PropertyNode> pList = getInstanceProperties(cNode);
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes)) continue;
first = appendPrefix(cNode, body, result, first, pNode.getName());
String getterName = "get" + Verifier.capitalize(pNode.getName());
Expression getter = new MethodCallExpression(VariableExpression.THIS_EXPRESSION, getterName, MethodCallExpression.NO_ARGUMENTS);
body.addStatement(append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", getter)));
}
List<FieldNode> fList = new ArrayList<FieldNode>();
if (includeFields) {
fList.addAll(getInstanceNonPropertyFields(cNode));
}
for (FieldNode fNode : fList) {
if (shouldSkip(fNode.getName(), excludes, includes)) continue;
first = appendPrefix(cNode, body, result, first, fNode.getName());
body.addStatement(append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", new VariableExpression(fNode))));
}
if (includeSuper) {
appendPrefix(cNode, body, result, first, "super");
// not through MOP to avoid infinite recursion
body.addStatement(append(result, new MethodCallExpression(VariableExpression.SUPER_EXPRESSION, "toString", MethodCallExpression.NO_ARGUMENTS)));