Search in sources :

Example 1 with ToString

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;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ToString(groovy.transform.ToString)

Example 2 with 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;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ToString(groovy.transform.ToString) Comparator(java.util.Comparator) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Aggregations

ToString (groovy.transform.ToString)2 ArrayList (java.util.ArrayList)2 FieldNode (org.codehaus.groovy.ast.FieldNode)2 PropertyNode (org.codehaus.groovy.ast.PropertyNode)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)2 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)2 Comparator (java.util.Comparator)1