Search in sources :

Example 46 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

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 47 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class SortableASTTransformation method createSortable.

private void createSortable(AnnotationNode annotation, ClassNode classNode) {
    List<String> includes = getMemberList(annotation, "includes");
    List<String> excludes = getMemberList(annotation, "excludes");
    if (!checkIncludeExclude(annotation, excludes, includes, MY_TYPE_NAME))
        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 48 with PropertyNode

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

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorMap.

private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
    for (PropertyNode pNode : list) {
        body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
    }
    // check for missing properties
    body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
    createConstructorMapCommon(cNode, body);
    if (list.size() > 0) {
        createNoArgConstructor(cNode);
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 50 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class BindableASTTransformation method addListenerToClass.

private void addListenerToClass(SourceUnit source, ClassNode classNode) {
    if (needsPropertyChangeSupport(classNode, source)) {
        addPropertyChangeSupport(classNode);
    }
    for (PropertyNode propertyNode : classNode.getProperties()) {
        FieldNode field = propertyNode.getField();
        // look to see if per-field handlers will catch this one...
        if (hasBindableAnnotation(field) || ((field.getModifiers() & Opcodes.ACC_FINAL) != 0) || field.isStatic() || VetoableASTTransformation.hasVetoableAnnotation(field)) {
            // VetoableASTTransformation will handle both @Bindable and @Vetoable
            continue;
        }
        createListenerSetter(classNode, propertyNode);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) 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