Search in sources :

Example 6 with JavaNode

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

the class NpathBaseVisitor method multiplyChildrenComplexities.

/* Multiplies the complexity of the children of this node. */
private int multiplyChildrenComplexities(JavaNode node, Object data) {
    int product = 1;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JavaNode n = (JavaNode) node.jjtGetChild(i);
        product *= (Integer) n.jjtAccept(this, data);
    }
    return product;
}
Also used : JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 7 with JavaNode

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

the class NpathBaseVisitor method sumChildrenComplexities.

/* Sums the complexity of the children of the node. */
private int sumChildrenComplexities(JavaNode node, Object data) {
    int sum = 0;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JavaNode n = (JavaNode) node.jjtGetChild(i);
        sum += (Integer) n.jjtAccept(this, data);
    }
    return sum;
}
Also used : JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 8 with JavaNode

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

the class VariableAccessVisitor method collectDeclarations.

private Set<Map<VariableNameDeclaration, List<NameOccurrence>>> collectDeclarations(DataFlowNode inode) {
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> decls = new HashSet<>();
    Map<VariableNameDeclaration, List<NameOccurrence>> varDecls;
    for (int i = 0; i < inode.getFlow().size(); i++) {
        DataFlowNode n = inode.getFlow().get(i);
        if (n instanceof StartOrEndDataFlowNode) {
            continue;
        }
        varDecls = ((JavaNode) n.getNode()).getScope().getDeclarations(VariableNameDeclaration.class);
        if (!decls.contains(varDecls)) {
            decls.add(varDecls);
        }
    }
    return decls;
}
Also used : StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) HashSet(java.util.HashSet) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 9 with JavaNode

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

the class NpathBaseVisitor method visit.

@Override
public Object visit(ASTSwitchStatement node, Object data) {
    // bool_comp of switch + sum(npath(case_range))
    int boolCompSwitch = CycloMetric.booleanExpressionComplexity(node.getFirstChildOfType(ASTExpression.class));
    int npath = 0;
    int caseRange = 0;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JavaNode n = (JavaNode) node.jjtGetChild(i);
        // Fall-through labels count as 1 for complexity
        if (n instanceof ASTSwitchLabel) {
            npath += caseRange;
            caseRange = 1;
        } else {
            Integer complexity = (Integer) n.jjtAccept(this, data);
            caseRange *= complexity;
        }
    }
    // add in npath of last label
    npath += caseRange;
    return boolCompSwitch + npath;
}
Also used : ASTSwitchLabel(net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 10 with JavaNode

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

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTPrimaryExpression primaryNode, Object data) {
    // visit method arguments in reverse
    for (int i = primaryNode.jjtGetNumChildren() - 1; i >= 0; --i) {
        ((JavaNode) primaryNode.jjtGetChild(i)).jjtAccept(this, data);
    }
    JavaTypeDefinition primaryNodeType = null;
    AbstractJavaTypeNode previousChild = null;
    AbstractJavaTypeNode nextChild;
    Class<?> accessingClass = getEnclosingTypeDeclarationClass(primaryNode);
    for (int childIndex = 0; childIndex < primaryNode.jjtGetNumChildren(); ++childIndex) {
        AbstractJavaTypeNode currentChild = (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex);
        nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren() ? (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
        // skip children which already have their type assigned
        if (currentChild.getType() == null) {
            // Last token, because if 'this' is a Suffix, it'll have tokens '.' and 'this'
            if (currentChild.jjtGetLastToken().toString().equals("this")) {
                if (previousChild != null) {
                    // Qualified 'this' expression
                    currentChild.setTypeDefinition(previousChild.getTypeDefinition());
                } else {
                    // simple 'this' expression
                    ASTClassOrInterfaceDeclaration typeDeclaration = currentChild.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
                    if (typeDeclaration != null) {
                        currentChild.setTypeDefinition(typeDeclaration.getTypeDefinition());
                    }
                }
            // Last token, because if 'super' is a Suffix, it'll have tokens '.' and 'super'
            } else if (currentChild.jjtGetLastToken().toString().equals("super")) {
                if (previousChild != null) {
                    // Qualified 'super' expression
                    // anonymous classes can't have qualified super expression, thus
                    // getSuperClassTypeDefinition's second argumet isn't null, but we are not
                    // looking for enclosing super types
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, previousChild.getType()));
                } else {
                    // simple 'super' expression
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, null));
                }
            } else if (currentChild.getFirstChildOfType(ASTArguments.class) != null) {
                currentChild.setTypeDefinition(previousChild.getTypeDefinition());
            } else if (previousChild != null && previousChild.getType() != null) {
                String currentChildImage = currentChild.getImage();
                if (currentChildImage == null) {
                    // this.<Something>foo(); <Something>foo would be in a Suffix and would have a null image
                    currentChildImage = currentChild.jjtGetLastToken().toString();
                }
                ASTArguments astArguments = nextChild != null ? nextChild.getFirstChildOfType(ASTArguments.class) : null;
                if (astArguments != null) {
                    // method
                    ASTArgumentList astArgumentList = getArgumentList(astArguments);
                    int methodArgsArity = getArgumentListArity(astArgumentList);
                    List<JavaTypeDefinition> typeArguments = getMethodExplicitTypeArugments(currentChild);
                    List<MethodType> methods = getApplicableMethods(previousChild.getTypeDefinition(), currentChildImage, typeArguments, methodArgsArity, accessingClass);
                    currentChild.setTypeDefinition(getBestMethodReturnType(previousChild.getTypeDefinition(), methods, astArgumentList));
                } else {
                    // field
                    currentChild.setTypeDefinition(getFieldType(previousChild.getTypeDefinition(), currentChildImage, accessingClass));
                }
            }
        }
        if (currentChild.getType() != null) {
            primaryNodeType = currentChild.getTypeDefinition();
        } else {
            // avoid falsely passing tests
            primaryNodeType = null;
            break;
        }
        previousChild = currentChild;
    }
    primaryNode.setTypeDefinition(primaryNodeType);
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Aggregations

JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)13 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 DummyJavaNode (net.sourceforge.pmd.lang.java.ast.DummyJavaNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)2 Scope (net.sourceforge.pmd.lang.symboltable.Scope)2 DataPoint (net.sourceforge.pmd.stat.DataPoint)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)1 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTClassOrInterfaceBody (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody)1