Search in sources :

Example 1 with ASTFormalParameter

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

the class VariableNameDeclarationTest method testExceptionBlkParam.

@Test
public void testExceptionBlkParam() {
    ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
    id.testingOnlySetBeginLine(10);
    id.setImage("foo");
    ASTFormalParameter param = new ASTFormalParameter(2);
    id.jjtSetParent(param);
    param.jjtSetParent(new ASTTryStatement(1));
    VariableNameDeclaration decl = new VariableNameDeclaration(id);
    assertTrue(decl.isExceptionBlockParameter());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 2 with ASTFormalParameter

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

the class ClassTypeResolverTest method testInnerClass.

@Test
public void testInnerClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(InnerClass.class);
    Class<?> theInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.InnerClass$TheInnerClass");
    // Outer class
    ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
    assertEquals(InnerClass.class, typeDeclaration.getType());
    ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
    assertEquals(InnerClass.class, outerClassDeclaration.getType());
    // Inner class
    assertEquals(theInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    // Method parameter as inner class
    ASTFormalParameter formalParameter = typeDeclaration.getFirstDescendantOfType(ASTFormalParameter.class);
    assertEquals(theInnerClass, formalParameter.getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 3 with ASTFormalParameter

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

the class JavaRuleViolationTest method testASTFormalParameterVariableName.

/**
 * Verifies that {@link JavaRuleViolation} sets the variable name for an
 * {@link ASTFormalParameter} node.
 */
@Test
public void testASTFormalParameterVariableName() {
    ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
    final ASTFormalParameter node = ast.getFirstDescendantOfType(ASTFormalParameter.class);
    final RuleContext context = new RuleContext();
    final JavaRuleViolation violation = new JavaRuleViolation(null, context, node, null);
    assertEquals("x", violation.getVariableName());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 4 with ASTFormalParameter

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

the class VariableAccessVisitor method markUsages.

private List<VariableAccess> markUsages(DataFlowNode inode) {
    // undefinitions was once a field... seems like it works fine as a local
    List<VariableAccess> undefinitions = new ArrayList<>();
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            VariableNameDeclaration vnd = entry.getKey();
            if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
                // add definition for initialized variables
                addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
            }
            undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
            for (NameOccurrence occurrence : entry.getValue()) {
                addAccess((JavaNameOccurrence) occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 5 with ASTFormalParameter

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

the class ExceptionAsFlowControlRule method visit.

@Override
public Object visit(ASTThrowStatement node, Object data) {
    ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class);
    if (parent == null) {
        return data;
    }
    for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent.getFirstParentOfType(ASTTryStatement.class)) {
        List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class);
        for (ASTCatchStatement catchStmt : list) {
            ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0);
            ASTType type = fp.getFirstDescendantOfType(ASTType.class);
            ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
            if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
                addViolation(data, name);
            }
        }
    }
    return data;
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Aggregations

ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)21 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)5 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ArrayList (java.util.ArrayList)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 Test (org.junit.Test)3 List (java.util.List)2 Map (java.util.Map)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectInputStream (java.io.ObjectInputStream)1 RuleContext (net.sourceforge.pmd.RuleContext)1 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1