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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations