Search in sources :

Example 1 with AbstractJavaNode

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

the class PrematureDeclarationRule method blocksAfter.

/**
 * Returns all the blocks found right after the node supplied within the its
 * current scope.
 *
 * @param block
 *            SimpleJavaNode
 * @param node
 *            SimpleNode
 * @return List
 */
private static List<ASTBlockStatement> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
    int count = block.jjtGetNumChildren();
    int start = indexOf(block, node.jjtGetParent()) + 1;
    List<ASTBlockStatement> nextBlocks = new ArrayList<>(count);
    for (int i = start; i < count; i++) {
        Node maybeBlock = block.jjtGetChild(i);
        if (maybeBlock instanceof ASTBlockStatement) {
            nextBlocks.add((ASTBlockStatement) maybeBlock);
        }
    }
    return nextBlocks;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ArrayList(java.util.ArrayList)

Example 2 with AbstractJavaNode

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

the class UnusedPrivateFieldRule method usedInOuter.

private boolean usedInOuter(NameDeclaration decl, JavaNode body) {
    List<ASTClassOrInterfaceBodyDeclaration> classOrInterfaceBodyDeclarations = body.findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class);
    List<ASTEnumConstant> enumConstants = body.findChildrenOfType(ASTEnumConstant.class);
    List<AbstractJavaNode> nodes = new ArrayList<>();
    nodes.addAll(classOrInterfaceBodyDeclarations);
    nodes.addAll(enumConstants);
    for (AbstractJavaNode node : nodes) {
        for (ASTPrimarySuffix primarySuffix : node.findDescendantsOfType(ASTPrimarySuffix.class, true)) {
            if (decl.getImage().equals(primarySuffix.getImage())) {
                // No violation
                return true;
            }
        }
        for (ASTPrimaryPrefix primaryPrefix : node.findDescendantsOfType(ASTPrimaryPrefix.class, true)) {
            ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
            if (name != null) {
                for (String id : name.getImage().split("\\.")) {
                    if (id.equals(decl.getImage())) {
                        // No violation
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ArrayList(java.util.ArrayList) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTEnumConstant(net.sourceforge.pmd.lang.java.ast.ASTEnumConstant)

Example 3 with AbstractJavaNode

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

the class PrematureDeclarationRule method visit.

/**
 * @param node
 *            ASTLocalVariableDeclaration
 * @param data
 *            Object
 * @return Object
 * @see net.sourceforge.pmd.lang.java.ast.JavaParserVisitor#visit(ASTLocalVariableDeclaration,
 *      Object)
 */
public Object visit(ASTLocalVariableDeclaration node, Object data) {
    // is it part of a for-loop declaration?
    if (node.jjtGetParent() instanceof ASTForInit) {
        // yes, those don't count
        return visit((AbstractJavaNode) node, data);
    }
    String varName = varNameIn(node);
    AbstractJavaNode grandparent = (AbstractJavaNode) node.jjtGetParent().jjtGetParent();
    List<ASTBlockStatement> nextBlocks = blocksAfter(grandparent, node);
    for (ASTBlockStatement block : nextBlocks) {
        if (hasReferencesIn(block, varName) || isLambda(block)) {
            break;
        }
        if (hasExit(block)) {
            addViolation(data, node, varName);
            break;
        }
    }
    return visit((AbstractJavaNode) node, data);
}
Also used : AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit)

Example 4 with AbstractJavaNode

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

the class ClassTypeResolverTest method testMethodParameterization.

@Test
public void testMethodParameterization() throws JaxenException, NoSuchMethodException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    JavaTypeDefinition context = forClass(GenericMethodsImplicit.class, forClass(Thread.class));
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    MethodType inferedMethod = MethodTypeResolution.parameterizeInvocation(context, method, argList);
    assertEquals(inferedMethod.getParameterTypes().get(0), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(1), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(2), forClass(Integer.class));
    assertEquals(inferedMethod.getParameterTypes().get(3), forClass(SuperClassAOther2.class));
}
Also used : MethodType(net.sourceforge.pmd.lang.java.typeresolution.MethodType) SuperClassA2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) Method(java.lang.reflect.Method) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) Test(org.junit.Test)

Example 5 with AbstractJavaNode

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

the class ClassTypeResolverTest method testMethodInitialConstraints.

@Test
public void testMethodInitialConstraints() throws NoSuchMethodException, JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    List<Variable> variables = new ArrayList<>();
    for (int i = 0; i < 2; ++i) {
        variables.add(new Variable());
    }
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    List<Constraint> constraints = MethodTypeResolution.produceInitialConstraints(method, argList, variables);
    assertEquals(constraints.size(), 3);
    // A
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassA.class), variables.get(0), LOOSE_INVOCATION)));
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther.class), variables.get(0), LOOSE_INVOCATION)));
    // B
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther2.class), variables.get(1), LOOSE_INVOCATION)));
}
Also used : Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) SuperClassAOther(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) SuperClassA(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA) Test(org.junit.Test)

Aggregations

AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)5 ArrayList (java.util.ArrayList)3 Method (java.lang.reflect.Method)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 SuperClassAOther2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2)2 Test (org.junit.Test)2 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)1 ASTEnumConstant (net.sourceforge.pmd.lang.java.ast.ASTEnumConstant)1 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 MethodType (net.sourceforge.pmd.lang.java.typeresolution.MethodType)1 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)1 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)1 Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)1 SuperClassA (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA)1