Search in sources :

Example 51 with PropertyNode

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

the class BindableASTTransformation method addListenerToProperty.

private void addListenerToProperty(SourceUnit source, AnnotationNode node, ClassNode declaringClass, FieldNode field) {
    String fieldName = field.getName();
    for (PropertyNode propertyNode : declaringClass.getProperties()) {
        if (propertyNode.getName().equals(fieldName)) {
            if (field.isStatic()) {
                //noinspection ThrowableInstanceNeverThrown
                source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable cannot annotate a static property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
            } else {
                if (needsPropertyChangeSupport(declaringClass, source)) {
                    addPropertyChangeSupport(declaringClass);
                }
                createListenerSetter(declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) PropertyNode(org.codehaus.groovy.ast.PropertyNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 52 with PropertyNode

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

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)

Example 53 with PropertyNode

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

the class GeneralUtils method getAllProperties.

public static List<PropertyNode> getAllProperties(ClassNode type) {
    ClassNode node = type;
    List<PropertyNode> result = new ArrayList<PropertyNode>();
    while (node != null) {
        result.addAll(node.getProperties());
        node = node.getSuperClass();
    }
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 54 with PropertyNode

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

the class BeanUtils method addPseudoProperties.

private static void addPseudoProperties(ClassNode cNode, List<PropertyNode> result, Set<String> names, boolean includeStatic, boolean includePseudoGetters, boolean includeSuperProperties) {
    if (!includePseudoGetters)
        return;
    List<MethodNode> methods = cNode.getAllDeclaredMethods();
    ClassNode node = cNode.getSuperClass();
    if (includeSuperProperties) {
        while (node != null) {
            for (MethodNode next : node.getAllDeclaredMethods()) {
                if (!next.isPrivate()) {
                    methods.add(next);
                }
            }
            node = node.getSuperClass();
        }
    }
    for (MethodNode mNode : methods) {
        if (!includeStatic && mNode.isStatic())
            continue;
        String name = mNode.getName();
        if ((name.length() <= 3 && !name.startsWith(IS_PREFIX)) || name.equals("getClass") || name.equals("getMetaClass") || name.equals("getDeclaringClass")) {
            // Optimization: skip invalid propertyNames
            continue;
        }
        if (mNode.getDeclaringClass() != cNode && mNode.isPrivate()) {
            // skip private super methods
            continue;
        }
        int paramCount = mNode.getParameters().length;
        ClassNode returnType = mNode.getReturnType();
        if (paramCount == 0) {
            if (name.startsWith(GET_PREFIX)) {
                // Simple getter
                String propName = decapitalize(name.substring(3));
                if (!names.contains(propName)) {
                    result.add(new PropertyNode(propName, mNode.getModifiers(), returnType, cNode, null, mNode.getCode(), null));
                    names.add(propName);
                }
            } else {
                if (name.startsWith(IS_PREFIX) && returnType.equals(ClassHelper.boolean_TYPE)) {
                    // boolean getter
                    String propName = decapitalize(name.substring(2));
                    if (!names.contains(propName)) {
                        names.add(propName);
                        result.add(new PropertyNode(propName, mNode.getModifiers(), returnType, cNode, null, mNode.getCode(), null));
                    }
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 55 with PropertyNode

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

the class BeanUtils method getAllProperties.

/**
     * Get all properties including JavaBean pseudo properties matching getter conventions.
     *
     * @param type the ClassNode
     * @param includeSuperProperties whether to include super properties
     * @param includeStatic whether to include static properties
     * @param includePseudoGetters whether to include JavaBean pseudo (getXXX/isYYY) properties with no corresponding field
     * @return the list of found property nodes
     */
public static List<PropertyNode> getAllProperties(ClassNode type, boolean includeSuperProperties, boolean includeStatic, boolean includePseudoGetters) {
    // TODO add generics support so this can be used for @EAHC
    // TODO add an includePseudoSetters so this can be used for @TupleConstructor
    ClassNode node = type;
    List<PropertyNode> result = new ArrayList<PropertyNode>();
    Set<String> names = new HashSet<String>();
    while (node != null) {
        addExplicitProperties(node, result, names, includeStatic);
        if (!includeSuperProperties)
            break;
        node = node.getSuperClass();
    }
    addPseudoProperties(type, result, names, includeStatic, includePseudoGetters, includeSuperProperties);
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

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