Search in sources :

Example 1 with JavaNode

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())));
}
Also used : DummyJavaNode(net.sourceforge.pmd.lang.java.ast.DummyJavaNode) DummyJavaNode(net.sourceforge.pmd.lang.java.ast.DummyJavaNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 2 with JavaNode

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())));
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) DummyJavaNode(net.sourceforge.pmd.lang.java.ast.DummyJavaNode) DummyJavaNode(net.sourceforge.pmd.lang.java.ast.DummyJavaNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 3 with JavaNode

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;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)

Example 4 with JavaNode

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);
}
Also used : DataPoint(net.sourceforge.pmd.stat.DataPoint) DataPoint(net.sourceforge.pmd.stat.DataPoint) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 5 with JavaNode

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();
}
Also used : CycloPathAwareDecorator(net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloPathAwareDecorator) CycloAssertAwareDecorator(net.sourceforge.pmd.lang.java.metrics.impl.visitors.CycloAssertAwareDecorator) JavaParserDecoratedVisitor(net.sourceforge.pmd.lang.java.ast.JavaParserDecoratedVisitor) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MetricOption(net.sourceforge.pmd.lang.metrics.MetricOption) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

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