Java Code Examples for org.codehaus.groovy.ast.PropertyNode#getName()

The following examples show how to use org.codehaus.groovy.ast.PropertyNode#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortableASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private List<PropertyNode> findProperties(AnnotationNode annotation, final ClassNode classNode, final List<String> includes,
                                          final List<String> excludes, final boolean allProperties,
                                          final boolean includeSuperProperties, final boolean allNames) {
    Set<String> names = new HashSet<String>();
    List<PropertyNode> props = getAllProperties(names, classNode, classNode, true, false, allProperties,
            false, includeSuperProperties, false, false, allNames, false);
    List<PropertyNode> properties = new ArrayList<PropertyNode>();
    for (PropertyNode property : props) {
        String propertyName = property.getName();
        if ((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 = Comparator.comparingInt(o -> includes.indexOf(o.getName()));
        properties.sort(includeComparator);
    }
    return properties;
}
 
Example 2
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void checkDuplicateProperties(PropertyNode node) {
    ClassNode cn = node.getDeclaringClass();
    String name = node.getName();
    String getterName = "get" + capitalize(name);
    if (Character.isUpperCase(name.charAt(0))) {
        for (PropertyNode propNode : cn.getProperties()) {
            String otherName = propNode.getField().getName();
            String otherGetterName = "get" + capitalize(otherName);
            if (node != propNode && getterName.equals(otherGetterName)) {
                String msg = "The field " + name + " and " + otherName + " on the class " +
                        cn.getName() + " will result in duplicate JavaBean properties, which is not allowed";
                addError(msg, node);
            }
        }
    }
}
 
Example 3
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static String getRefDescriptor(ASTNode ref) {
    if (ref instanceof FieldNode) {
        FieldNode f = (FieldNode) ref;
        return "the field "+f.getName()+" ";
    } else if (ref instanceof PropertyNode) {
        PropertyNode p = (PropertyNode) ref;
        return "the property "+p.getName()+" ";
    } else if (ref instanceof ConstructorNode) {
        return "the constructor "+ref.getText()+" ";
    } else if (ref instanceof MethodNode) {
        return "the method "+ref.getText()+" ";
    } else if (ref instanceof ClassNode) {
        return "the super class "+ref+" ";
    }
    return "<unknown with class "+ref.getClass()+"> ";
}
 
Example 4
Source File: DefaultPropertyHandler.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) {
    String name = pNode.getName();
    FieldNode fNode = pNode.getField();
    boolean useSetters = xform.memberHasValue(anno, "useSetters", true);
    boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
    if (namedArgsMap != null) {
        return assignFieldS(useSetters, namedArgsMap, name);
    } else {
        Expression var = varX(name);
        if (useSetters && hasSetter) {
            return setViaSetterS(name, var);
        } else {
            return assignToFieldS(name, var);
        }
    }
}
 
Example 5
Source File: GroovydocVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitProperty(PropertyNode node) {
    String name = node.getName();
    SimpleGroovyFieldDoc fieldDoc = new SimpleGroovyFieldDoc(name, currentClassDoc);
    fieldDoc.setType(new SimpleGroovyType(makeType(node.getType())));
    int mods = node.getModifiers();
    if (!hasAnno(node.getField(), "PackageScope")) {
        processModifiers(fieldDoc, node.getField(), mods);
        Groovydoc groovydoc = node.getGroovydoc();
        fieldDoc.setRawCommentText(groovydoc == null ? "" : getDocContent(groovydoc));
        currentClassDoc.addProperty(fieldDoc);
    }
    processAnnotations(fieldDoc, node.getField());
    super.visitProperty(node);
}
 
Example 6
Source File: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void processProps(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyHandler handler, boolean allNames, List<String> excludes, List<String> includes, List<PropertyNode> superList, Parameter map, BlockStatement inner) {
    for (PropertyNode pNode : superList) {
        String name = pNode.getName();
        if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue;
        Statement propInit = handler.createPropInit(xform, anno, cNode, pNode, map);
        if (propInit != null) {
            inner.addStatement(propInit);
        }
    }
}
 
Example 7
Source File: SortableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Statement createCompareToMethodBody(List<PropertyNode> properties, boolean reversed) {
    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(localVarX(THIS_HASH, ClassHelper.Integer_TYPE), callX(varX("this"), "hashCode")));
        statements.add(declS(localVarX(OTHER_HASH, ClassHelper.Integer_TYPE), callX(varX(OTHER), "hashCode")));
        statements.add(returnS(compareExpr(varX(THIS_HASH), varX(OTHER_HASH), reversed)));
    } else {
        // int value = 0;
        statements.add(declS(localVarX(VALUE, ClassHelper.int_TYPE), constX(0)));
        for (PropertyNode property : properties) {
            String propName = property.getName();
            // value = this.prop <=> other.prop;
            statements.add(assignS(varX(VALUE), compareExpr(propX(varX("this"), propName), propX(varX(OTHER), propName), reversed)));
            // 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;
}
 
Example 8
Source File: SortableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Statement createCompareMethodBody(PropertyNode property, boolean reversed) {
    String propName = property.getName();
    return block(
            // if (arg0 == arg1) return 0;
            ifS(eqX(varX(ARG0), varX(ARG1)), returnS(constX(0))),
            // if (arg0 != null && arg1 == null) return -1;
            ifS(andX(notNullX(varX(ARG0)), equalsNullX(varX(ARG1))), returnS(constX(-1))),
            // if (arg0 == null && arg1 != null) return 1;
            ifS(andX(equalsNullX(varX(ARG0)), notNullX(varX(ARG1))), returnS(constX(1))),
            // return arg0.prop <=> arg1.prop;
            returnS(compareExpr(propX(varX(ARG0), propName), propX(varX(ARG1), propName), reversed))
    );
}
 
Example 9
Source File: NamedVariantASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean processDelegateParam(final MethodNode mNode, final Parameter mapParam, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam) {
    if (isInnerClass(fromParam.getType())) {
        if (mNode.isStatic()) {
            addError("Error during " + NAMED_VARIANT + " processing. Delegate type '" + fromParam.getType().getNameWithoutPackage() + "' is an inner class which is not supported.", mNode);
            return false;
        }
    }

    Set<String> names = new HashSet<>();
    List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
    for (String next : names) {
        if (hasDuplicates(mNode, propNames, next)) return false;
    }
    List<MapEntryExpression> entries = new ArrayList<>();
    for (PropertyNode pNode : props) {
        String name = pNode.getName();
        // create entry [name: __namedArgs.getOrDefault('name', initialValue)]
        Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> getDefaultExpression(pNode.getType()));
        entries.add(entryX(constX(name), callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue))));
        // create annotation @NamedParam(value='name', type=DelegateType)
        AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
        namedParam.addMember("value", constX(name));
        namedParam.addMember("type", classX(pNode.getType()));
        mapParam.addAnnotation(namedParam);
    }
    Expression delegateMap = mapX(entries);
    args.addExpression(castX(fromParam.getType(), delegateMap));
    return true;
}
 
Example 10
Source File: ToStringASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, boolean includeSuperFields, 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 = localVarX("_result");
    body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
    List<ToStringElement> elements = new ArrayList<ToStringElement>();

    // def $toStringFirst = true
    final VariableExpression first = localVarX("$toStringFirst");
    body.addStatement(declS(first, constX(Boolean.TRUE)));

    // <class_name>(
    String className = (includePackage) ? cNode.getName() : cNode.getNameWithoutPackage();
    body.addStatement(appendS(result, constX(className + "(")));

    Set<String> names = new HashSet<String>();
    List<PropertyNode> superList;
    if (includeSuperProperties || includeSuperFields) {
        superList = getAllProperties(names, cNode, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, allProperties, false, true, true, true, allNames, false);
    } else {
        superList = new ArrayList<PropertyNode>();
    }
    List<PropertyNode> list = getAllProperties(names, cNode, cNode,true, includeFields, allProperties, false, false, true, false, allNames, false);
    list.addAll(superList);

    for (PropertyNode pNode : list) {
        String name = pNode.getName();
        if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue;
        FieldNode fNode = pNode.getField();
        if (!cNode.hasProperty(name) && fNode.getDeclaringClass() != null) {
            // it's really just a field
            elements.add(new ToStringElement(varX(fNode), name, canBeSelf(cNode, fNode.getType())));
        } else {
            Expression getter = getterThisX(cNode, pNode);
            elements.add(new ToStringElement(getter, name, canBeSelf(cNode, pNode.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 = Comparator.comparingInt(tse -> includes.indexOf(tse.name));
        elements.sort(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;
}