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

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getAllDeclaredMethods() . 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: NullCheckASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!NULL_CHECK_TYPE.equals(anno.getClassNode())) return;
    boolean includeGenerated = isIncludeGenerated(anno);

    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, NULL_CHECK_NAME)) return;
        for (ConstructorNode cn : cNode.getDeclaredConstructors()) {
            adjustMethod(cn, includeGenerated);
        }
        for (MethodNode mn : cNode.getAllDeclaredMethods()) {
            adjustMethod(mn, includeGenerated);
        }
    } else if (parent instanceof MethodNode) {
        // handles constructor case too
        adjustMethod((MethodNode) parent, false);
    }
}
 
Example 2
Source File: AutoFinalASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void processClass(ClassNode cNode, final ClassCodeVisitorSupport visitor) {
    if (!isEnabled(cNode)) return;
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cNode.getName() +
                "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
        return;
    }

    for (ConstructorNode cn : cNode.getDeclaredConstructors()) {
        if (hasNoExplicitAutoFinal(cn)) {
            processConstructorOrMethod(cn, visitor);
        }
    }

    for (MethodNode mn : cNode.getAllDeclaredMethods()) {
        if (hasNoExplicitAutoFinal(mn)) {
            processConstructorOrMethod(mn, visitor);
        }
    }

    Iterator<InnerClassNode> it = cNode.getInnerClasses();
    while (it.hasNext()) {
        InnerClassNode in = it.next();
        if (in.getAnnotations(MY_TYPE).isEmpty()) {
            processClass(in, visitor);
        }
    }

    visitor.visitClass(cNode);
}
 
Example 3
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkMethodsForIncorrectName(ClassNode cn) {
    if (!strictNames) return;
    List<MethodNode> methods = cn.getAllDeclaredMethods();
    for (MethodNode mNode : methods) {
        String name = mNode.getName();
        if (name.equals("<init>") || name.equals("<clinit>")) continue;
        // Groovy allows more characters than Character.isValidJavaIdentifier() would allow
        // if we find a good way to encode special chars we could remove (some of) these checks
        for (String ch : INVALID_NAME_CHARS) {
            if (name.contains(ch)) {
                addError("You are not allowed to have '" + ch + "' in a method name", mNode);
            }
        }
    }
}
 
Example 4
Source File: FromAbstractTypeMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<ClassNode[]> extractSignaturesFromMethods(final ClassNode cn) {
    List<MethodNode> methods = cn.getAllDeclaredMethods();
    List<ClassNode[]> signatures = new LinkedList<ClassNode[]>();
    for (MethodNode method : methods) {
        if (!method.isSynthetic() && method.isAbstract()) {
            extractParametersFromMethod(signatures, method);
        }
    }
    return signatures;
}
 
Example 5
Source File: BeanUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static void addPseudoProperties(ClassNode origType, ClassNode cNode, List<PropertyNode> result, Set<String> names, boolean includeStatic, boolean includePseudoGetters, boolean includePseudoSetters) {
    if (!includePseudoGetters && !includePseudoSetters) return;
    List<MethodNode> methods = cNode.getAllDeclaredMethods();
    for (MethodNode mNode : methods) {
        if (!includeStatic && mNode.isStatic()) continue;
        if (hasAnnotation(mNode, INTERNAL_TYPE)) 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() != origType && mNode.isPrivate()) {
            // skip private super methods
            continue;
        }
        int paramCount = mNode.getParameters().length;
        ClassNode paramType = mNode.getReturnType();
        String propName = null;
        Statement getter = null;
        Statement setter = null;
        if (paramCount == 0) {
            if (includePseudoGetters && name.startsWith(GET_PREFIX)) {
                // Simple getter
                propName = decapitalize(name.substring(3));
                getter = mNode.getCode();
            } else if (includePseudoGetters && name.startsWith(IS_PREFIX) && paramType.equals(ClassHelper.boolean_TYPE)) {
                // boolean getter
                propName = decapitalize(name.substring(2));
                getter = mNode.getCode();
            }
        } else if (paramCount == 1) {
            if (includePseudoSetters && name.startsWith(SET_PREFIX)) {
                // Simple setter
                propName = decapitalize(name.substring(3));
                setter = mNode.getCode();
                paramType = mNode.getParameters()[0].getType();

            }
        }
        if (propName != null) {
            addIfMissing(cNode, result, names, mNode, paramType, propName, getter, setter);
        }
    }
}