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());
}
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());
}
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);
}
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;
}
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());
}
Aggregations