Search in sources :

Example 1 with ASTTypeDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration 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 2 with ASTTypeDeclaration

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

the class ClassTypeResolverTest method testExtraTopLevelClass.

@Test
public void testExtraTopLevelClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(ExtraTopLevelClass.class);
    Class<?> theExtraTopLevelClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.TheExtraTopLevelClass");
    // First class
    ASTTypeDeclaration typeDeclaration = (ASTTypeDeclaration) acu.jjtGetChild(1);
    assertEquals(ExtraTopLevelClass.class, typeDeclaration.getType());
    assertEquals(ExtraTopLevelClass.class, typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    // Second class
    typeDeclaration = (ASTTypeDeclaration) acu.jjtGetChild(2);
    assertEquals(theExtraTopLevelClass, typeDeclaration.getType());
    assertEquals(theExtraTopLevelClass, typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) Test(org.junit.Test)

Example 3 with ASTTypeDeclaration

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

the class NcssTypeCountRule method visit.

@Override
public Object visit(ASTEnumDeclaration node, Object data) {
    /*
         * If the enum is a type in and of itself, don't count its declaration
         * twice.
         */
    if (node.jjtGetParent() instanceof ASTTypeDeclaration) {
        Integer nodeCount = countNodeChildren(node, data);
        int count = nodeCount.intValue() - 1;
        return Integer.valueOf(count);
    }
    return countNodeChildren(node, data);
}
Also used : ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) DataPoint(net.sourceforge.pmd.stat.DataPoint)

Example 4 with ASTTypeDeclaration

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

the class MethodReturnsInternalArrayRule method visit.

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
    if (!method.getResultType().returnsArray() || method.isPrivate()) {
        return data;
    }
    List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class);
    ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class);
    for (ASTReturnStatement ret : returns) {
        final String vn = getReturnedVariableName(ret);
        if (!isField(vn, td)) {
            continue;
        }
        if (ret.findDescendantsOfType(ASTPrimarySuffix.class).size() > 2) {
            continue;
        }
        if (ret.hasDescendantOfType(ASTAllocationExpression.class)) {
            continue;
        }
        if (hasArraysCopyOf(ret)) {
            continue;
        }
        if (hasClone(ret, vn)) {
            continue;
        }
        if (isEmptyArray(vn, td)) {
            continue;
        }
        if (!isLocalVariable(vn, method)) {
            addViolation(data, ret, vn);
        } else {
            // This is to handle field hiding
            final ASTPrimaryPrefix pp = ret.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (pp != null && pp.usesThisModifier()) {
                final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (ps.hasImageEqualTo(vn)) {
                    addViolation(data, ret, vn);
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 5 with ASTTypeDeclaration

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

the class ClassTypeResolverTest method testAnonymousInnerClass.

@Test
public void testAnonymousInnerClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(AnonymousInnerClass.class);
    Class<?> theAnonymousInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass$1");
    // Outer class
    ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
    assertEquals(AnonymousInnerClass.class, typeDeclaration.getType());
    ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
    assertEquals(AnonymousInnerClass.class, outerClassDeclaration.getType());
    // Anonymous Inner class
    assertEquals(theAnonymousInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTAllocationExpression.class).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) Test(org.junit.Test)

Aggregations

ASTTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)3 Test (org.junit.Test)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1 DataPoint (net.sourceforge.pmd.stat.DataPoint)1