use of net.sourceforge.pmd.lang.java.ast.JavaNode in project pmd by pmd.
the class ClassScopeTest method testCantContainsSuperToString.
@Test
public void testCantContainsSuperToString() {
ClassNameDeclaration classDeclaration = new ClassNameDeclaration(null);
ClassScope s = new ClassScope("Foo", classDeclaration);
JavaNode node = new DummyJavaNode(1);
node.setImage("super.toString");
assertFalse(s.contains(new JavaNameOccurrence(node, node.getImage())));
}
use of net.sourceforge.pmd.lang.java.ast.JavaNode in project pmd by pmd.
the class ClassScopeTest method testContainsStaticVariablePrefixedWithClassName.
@Test
public void testContainsStaticVariablePrefixedWithClassName() {
ClassNameDeclaration classDeclaration = new ClassNameDeclaration(null);
ClassScope s = new ClassScope("Foo", classDeclaration);
ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
node.setImage("X");
s.addDeclaration(new VariableNameDeclaration(node));
JavaNode node2 = new DummyJavaNode(2);
node2.setImage("Foo.X");
assertTrue(s.contains(new JavaNameOccurrence(node2, node2.getImage())));
}
use of net.sourceforge.pmd.lang.java.ast.JavaNode in project pmd by pmd.
the class ScopeAndDeclarationFinder method visit.
@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
/*
* Local variables declared inside the constructor need to be in a
* different scope so special handling is needed
*/
createMethodScope(node);
Scope methodScope = node.getScope();
Node formalParameters = node.jjtGetChild(0);
int i = 1;
int n = node.jjtGetNumChildren();
if (!(formalParameters instanceof ASTFormalParameters)) {
visit((ASTTypeParameters) formalParameters, data);
formalParameters = node.jjtGetChild(1);
i++;
}
visit((ASTFormalParameters) formalParameters, data);
Scope localScope = null;
for (; i < n; i++) {
JavaNode b = (JavaNode) node.jjtGetChild(i);
if (b instanceof ASTBlockStatement) {
if (localScope == null) {
createLocalScope(node);
localScope = node.getScope();
}
b.setScope(localScope);
visit(b, data);
} else {
visit(b, data);
}
}
if (localScope != null) {
// pop the local scope
scopes.pop();
// reset the correct scope for the constructor
node.setScope(methodScope);
}
// pop the method scope
scopes.pop();
return data;
}
use of net.sourceforge.pmd.lang.java.ast.JavaNode in project pmd by pmd.
the class AbstractNcssCountRule method visit.
@Override
public Object visit(JavaNode node, Object data) {
int numNodes = 0;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
JavaNode n = (JavaNode) node.jjtGetChild(i);
Integer treeSize = (Integer) n.jjtAccept(this, data);
numNodes += treeSize.intValue();
}
if (this.nodeClass.isInstance(node)) {
// Add 1 to account for base node
numNodes++;
DataPoint point = new DataPoint();
point.setNode(node);
point.setScore(1.0 * numNodes);
point.setMessage(getMessage());
addDataPoint(point);
}
return Integer.valueOf(numNodes);
}
use of net.sourceforge.pmd.lang.java.ast.JavaNode in project pmd by pmd.
the class CycloMetric method computeFor.
// TODO:cf Cyclo should develop factorized boolean operators to count them
@Override
public double computeFor(final MethodLikeNode node, MetricOptions options) {
Set<MetricOption> opts = options.getOptions();
JavaParserDecoratedVisitor visitor = new // TODO decorators are unmaintainable, change that someday
JavaParserDecoratedVisitor(// TODO decorators are unmaintainable, change that someday
CycloBaseVisitor.INSTANCE) {
// stops the visit when stumbling on a lambda or class decl
@Override
public Object visit(JavaNode localNode, Object data) {
// TODO generalize that to other metrics
return localNode.isFindBoundary() && !localNode.equals(node) ? data : super.visit(localNode, data);
}
};
if (opts.contains(CycloOption.CONSIDER_ASSERT)) {
visitor.decorateWith(new CycloAssertAwareDecorator());
}
if (!opts.contains(CycloOption.IGNORE_BOOLEAN_PATHS)) {
visitor.decorateWith(new CycloPathAwareDecorator());
}
MutableInt cyclo = (MutableInt) node.jjtAccept(visitor, new MutableInt(1));
return (double) cyclo.getValue();
}
Aggregations