Search in sources :

Example 1 with ASTResultType

use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.

the class BeanMembersShouldSerializeRule method isBeanAccessor.

private boolean isBeanAccessor(ASTMethodDeclarator meth) {
    String methodName = meth.getImage();
    if (methodName.startsWith("get") || methodName.startsWith("set")) {
        return true;
    }
    if (methodName.startsWith("is")) {
        ASTResultType ret = ((ASTMethodDeclaration) meth.jjtGetParent()).getResultType();
        List<ASTPrimitiveType> primitives = ret.findDescendantsOfType(ASTPrimitiveType.class);
        if (!primitives.isEmpty() && primitives.get(0).isBoolean()) {
            return true;
        }
    }
    return false;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

Example 2 with ASTResultType

use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.

the class UselessOverridingMethodRule method isMethodType.

// TODO: this method should be externalize into an utility class, shouldn't it ?
private boolean isMethodType(ASTMethodDeclaration node, String methodType) {
    boolean result = false;
    ASTResultType type = node.getResultType();
    if (type != null) {
        result = type.hasDescendantMatchingXPath("./Type/ReferenceType/ClassOrInterfaceType[@Image = '" + methodType + "']");
    }
    return result;
}
Also used : ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

Example 3 with ASTResultType

use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.

the class SuspiciousHashcodeMethodNameRule method visit.

public Object visit(ASTMethodDeclaration node, Object data) {
    /*
         * original XPath rule was //MethodDeclaration [ResultType
         * //PrimitiveType [@Image='int'] [//MethodDeclarator [@Image='hashcode'
         * or @Image='HashCode' or @Image='Hashcode']
         * [not(FormalParameters/*)]]]
         */
    ASTResultType type = node.getResultType();
    ASTMethodDeclarator decl = node.getFirstChildOfType(ASTMethodDeclarator.class);
    String name = decl.getImage();
    if ("hashcode".equalsIgnoreCase(name) && !"hashCode".equals(name) && decl.jjtGetChild(0).jjtGetNumChildren() == 0 && type.jjtGetNumChildren() != 0) {
        Node t = type.jjtGetChild(0).jjtGetChild(0);
        if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) {
            addViolation(data, node);
            return data;
        }
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) Node(net.sourceforge.pmd.lang.ast.Node) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

Example 4 with ASTResultType

use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.

the class LooseCouplingRule method visit.

// TODO - these should be brought in via external properties
// private static final Set implClassNames = CollectionUtil.asSet( new
// Object[] {
// "ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet",
// "TreeSet", "TreeMap", "Vector",
// "java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
// "java.util.LinkedHashMap", "java.util.LinkedHashSet",
// "java.util.TreeSet",
// "java.util.TreeMap", "java.util.Vector"
// });
@Override
public Object visit(ASTClassOrInterfaceType node, Object data) {
    if (methodHasOverride(node)) {
        return data;
    }
    Node parent = node.getNthParent(3);
    Class<?> clazzType = node.getType();
    boolean isType = CollectionUtil.isCollectionType(clazzType, false);
    if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
        addViolation(data, node, node.getImage());
    }
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

Example 5 with ASTResultType

use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.

the class UseUtilityClassRule method visit.

@Override
public Object visit(ASTClassOrInterfaceBody decl, Object data) {
    if (decl.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
        ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) decl.jjtGetParent();
        if (parent.isAbstract() || parent.isInterface() || isExceptionType(parent)) {
            return super.visit(decl, data);
        }
        if (isOkUsingLombok(parent)) {
            return super.visit(decl, data);
        }
        int i = decl.jjtGetNumChildren();
        int methodCount = 0;
        boolean isOK = false;
        while (i > 0) {
            Node p = decl.jjtGetChild(--i);
            if (p.jjtGetNumChildren() == 0) {
                continue;
            }
            Node n = skipAnnotations(p);
            if (n instanceof ASTFieldDeclaration) {
                if (!((ASTFieldDeclaration) n).isStatic()) {
                    isOK = true;
                    break;
                }
            } else if (n instanceof ASTConstructorDeclaration) {
                if (((ASTConstructorDeclaration) n).isPrivate()) {
                    isOK = true;
                    break;
                }
            } else if (n instanceof ASTMethodDeclaration) {
                ASTMethodDeclaration m = (ASTMethodDeclaration) n;
                if (!m.isPrivate()) {
                    methodCount++;
                }
                if (!m.isStatic()) {
                    isOK = true;
                    break;
                }
                // TODO use symbol table
                if (m.getMethodName().equals("suite")) {
                    ASTResultType res = m.getResultType();
                    ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                    if (c != null && c.hasImageEqualTo("Test")) {
                        isOK = true;
                        break;
                    }
                }
            }
        }
        if (!isOK && methodCount > 0) {
            addViolation(data, decl);
        }
    }
    return super.visit(decl, data);
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Aggregations

ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)6 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 AbstractJavaAccessNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode)1