Search in sources :

Example 61 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.

the class PackageScopeASTTransformation method visitFieldNode.

private static void visitFieldNode(FieldNode fNode) {
    final ClassNode cNode = fNode.getDeclaringClass();
    final List<PropertyNode> pList = cNode.getProperties();
    PropertyNode foundProp = null;
    for (PropertyNode pNode : pList) {
        if (pNode.getName().equals(fNode.getName())) {
            foundProp = pNode;
            break;
        }
    }
    if (foundProp != null) {
        revertVisibility(fNode);
        pList.remove(foundProp);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 62 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.

the class SortableASTTransformation method createCompareToMethodBody.

private static Statement createCompareToMethodBody(List<PropertyNode> properties) {
    List<Statement> statements = new ArrayList<Statement>();
    // if (this.is(other)) return 0;
    statements.add(ifS(callThisX("is", args(OTHER)), returnS(constX(0))));
    if (properties.isEmpty()) {
        // perhaps overkill but let compareTo be based on hashes for commutativity
        // return this.hashCode() <=> other.hashCode()
        statements.add(declS(varX(THIS_HASH, ClassHelper.Integer_TYPE), callX(varX("this"), "hashCode")));
        statements.add(declS(varX(OTHER_HASH, ClassHelper.Integer_TYPE), callX(varX(OTHER), "hashCode")));
        statements.add(returnS(cmpX(varX(THIS_HASH), varX(OTHER_HASH))));
    } else {
        // int value = 0;
        statements.add(declS(varX(VALUE, ClassHelper.int_TYPE), constX(0)));
        for (PropertyNode property : properties) {
            String propName = property.getName();
            // value = this.prop <=> other.prop;
            statements.add(assignS(varX(VALUE), cmpX(propX(varX("this"), propName), propX(varX(OTHER), propName))));
            // if (value != 0) return value;
            statements.add(ifS(neX(varX(VALUE), constX(0)), returnS(varX(VALUE))));
        }
        // objects are equal
        statements.add(returnS(constX(0)));
    }
    final BlockStatement body = new BlockStatement();
    body.addStatements(statements);
    return body;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 63 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.

the class SortableASTTransformation method findProperties.

private List<PropertyNode> findProperties(AnnotationNode annotation, ClassNode classNode, final List<String> includes, final List<String> excludes) {
    List<PropertyNode> properties = new ArrayList<PropertyNode>();
    for (PropertyNode property : classNode.getProperties()) {
        String propertyName = property.getName();
        if (property.isStatic() || (excludes != null && excludes.contains(propertyName)) || includes != null && !includes.contains(propertyName))
            continue;
        properties.add(property);
    }
    for (PropertyNode pNode : properties) {
        checkComparable(pNode);
    }
    if (includes != null) {
        Comparator<PropertyNode> includeComparator = new Comparator<PropertyNode>() {

            public int compare(PropertyNode o1, PropertyNode o2) {
                return new Integer(includes.indexOf(o1.getName())).compareTo(includes.indexOf(o2.getName()));
            }
        };
        Collections.sort(properties, includeComparator);
    }
    return properties;
}
Also used : PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) AbstractComparator(org.codehaus.groovy.runtime.AbstractComparator) Comparator(java.util.Comparator)

Example 64 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.

the class SortableASTTransformation method createSortable.

private void createSortable(AnnotationNode annotation, ClassNode classNode) {
    List<String> includes = getMemberStringList(annotation, "includes");
    List<String> excludes = getMemberStringList(annotation, "excludes");
    if (!checkIncludeExcludeUndefinedAware(annotation, excludes, includes, MY_TYPE_NAME))
        return;
    if (!checkPropertyList(classNode, includes, "includes", annotation, MY_TYPE_NAME, false))
        return;
    if (!checkPropertyList(classNode, excludes, "excludes", annotation, MY_TYPE_NAME, false))
        return;
    if (classNode.isInterface()) {
        addError(MY_TYPE_NAME + " cannot be applied to interface " + classNode.getName(), annotation);
    }
    List<PropertyNode> properties = findProperties(annotation, classNode, includes, excludes);
    implementComparable(classNode);
    classNode.addMethod(new MethodNode("compareTo", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), OTHER)), ClassNode.EMPTY_ARRAY, createCompareToMethodBody(properties)));
    for (PropertyNode property : properties) {
        createComparatorFor(classNode, property);
    }
    new VariableScopeVisitor(sourceUnit, true).visitClass(classNode);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 65 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode 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

PropertyNode (org.codehaus.groovy.ast.PropertyNode)71 ClassNode (org.codehaus.groovy.ast.ClassNode)36 FieldNode (org.codehaus.groovy.ast.FieldNode)30 ArrayList (java.util.ArrayList)25 MethodNode (org.codehaus.groovy.ast.MethodNode)19 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)12 Parameter (org.codehaus.groovy.ast.Parameter)11 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)10 Expression (org.codehaus.groovy.ast.expr.Expression)10 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)8 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)7 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)6 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)6 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)6 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)6 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)6 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)6 HashSet (java.util.HashSet)5