Search in sources :

Example 6 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class UselessAssignment method execute.

public void execute(CurrentPath path) {
    Map<String, Usage> hash = new HashMap<>();
    // System.out.println("path size is " + path.size());
    for (Iterator<DataFlowNode> i = path.iterator(); i.hasNext(); ) {
        // System.out.println("i = " + i);
        DataFlowNode inode = i.next();
        if (inode.getVariableAccess() == null) {
            continue;
        }
        for (int j = 0; j < inode.getVariableAccess().size(); j++) {
            VariableAccess va = inode.getVariableAccess().get(j);
            // System.out.println("inode = " + inode + ", va = " + va);
            Usage u = hash.get(va.getVariableName());
            if (u != null) {
                // FIXME need to check for assignment as well!
                if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
                    // System.out.println(va.getVariableName() + ":" + u);
                    addViolation(rc, u.node.getNode(), va.getVariableName());
                }
            /*
                     * // UR - ?? else if (last == VariableAccess.UNDEFINITION
                     * && va.isReference()) {
                     * //this.rc.getReport().addRuleViolation(
                     * createRuleViolation(rc, inode.getNode(),
                     * va.getVariableName(), "UR")); } // DU - variable is
                     * defined and then goes out of scope // i.e., unused
                     * parameter else if (last == VariableAccess.DEFINITION &&
                     * va.isUndefinition()) { if (inode.getNode() != null) {
                     * this.rc.getReport().addRuleViolation(createRuleViolation(
                     * rc, tmp, va.getVariableName(), "DU")); } }
                     */
            }
            u = new Usage(va.getAccessType(), inode);
            hash.put(va.getVariableName(), u);
        }
    }
}
Also used : HashMap(java.util.HashMap) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Example 7 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method addVariableAccess.

/**
 * Adds a VariableAccess to a dataflow node.
 *
 * @param node
 *            location of the access of a variable
 * @param va
 *            variable access to add
 * @param flow
 *            dataflownodes that can contain the node.
 */
private void addVariableAccess(Node node, VariableAccess va, List<DataFlowNode> flow) {
    // backwards to find the right inode (not a method declaration)
    for (int i = flow.size() - 1; i > 0; i--) {
        DataFlowNode inode = flow.get(i);
        if (inode.getNode() == null) {
            continue;
        }
        List<? extends Node> children = inode.getNode().findDescendantsOfType(node.getClass());
        for (Node n : children) {
            if (node.equals(n)) {
                List<VariableAccess> v = new ArrayList<>();
                v.add(va);
                inode.setVariableAccess(v);
                return;
            }
        }
    }
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Node(net.sourceforge.pmd.lang.ast.Node) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList)

Example 8 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method computeNow.

private void computeNow(Node node) {
    DataFlowNode inode = node.getDataFlowNode();
    List<VariableAccess> undefinitions = markUsages(inode);
    // all variables are first in state undefinition
    DataFlowNode firstINode = inode.getFlow().get(0);
    firstINode.setVariableAccess(undefinitions);
    // all variables are getting undefined when leaving scope
    DataFlowNode lastINode = inode.getFlow().get(inode.getFlow().size() - 1);
    lastINode.setVariableAccess(undefinitions);
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Example 9 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessTest method testGetVariableName.

@Test
public void testGetVariableName() {
    VariableAccess va = new VariableAccess(VariableAccess.DEFINITION, "foo.bar");
    assertEquals("foo", va.getVariableName());
    va = new VariableAccess(VariableAccess.DEFINITION, ".foobar");
    assertEquals("", va.getVariableName());
    va = new VariableAccess(VariableAccess.DEFINITION, "foobar.");
    assertEquals("foobar", va.getVariableName());
    va = new VariableAccess(VariableAccess.DEFINITION, "foobar");
    assertEquals("foobar", va.getVariableName());
}
Also used : VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) Test(org.junit.Test)

Example 10 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd-eclipse-plugin by pmd.

the class DataflowGraphViewer method referenceStringFrom.

private String referenceStringFrom(DataFlowNode dfNode) {
    List<VariableAccess> access = dfNode.getVariableAccess();
    if (access == null) {
        return null;
    }
    StringBuilder exp = new StringBuilder();
    for (int k = 0; k < access.size(); k++) {
        if (k > 0) {
            exp.append(", ");
        }
        VariableAccess va = access.get(k);
        switch(va.getAccessType()) {
            case VariableAccess.DEFINITION:
                exp.append("d(");
                break;
            case VariableAccess.REFERENCING:
                exp.append("r(");
                break;
            case VariableAccess.UNDEFINITION:
                exp.append("u(");
                break;
            default:
                exp.append("?(");
        }
        exp.append(va.getVariableName()).append(')');
    }
    return exp.toString();
}
Also used : VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Aggregations

VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)10 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)6 ArrayList (java.util.ArrayList)4 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Node (net.sourceforge.pmd.lang.ast.Node)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)1 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameter)1 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)1 PLSQLNameOccurrence (net.sourceforge.pmd.lang.plsql.symboltable.PLSQLNameOccurrence)1 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)1 Test (org.junit.Test)1