use of groovy.transform.ToString in project groovy-core by groovy.
the class ToStringASTTransformation method calculateToStringStatements.
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean includeSuperProperties, BlockStatement body) {
// def _result = new StringBuilder()
final Expression result = varX("_result");
body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
// def $toStringFirst = true
final VariableExpression first = varX("$toStringFirst");
body.addStatement(declS(first, constX(Boolean.TRUE)));
// <class_name>(
String className = (includePackage) ? cNode.getName() : cNode.getNameWithoutPackage();
body.addStatement(appendS(result, constX(className + "(")));
// append properties
List<PropertyNode> pList;
if (includeSuperProperties) {
pList = getAllProperties(cNode);
Iterator<PropertyNode> pIterator = pList.iterator();
while (pIterator.hasNext()) {
if (pIterator.next().isStatic()) {
pIterator.remove();
}
}
} else {
pList = getInstanceProperties(cNode);
}
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes))
continue;
Expression getter = getterX(cNode, pNode);
appendValue(body, result, first, getter, pNode.getName(), includeNames, ignoreNulls);
}
// append fields if needed
if (includeFields) {
List<FieldNode> fList = new ArrayList<FieldNode>();
fList.addAll(getInstanceNonPropertyFields(cNode));
for (FieldNode fNode : fList) {
if (shouldSkip(fNode.getName(), excludes, includes))
continue;
appendValue(body, result, first, varX(fNode), fNode.getName(), includeNames, ignoreNulls);
}
}
// append super if needed
if (includeSuper) {
appendCommaIfNotFirst(body, result, first);
appendPrefix(body, result, "super", includeNames);
// not through MOP to avoid infinite recursion
body.addStatement(appendS(result, callSuperX("toString")));
}
// wrap up
body.addStatement(appendS(result, constX(")")));
MethodCallExpression toString = callX(result, "toString");
toString.setImplicitThis(false);
return toString;
}
use of groovy.transform.ToString in project groovy by apache.
the class ToStringASTTransformation method calculateToStringStatements.
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, final List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean includeSuperProperties, boolean allProperties, BlockStatement body, boolean allNames) {
// def _result = new StringBuilder()
final Expression result = varX("_result");
body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
List<ToStringElement> elements = new ArrayList<ToStringElement>();
// def $toStringFirst = true
final VariableExpression first = varX("$toStringFirst");
body.addStatement(declS(first, constX(Boolean.TRUE)));
// <class_name>(
String className = (includePackage) ? cNode.getName() : cNode.getNameWithoutPackage();
body.addStatement(appendS(result, constX(className + "(")));
// append properties
List<PropertyNode> pList = BeanUtils.getAllProperties(cNode, includeSuperProperties, false, allProperties);
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes, allNames))
continue;
Expression getter = getterThisX(cNode, pNode);
elements.add(new ToStringElement(getter, pNode.getName(), canBeSelf(cNode, pNode.getOriginType())));
}
// append fields if needed
if (includeFields) {
List<FieldNode> fList = new ArrayList<FieldNode>();
fList.addAll(getInstanceNonPropertyFields(cNode));
for (FieldNode fNode : fList) {
if (shouldSkip(fNode.getName(), excludes, includes, allNames))
continue;
elements.add(new ToStringElement(varX(fNode), fNode.getName(), canBeSelf(cNode, fNode.getType())));
}
}
// append super if needed
if (includeSuper) {
// not through MOP to avoid infinite recursion
elements.add(new ToStringElement(callSuperX("toString"), "super", false));
}
if (includes != null) {
Comparator<ToStringElement> includeComparator = new Comparator<ToStringElement>() {
public int compare(ToStringElement tse1, ToStringElement tse2) {
return new Integer(includes.indexOf(tse1.name)).compareTo(includes.indexOf(tse2.name));
}
};
Collections.sort(elements, includeComparator);
}
for (ToStringElement el : elements) {
appendValue(body, result, first, el.value, el.name, includeNames, ignoreNulls, el.canBeSelf);
}
// wrap up
body.addStatement(appendS(result, constX(")")));
MethodCallExpression toString = callX(result, "toString");
toString.setImplicitThis(false);
return toString;
}
Aggregations