Search in sources :

Example 11 with ASTPrimaryPrefix

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

the class GuardLogStatementRule method visit.

@Override
public Object visit(ASTPrimaryExpression node, Object data) {
    if (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
        ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node.jjtGetChild(0);
        String methodCall = getMethodCallName(prefix);
        String logLevel = getLogLevelName(node, methodCall);
        if (guardStmtByLogLevel.containsKey(methodCall) && node.jjtGetChild(1) instanceof ASTPrimarySuffix && node.jjtGetChild(1).hasDescendantOfType(ASTAdditiveExpression.class)) {
            if (!hasGuard(node, methodCall, logLevel)) {
                super.addViolation(data, node);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 12 with ASTPrimaryPrefix

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

the class GuardLogStatementRule method hasGuard.

private boolean hasGuard(ASTPrimaryExpression node, String methodCall, String logLevel) {
    ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
    if (ifStatement == null) {
        return false;
    }
    // an if statement always has an expression
    ASTExpression expr = ifStatement.getFirstChildOfType(ASTExpression.class);
    List<ASTPrimaryPrefix> guardCalls = expr.findDescendantsOfType(ASTPrimaryPrefix.class);
    if (guardCalls.isEmpty()) {
        return false;
    }
    boolean foundGuard = false;
    // check all conditions in the if expression
    for (ASTPrimaryPrefix guardCall : guardCalls) {
        if (guardCall.jjtGetNumChildren() < 1 || guardCall.jjtGetChild(0).getImage() == null) {
            continue;
        }
        String guardMethodCall = getLastPartOfName(guardCall.jjtGetChild(0).getImage());
        boolean guardMethodCallMatches = guardStmtByLogLevel.get(methodCall).contains(guardMethodCall);
        boolean hasArguments = guardCall.jjtGetParent().hasDescendantOfType(ASTArgumentList.class);
        if (guardMethodCallMatches && !hasArguments) {
            // simple case: guard method without arguments found
            foundGuard = true;
        } else if (guardMethodCallMatches && hasArguments) {
            // java.util.logging: guard method with argument. Verify the log level
            String guardArgLogLevel = getLogLevelName(guardCall.jjtGetParent(), guardMethodCall);
            foundGuard = logLevel.equals(guardArgLogLevel);
        }
        if (foundGuard) {
            break;
        }
    }
    return foundGuard;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 13 with ASTPrimaryPrefix

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

the class MethodReturnsInternalArrayRule method visit.

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
    if (!method.getResultType().returnsArray() || method.isPrivate()) {
        return data;
    }
    List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class);
    ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class);
    for (ASTReturnStatement ret : returns) {
        final String vn = getReturnedVariableName(ret);
        if (!isField(vn, td)) {
            continue;
        }
        if (ret.findDescendantsOfType(ASTPrimarySuffix.class).size() > 2) {
            continue;
        }
        if (ret.hasDescendantOfType(ASTAllocationExpression.class)) {
            continue;
        }
        if (hasArraysCopyOf(ret)) {
            continue;
        }
        if (hasClone(ret, vn)) {
            continue;
        }
        if (isEmptyArray(vn, td)) {
            continue;
        }
        if (!isLocalVariable(vn, method)) {
            addViolation(data, ret, vn);
        } else {
            // This is to handle field hiding
            final ASTPrimaryPrefix pp = ret.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (pp != null && pp.usesThisModifier()) {
                final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (ps.hasImageEqualTo(vn)) {
                    addViolation(data, ret, vn);
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 14 with ASTPrimaryPrefix

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

the class ArrayIsStoredDirectlyRule method getExpressionVarName.

private String getExpressionVarName(Node e) {
    String assignedVar = getFirstNameImage(e);
    if (assignedVar == null) {
        ASTPrimarySuffix suffix = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
        if (suffix != null) {
            assignedVar = suffix.getImage();
            ASTPrimaryPrefix prefix = e.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (prefix != null) {
                if (prefix.usesThisModifier()) {
                    assignedVar = "this." + assignedVar;
                } else if (prefix.usesSuperModifier()) {
                    assignedVar = "super." + assignedVar;
                }
            }
        }
    }
    return assignedVar;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 15 with ASTPrimaryPrefix

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

the class TccAttributeAccessCollector method getVariableName.

private String getVariableName(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    if (prefix.usesThisModifier()) {
        List<ASTPrimarySuffix> suffixes = node.findChildrenOfType(ASTPrimarySuffix.class);
        if (suffixes.size() > 1) {
            if (!suffixes.get(1).isArguments()) {
                // not a method call
                return suffixes.get(0).getImage();
            }
        }
    }
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String variableName = null;
    if (name != null) {
        int dotIndex = name.getImage().indexOf(".");
        if (dotIndex == -1) {
            variableName = name.getImage();
        } else {
            variableName = name.getImage().substring(0, dotIndex);
        }
    }
    return variableName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Aggregations

ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)30 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)16 Node (net.sourceforge.pmd.lang.ast.Node)15 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)15 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)11 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)5 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 ArrayList (java.util.ArrayList)4 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 Test (org.junit.Test)4 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2