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

The following examples show how to use org.codehaus.groovy.ast.PropertyNode#isStatic() . 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: 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 2
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 3
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitProperty(final PropertyNode node) {
    Map<GenericsTypeName, GenericsType> oldPNames = genericParameterNames;
    if (node.isStatic() && !Traits.isTrait(node.getDeclaringClass())) {
        genericParameterNames = new HashMap<>();
    }

    ClassNode t = node.getType();
    resolveOrFail(t, node);
    super.visitProperty(node);
    fieldTypesChecked.add(node.getField());

    genericParameterNames = oldPNames;
}
 
Example 4
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 5
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 6
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 7
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 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: 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;
}