use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class ClassScopeTest method testNestedClassesResolution.
@Test
public void testNestedClassesResolution() {
parseForClass(InnerClass.class);
final ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
final ClassScope c = (ClassScope) n.getScope();
assertEquals(InnerClass.class, c.resolveType("InnerClass"));
assertEquals(TheInnerClass.class, c.resolveType("InnerClass.TheInnerClass"));
// Within this scope, we can access it directly
assertEquals(TheInnerClass.class, c.resolveType("TheInnerClass"));
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class MethodNameDeclarationTest method testEquality.
@Test
public void testEquality() {
// Verify proper number of nodes are not equal
parseCode15(SIMILAR);
ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
Set<NameDeclaration> methodNameDeclarations = m.keySet();
assertEquals("Wrong number of method name declarations", methodNameDeclarations.size(), 3);
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class ClassScope method resolveGenericType.
/**
* Tries to resolve a given typeImage as a generic Type. If the Generic Type
* is found, any defined ClassOrInterfaceType below this type declaration is
* used (this is typically a type bound, e.g. {@code <T extends List>}.
*
* @param argument
* the node, from where to start searching.
* @param typeImage
* the type as string
* @return the resolved class or <code>null</code> if nothing was found.
*/
private Class<?> resolveGenericType(Node argument, String typeImage) {
List<ASTTypeParameter> types = new ArrayList<>();
// first search only within the same method
ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
if (firstParentOfType != null) {
types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class));
}
// then search class level types, from inner-most to outer-most
List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) {
ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class);
if (classLevelTypeParameters != null) {
types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class));
}
}
return resolveGenericType(typeImage, types);
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class ClassTypeResolver method getEnclosingTypeDeclaration.
/**
* Returns the the first Class declaration around the node.
*
* @param node The node with the enclosing Class declaration.
* @return The JavaTypeDefinition of the enclosing Class declaration.
*/
private TypeNode getEnclosingTypeDeclaration(Node node) {
Node previousNode = null;
while (node != null) {
if (node instanceof ASTClassOrInterfaceDeclaration) {
return (TypeNode) node;
// anonymous class declaration
} else if (// is anonymous class declaration
node instanceof ASTAllocationExpression && // array cant be anonymous
node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null && !(previousNode instanceof ASTArguments)) {
// we might come out of the constructor
return (TypeNode) node;
}
previousNode = node;
node = node.jjtGetParent();
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration in project pmd by pmd.
the class UnnecessaryModifierRule method visit.
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface() && node.isAbstract()) {
// an abstract interface
addViolation(data, node, getMessage());
}
if (!node.isNested()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
boolean isParentInterfaceOrAnnotation = parent instanceof ASTAnnotationTypeDeclaration || parent instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) parent).isInterface();
// a public interface within an interface or annotation
if (node.isInterface() && node.isPublic() && isParentInterfaceOrAnnotation) {
addViolation(data, node, getMessage());
}
if (node.isInterface() && node.isStatic()) {
// a static interface
addViolation(data, node, getMessage());
}
// a public and/or static class within an interface or annotation
if (!node.isInterface() && (node.isPublic() || node.isStatic()) && isParentInterfaceOrAnnotation) {
addViolation(data, node, getMessage());
}
return data;
}
Aggregations