Search in sources :

Example 1 with MethodNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.

the class UnusedPrivateMethodRule method findUnique.

private Set<MethodNameDeclaration> findUnique(Map<MethodNameDeclaration, List<NameOccurrence>> methods) {
    // some rather hideous hackery here
    // to work around the fact that PMD does not yet do full type analysis
    // when it does, delete this
    Set<MethodNameDeclaration> unique = new HashSet<>();
    Set<String> sigs = new HashSet<>();
    for (MethodNameDeclaration mnd : methods.keySet()) {
        String sig = mnd.getImage() + mnd.getParameterCount() + mnd.isVarargs();
        if (!sigs.contains(sig)) {
            unique.add(mnd);
        }
        sigs.add(sig);
    }
    return unique;
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) HashSet(java.util.HashSet)

Example 2 with MethodNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.

the class UseCollectionIsEmptyRule method getTypeOfMethodCall.

private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) {
    ASTClassOrInterfaceType type = null;
    ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class);
    if (methodName != null) {
        ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class);
        Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations();
        for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) {
            if (e.getKey().getName().equals(methodName.getImage())) {
                type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class).getFirstChildOfType(ASTResultType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                break;
            }
        }
    }
    return type;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) HashMap(java.util.HashMap) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 3 with MethodNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.

the class AccessorMethodGenerationRule method analyzeScope.

private void analyzeScope(final AbstractJavaScope file, final Object data) {
    for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
        final ClassScope classScope = (ClassScope) classDecl.getScope();
        // Check fields
        for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
            final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
            analyzeMember(field, varDecl.getValue(), classScope, data);
        }
        // Check methods
        for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
            final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
            analyzeMember(method, methodDecl.getValue(), classScope, data);
        }
        // Check inner classes
        analyzeScope(classScope, data);
    }
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ClassNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration) List(java.util.List) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 4 with MethodNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration in project pmd by pmd.

the class BeanMembersShouldSerializeRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    if (node.isInterface()) {
        return data;
    }
    Map<MethodNameDeclaration, List<NameOccurrence>> methods = node.getScope().getEnclosingScope(ClassScope.class).getMethodDeclarations();
    List<ASTMethodDeclarator> getSetMethList = new ArrayList<>(methods.size());
    for (MethodNameDeclaration d : methods.keySet()) {
        ASTMethodDeclarator mnd = d.getMethodNameDeclaratorNode();
        if (isBeanAccessor(mnd)) {
            getSetMethList.add(mnd);
        }
    }
    String[] methNameArray = imagesOf(getSetMethList);
    Arrays.sort(methNameArray);
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration decl = entry.getKey();
        AccessNode accessNodeParent = decl.getAccessNodeParent();
        if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic()) {
            continue;
        }
        String varName = trimIfPrefix(decl.getImage());
        varName = varName.substring(0, 1).toUpperCase(Locale.ROOT) + varName.substring(1, varName.length());
        boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
        boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
        // variable...
        if (!hasGetMethod || !accessNodeParent.isFinal() && !hasSetMethod) {
            addViolation(data, decl.getNode(), decl.getImage());
        }
    }
    return super.visit(node, data);
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ArrayList(java.util.ArrayList) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope) ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Aggregations

MethodNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration)4 List (java.util.List)3 Map (java.util.Map)3 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)3 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)1 ClassNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration)1