Search in sources :

Example 1 with AccessNode

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

the class ImmutableFieldRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    Object result = super.visit(node, data);
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
    List<ASTConstructorDeclaration> constructors = findAllConstructors(node);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration field = entry.getKey();
        AccessNode accessNodeParent = field.getAccessNodeParent();
        if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal() || accessNodeParent.isVolatile() || hasClassLombokAnnotation()) {
            continue;
        }
        FieldImmutabilityType type = initializedInConstructor(entry.getValue(), new HashSet<>(constructors));
        if (type == FieldImmutabilityType.MUTABLE) {
            continue;
        }
        if (type == FieldImmutabilityType.IMMUTABLE || type == FieldImmutabilityType.CHECKDECL && initializedWhenDeclared(field)) {
            addViolation(data, field.getNode(), field.getImage());
        }
    }
    return result;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Example 2 with AccessNode

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

the class UnusedPrivateFieldRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    boolean classHasLombok = hasLombokAnnotation(node);
    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 (!accessNodeParent.isPrivate() || isOK(decl.getImage()) || classHasLombok || hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
            continue;
        }
        if (!actuallyUsed(entry.getValue())) {
            if (!usedInOuterClass(node, decl) && !usedInOuterEnum(node, decl)) {
                addViolation(data, decl.getNode(), decl.getImage());
            }
        }
    }
    return super.visit(node, data);
}
Also used : Annotatable(net.sourceforge.pmd.lang.java.ast.Annotatable) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Example 3 with AccessNode

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

the class MethodArgumentCouldBeFinalRule method lookForViolation.

private void lookForViolation(Scope scope, Object data) {
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = scope.getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        VariableNameDeclaration var = entry.getKey();
        AccessNode node = var.getAccessNodeParent();
        if (!node.isFinal() && node instanceof ASTFormalParameter && !assigned(entry.getValue())) {
            addViolation(data, node, var.getImage());
        }
    }
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map)

Example 4 with AccessNode

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 5 with AccessNode

use of net.sourceforge.pmd.lang.java.ast.AccessNode 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

AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)7 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)6 List (java.util.List)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 Annotatable (net.sourceforge.pmd.lang.java.ast.Annotatable)1 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)1 MethodNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration)1