Search in sources :

Example 21 with ASTName

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

the class UnusedImportsRule method visit.

@Override
public Object visit(ASTImportDeclaration node, Object data) {
    if (node.isImportOnDemand()) {
        ASTName importedType = (ASTName) node.jjtGetChild(0);
        imports.add(new ImportWrapper(importedType.getImage(), null, node, node.getType(), node.isStatic()));
    } else {
        if (!node.isImportOnDemand()) {
            ASTName importedType = (ASTName) node.jjtGetChild(0);
            String className;
            if (isQualifiedName(importedType)) {
                int lastDot = importedType.getImage().lastIndexOf('.') + 1;
                className = importedType.getImage().substring(lastDot);
            } else {
                className = importedType.getImage();
            }
            imports.add(new ImportWrapper(importedType.getImage(), className, node));
        }
    }
    return data;
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 22 with ASTName

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

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

the class AbstractSunSecureRule method getReturnedVariableName.

/**
 * Gets the name of the variable returned. Some examples: <br>
 * for this.foo returns foo <br>
 * for foo returns foo <br>
 * for foo.bar returns foo.bar
 *
 * @param ret
 *            a return statement to evaluate
 * @return the name of the variable associated or <code>null</code> if it
 *         cannot be detected
 */
protected final String getReturnedVariableName(ASTReturnStatement ret) {
    if (hasTernaryCondition(ret) && hasTernaryNullCheck(ret)) {
        return ret.getFirstDescendantOfType(ASTConditionalExpression.class).jjtGetChild(0).getFirstDescendantOfType(ASTName.class).getImage();
    }
    final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
    if (n != null) {
        return n.getImage();
    }
    final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
    if (ps != null) {
        return ps.getImage();
    }
    return null;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 24 with ASTName

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

the class UnnecessaryLocalBeforeReturnRule method visit.

@Override
public Object visit(ASTReturnStatement rtn, Object data) {
    // skip returns of literals
    ASTName name = rtn.getFirstDescendantOfType(ASTName.class);
    if (name == null) {
        return data;
    }
    // skip 'complicated' expressions
    if (rtn.findDescendantsOfType(ASTExpression.class).size() > 1 || rtn.findDescendantsOfType(ASTPrimaryExpression.class).size() > 1 || isMethodCall(rtn)) {
        return data;
    }
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = name.getScope().getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration variableDeclaration = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (usages.size() == 1) {
            // If there is more than 1 usage, then it's not only returned
            NameOccurrence occ = usages.get(0);
            if (occ.getLocation().equals(name) && isNotAnnotated(variableDeclaration)) {
                String var = name.getImage();
                if (var.indexOf('.') != -1) {
                    var = var.substring(0, var.indexOf('.'));
                }
                // Is the variable initialized with another member that is later used?
                if (!isInitDataModifiedAfterInit(variableDeclaration, rtn) && !statementsBeforeReturn(variableDeclaration, rtn)) {
                    addViolation(data, rtn, var);
                }
            }
        }
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 25 with ASTName

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

the class UnnecessaryLocalBeforeReturnRule method isInitDataModifiedAfterInit.

private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variableDeclaration, final ASTReturnStatement rtn) {
    final ASTVariableInitializer initializer = variableDeclaration.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class);
    if (initializer != null) {
        final List<ASTName> referencedNames = initializer.findDescendantsOfType(ASTName.class);
        for (final ASTName refName : referencedNames) {
            // TODO : Shouldn't the scope allow us to search for a var name occurrences directly, moving up through parent scopes?
            Scope scope = refName.getScope();
            do {
                final Map<VariableNameDeclaration, List<NameOccurrence>> declarations = scope.getDeclarations(VariableNameDeclaration.class);
                for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
                    if (entry.getKey().getName().equals(refName.getImage())) {
                        // Variable found! Check usage locations
                        for (final NameOccurrence occ : entry.getValue()) {
                            final ScopedNode location = occ.getLocation();
                            // Is it used after initializing our "unnecessary" local but before the return statement?
                            if (isAfter(location, initializer) && isAfter(rtn, location)) {
                                return true;
                            }
                        }
                        return false;
                    }
                }
                scope = scope.getParent();
            } while (scope != null);
        }
    }
    return false;
}
Also used : ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3