Java Code Examples for org.codehaus.groovy.ast.ClassNode#getProperties()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getProperties() . 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: VetoableASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void addListenerToProperty(SourceUnit source, AnnotationNode node, AnnotatedNode parent) {
    ClassNode declaringClass = parent.getDeclaringClass();
    FieldNode field = ((FieldNode) parent);
    String fieldName = field.getName();
    for (PropertyNode propertyNode : declaringClass.getProperties()) {
        boolean bindable = BindableASTTransformation.hasBindableAnnotation(parent)
                || BindableASTTransformation.hasBindableAnnotation(parent.getDeclaringClass());

        if (propertyNode.getName().equals(fieldName)) {
            if (field.isStatic()) {
                //noinspection ThrowableInstanceNeverThrown
                source.getErrorCollector().addErrorAndContinue("@groovy.beans.Vetoable cannot annotate a static property.", node, source);
            } else {
                createListenerSetter(source, bindable, declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue("@groovy.beans.Vetoable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node, source);
}
 
Example 2
Source File: BindableASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
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))
        {
            // explicitly labeled properties are already handled,
            // don't transform final properties
            // don't transform static properties
            // VetoableASTTransformation will handle both @Bindable and @Vetoable
            continue;
        }
        createListenerSetter(classNode, propertyNode);
    }
}
 
Example 3
Source File: CategoryASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static boolean ensureNoInstanceFieldOrProperty(final SourceUnit source, final ClassNode parent) {
    boolean valid = true;
    for (FieldNode fieldNode : parent.getFields()) {
        if (!fieldNode.isStatic() && fieldNode.getLineNumber()>0) {
            // if <0, probably an AST transform or internal code (like generated metaclass field, ...)
            addUnsupportedError(fieldNode,  source);
            valid = false;
        }
    }
    for (PropertyNode propertyNode : parent.getProperties()) {
        if (!propertyNode.isStatic() && propertyNode.getLineNumber()>0) {
            // if <0, probably an AST transform or internal code (like generated metaclass field, ...)
            addUnsupportedError(propertyNode, source);
            valid = false;
        }
    }
    return valid;
}
 
Example 4
Source File: BindableASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
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("@groovy.beans.Bindable cannot annotate a static property.", node, source);
            } else {
                if (needsPropertyChangeSupport(declaringClass, source)) {
                    addPropertyChangeSupport(declaringClass);
                }
                createListenerSetter(declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue("@groovy.beans.Bindable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node, source);
}
 
Example 5
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 6
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static List<FieldNode> getSuperPropertyFields(final ClassNode cNode) {
    List<FieldNode> result;
    if (cNode == ClassHelper.OBJECT_TYPE) {
        result = new ArrayList<>();
    } else {
        result = getSuperPropertyFields(cNode.getSuperClass());
    }
    for (PropertyNode pNode : cNode.getProperties()) {
        if (!pNode.isStatic()) {
            result.add(pNode.getField());
        }
    }
    return result;
}
 
Example 7
Source File: VetoableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void addListenerToClass(SourceUnit source, ClassNode classNode) {
    boolean bindable = BindableASTTransformation.hasBindableAnnotation(classNode);
    for (PropertyNode propertyNode : classNode.getProperties()) {
        if (!hasVetoableAnnotation(propertyNode.getField())
            && !propertyNode.getField().isFinal()
            && !propertyNode.getField().isStatic())
        {
            createListenerSetter(source,
                    bindable || BindableASTTransformation.hasBindableAnnotation(propertyNode.getField()),
                classNode, propertyNode);
        }
    }
}
 
Example 8
Source File: BeanUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void addExplicitProperties(ClassNode cNode, List<PropertyNode> result, Set<String> names, boolean includeStatic) {
    for (PropertyNode pNode : cNode.getProperties()) {
        if (includeStatic || !pNode.isStatic()) {
            if (!names.contains(pNode.getName())) {
                result.add(pNode);
                names.add(pNode.getName());
            }
        }
    }
}
 
Example 9
Source File: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void genProps(ClassNode classNode, PrintWriter out) {
    List<PropertyNode> props = classNode.getProperties();
    if (props != null) {
        for (PropertyNode propNode : props) {
            genProp(propNode, out);
        }
    }
}
 
Example 10
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static List<FieldNode> getInstancePropertyFields(final ClassNode cNode) {
    List<FieldNode> result = new ArrayList<>();
    for (PropertyNode pNode : cNode.getProperties()) {
        if (!pNode.isStatic()) {
            result.add(pNode.getField());
        }
    }
    return result;
}
 
Example 11
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static List<PropertyNode> getInstanceProperties(final ClassNode cNode) {
    List<PropertyNode> result = new ArrayList<>();
    for (PropertyNode pNode : cNode.getProperties()) {
        if (!pNode.isStatic()) {
            result.add(pNode);
        }
    }
    return result;
}
 
Example 12
Source File: PackageScopeASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 13
Source File: UnionTypeClassNode.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public List<PropertyNode> getProperties() {
    List<PropertyNode> nodes = new LinkedList<PropertyNode>();
    for (ClassNode delegate : delegates) {
        List<PropertyNode> properties = delegate.getProperties();
        if (properties != null) nodes.addAll(properties);
    }
    return nodes;
}
 
Example 14
Source File: TupleConstructorASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
    BlockStatement block = new BlockStatement();
    for (PropertyNode pNode : cNode.getProperties()) {
        if (pNode.isStatic()) continue;

        // if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
        Statement ifStatement = ifS(
                callX(namedArgs, "containsKey", constX(pNode.getName())),
                assignS(varX(pNode), propX(namedArgs, pNode.getName())));
        block.addStatement(ifStatement);
    }
    block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
    return block;
}
 
Example 15
Source File: ClassNodeUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Detect whether a static property with the given name is within the class
 * or a super class.
 *
 * @param cNode the ClassNode of interest
 * @param propName the property name
 * @return the static property if found or else null
 */
public static PropertyNode getStaticProperty(ClassNode cNode, String propName) {
    ClassNode classNode = cNode;
    while (classNode != null) {
        for (PropertyNode pn : classNode.getProperties()) {
            if (pn.getName().equals(propName) && pn.isStatic()) return pn;
        }
        classNode = classNode.getSuperClass();
    }
    return null;
}
 
Example 16
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static List<PropertyNode> getAllProperties(final Set<String> names, final ClassNode origType, final ClassNode cNode, final boolean includeProperties,
                                                  final boolean includeFields, final boolean includePseudoGetters, final boolean includePseudoSetters,
                                                  final boolean traverseSuperClasses, final boolean skipReadonly, final boolean reverse, final boolean allNames, final boolean includeStatic) {
    List<PropertyNode> result = new ArrayList<>();
    if (cNode != ClassHelper.OBJECT_TYPE && traverseSuperClasses && !reverse) {
        result.addAll(getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, includePseudoGetters, includePseudoSetters, true, skipReadonly));
    }
    if (includeProperties) {
        for (PropertyNode pNode : cNode.getProperties()) {
            if ((!pNode.isStatic() || includeStatic) && !names.contains(pNode.getName())) {
                result.add(pNode);
                names.add(pNode.getName());
            }
        }
        if (includePseudoGetters || includePseudoSetters) {
            BeanUtils.addPseudoProperties(origType, cNode, result, names, includeStatic, includePseudoGetters, includePseudoSetters);
        }
    }
    if (includeFields) {
        for (FieldNode fNode : cNode.getFields()) {
            if ((fNode.isStatic() && !includeStatic) || fNode.isSynthetic() || cNode.getProperty(fNode.getName()) != null || names.contains(fNode.getName())) {
                continue;
            }

            // internal field
            if (fNode.getName().contains("$") && !allNames) {
                continue;
            }

            if (fNode.isPrivate() && !cNode.equals(origType)) {
                continue;
            }
            if (fNode.isFinal() && fNode.getInitialExpression() != null && skipReadonly) {
                continue;
            }
            result.add(new PropertyNode(fNode, fNode.getModifiers(), null, null));
            names.add(fNode.getName());
        }
    }
    if (cNode != ClassHelper.OBJECT_TYPE && traverseSuperClasses && reverse) {
        result.addAll(getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, includePseudoGetters, includePseudoSetters, true, skipReadonly));
    }
    return result;
}
 
Example 17
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void checkNoStaticMethodWithSameSignatureAsNonStatic(final ClassNode node) {
    ClassNode parent = node.getSuperClass();
    Map<String, MethodNode> result;
    // start with methods from the parent if any
    if (parent != null) {
        result = parent.getDeclaredMethodsMap();
    } else {
        result = new HashMap<String, MethodNode>();
    }
    // add in unimplemented abstract methods from the interfaces
    ClassNodeUtils.addDeclaredMethodsFromInterfaces(node, result);
    for (MethodNode methodNode : node.getMethods()) {
        MethodNode mn = result.get(methodNode.getTypeDescriptor());
        if (mn != null && (mn.isStatic() ^ methodNode.isStatic()) && !methodNode.isStaticConstructor()) {
            if (!mn.isAbstract()) continue;
            ClassNode declaringClass = mn.getDeclaringClass();
            ClassNode cn = declaringClass.getOuterClass();
            if (cn == null && declaringClass.isResolved()) {
                // in case of a precompiled class, the outerclass is unknown
                Class typeClass = declaringClass.getTypeClass();
                typeClass = typeClass.getEnclosingClass();
                if (typeClass != null) {
                    cn = ClassHelper.make(typeClass);
                }
            }
            if (!Traits.isTrait(cn)) {
                ASTNode errorNode = methodNode;
                String name = mn.getName();
                if (errorNode.getLineNumber() == -1) {
                    // try to get a better error message location based on the property
                    for (PropertyNode propertyNode : node.getProperties()) {
                        if (name.startsWith("set") || name.startsWith("get") || name.startsWith("is")) {
                            String propName = Verifier.capitalize(propertyNode.getField().getName());
                            String shortName = name.substring(name.startsWith("is") ? 2 : 3);
                            if (propName.equals(shortName)) {
                                errorNode = propertyNode;
                                break;
                            }
                        }
                    }
                }
                addError("The " + getDescription(methodNode) + " is already defined in " + getDescription(node) +
                        ". You cannot have both a static and an instance method with the same signature", errorNode);
            }
        }
        result.put(methodNode.getTypeDescriptor(), methodNode);
    }
}
 
Example 18
Source File: Verifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void addInitialization(ClassNode node, ConstructorNode constructorNode) {
    Statement firstStatement = constructorNode.getFirstStatement();
    // if some transformation decided to generate constructor then it probably knows who it does
    if (firstStatement instanceof BytecodeSequence)
        return;

    ConstructorCallExpression first = getFirstIfSpecialConstructorCall(firstStatement);

    // in case of this(...) let the other constructor do the init
    if (first != null && (first.isThisCall())) return;

    List<Statement> statements = new ArrayList<Statement>();
    List<Statement> staticStatements = new ArrayList<Statement>();
    final boolean isEnum = node.isEnum();
    List<Statement> initStmtsAfterEnumValuesInit = new ArrayList<Statement>();
    Set<String> explicitStaticPropsInEnum = new HashSet<String>();
    if (isEnum) {
        for (PropertyNode propNode : node.getProperties()) {
            if (!propNode.isSynthetic() && propNode.getField().isStatic()) {
                explicitStaticPropsInEnum.add(propNode.getField().getName());
            }
        }
        for (FieldNode fieldNode : node.getFields()) {
            if (!fieldNode.isSynthetic() && fieldNode.isStatic() && fieldNode.getType() != node) {
                explicitStaticPropsInEnum.add(fieldNode.getName());
            }
        }
    }

    if (!Traits.isTrait(node)) {
        for (FieldNode fn : node.getFields()) {
            addFieldInitialization(statements, staticStatements, fn, isEnum,
                    initStmtsAfterEnumValuesInit, explicitStaticPropsInEnum);
        }
    }

    statements.addAll(node.getObjectInitializerStatements());

    BlockStatement block = getCodeAsBlock(constructorNode);
    List<Statement> otherStatements = block.getStatements();
    if (!otherStatements.isEmpty()) {
        if (first != null) {
            // it is super(..) since this(..) is already covered
            otherStatements.remove(0);
            statements.add(0, firstStatement);
        }
        Statement stmtThis$0 = getImplicitThis$0StmtIfInnerClass(otherStatements);
        if (stmtThis$0 != null) {
            // since there can be field init statements that depend on method/property dispatching
            // that uses this$0, it needs to bubble up before the super call itself (GROOVY-4471)
            statements.add(0, stmtThis$0);
        }
        statements.addAll(otherStatements);
    }
    BlockStatement newBlock = new BlockStatement(statements, block.getVariableScope());
    newBlock.setSourcePosition(block);
    constructorNode.setCode(newBlock);


    if (!staticStatements.isEmpty()) {
        if (isEnum) {
            /*
             * GROOVY-3161: initialize statements for explicitly declared static fields
             * inside an enum should come after enum values are initialized
             */
            staticStatements.removeAll(initStmtsAfterEnumValuesInit);
            node.addStaticInitializerStatements(staticStatements, true);
            if (!initStmtsAfterEnumValuesInit.isEmpty()) {
                node.positionStmtsAfterEnumInitStmts(initStmtsAfterEnumValuesInit);
            }
        } else {
            node.addStaticInitializerStatements(staticStatements, true);
        }
    }
}
 
Example 19
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void generatePropertyMethods(final ClassNode cNode) {
    for (PropertyNode node : cNode.getProperties()) {
        processProperty(cNode, node);
    }
}