Search in sources :

Example 16 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class LocalScope method addNameOccurrence.

@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
    PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
    Set<NameDeclaration> declarations = findVariableHere(occurrence);
    if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            nameOccurrences.add(occurrence);
            Node n = occurrence.getLocation();
            if (n instanceof ASTName) {
                ((ASTName) n).setNameDeclaration(decl);
            }
        // TODO what to do with PrimarySuffix case?
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.plsql.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 17 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class LocalScope method findVariableHere.

public Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
    Set<NameDeclaration> result = new HashSet<>();
    if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
        return result;
    }
    ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
    Applier.apply(finder, getVariableDeclarations().keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) HashSet(java.util.HashSet)

Example 18 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class UselessStringValueOfRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
        return super.visit(node, data);
    }
    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if ("String.valueOf".equals(image)) {
        Node parent = node.jjtGetParent();
        if (parent.jjtGetNumChildren() != 2) {
            return super.visit(node, data);
        }
        // skip String.valueOf(anyarraytype[])
        ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            ASTName arg = args.getFirstDescendantOfType(ASTName.class);
            if (arg != null) {
                NameDeclaration declaration = arg.getNameDeclaration();
                if (declaration != null) {
                    ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
                    if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
                        return super.visit(node, data);
                    }
                }
            }
        }
        Node gp = parent.jjtGetParent();
        if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
            boolean ok = false;
            if (gp.jjtGetChild(0) == parent) {
                ok = !isPrimitive(gp.jjtGetChild(1));
            } else {
                for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
                    ok = !isPrimitive(gp.jjtGetChild(i));
                }
            }
            if (ok) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 19 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class MethodScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            getVariableDeclarations().get(decl).add(javaOccurrence);
            Node n = javaOccurrence.getLocation();
            if (n instanceof ASTName) {
                ((ASTName) n).setNameDeclaration(decl);
            }
        // TODO what to do with PrimarySuffix case?
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration)

Example 20 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class JUnitTestsShouldIncludeAssertRule method getRuleAnnotatedExpectedExceptions.

/**
 * Gets a list of NameDeclarations for all the fields that have type
 * ExpectedException and have a Rule annotation.
 *
 * @param classScope
 *            The class scope to search for
 * @return See description
 */
private Map<String, List<NameOccurrence>> getRuleAnnotatedExpectedExceptions(Scope classScope) {
    Map<String, List<NameOccurrence>> result = new HashMap<>();
    Map<NameDeclaration, List<NameOccurrence>> decls = classScope.getDeclarations();
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        Node parent = entry.getKey().getNode().jjtGetParent().jjtGetParent().jjtGetParent();
        if (parent.hasDescendantOfType(ASTMarkerAnnotation.class) && parent.getFirstChildOfType(ASTFieldDeclaration.class) != null) {
            String annot = parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage();
            if (!"Rule".equals(annot) && !"org.junit.Rule".equals(annot)) {
                continue;
            }
            Node type = parent.getFirstDescendantOfType(ASTReferenceType.class);
            if (!"ExpectedException".equals(type.jjtGetChild(0).getImage())) {
                continue;
            }
            result.put(entry.getKey().getName(), entry.getValue());
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Node(net.sourceforge.pmd.lang.ast.Node) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) HashMap(java.util.HashMap) Map(java.util.Map) ASTMarkerAnnotation(net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)

Aggregations

NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)49 List (java.util.List)28 Test (org.junit.Test)28 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)13 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)12 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)12 Map (java.util.Map)10 Node (net.sourceforge.pmd.lang.ast.Node)10 Scope (net.sourceforge.pmd.lang.symboltable.Scope)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)6 ImageFinderFunction (net.sourceforge.pmd.lang.symboltable.ImageFinderFunction)6 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTName (net.sourceforge.pmd.lang.plsql.ast.ASTName)3 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2