Search in sources :

Example 6 with ASTClassOrInterfaceDeclaration

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"));
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 7 with ASTClassOrInterfaceDeclaration

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);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 8 with ASTClassOrInterfaceDeclaration

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);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTTypeParameter(net.sourceforge.pmd.lang.java.ast.ASTTypeParameter) ASTTypeParameters(net.sourceforge.pmd.lang.java.ast.ASTTypeParameters)

Example 9 with ASTClassOrInterfaceDeclaration

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;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) QualifiableNode(net.sourceforge.pmd.lang.ast.QualifiableNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments)

Example 10 with ASTClassOrInterfaceDeclaration

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;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotationTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration)

Aggregations

ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)31 Test (org.junit.Test)18 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)15 List (java.util.List)14 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)12 Node (net.sourceforge.pmd.lang.ast.Node)7 Map (java.util.Map)4 ASTAnnotationTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)3 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)3 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)3 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)3 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 ArrayList (java.util.ArrayList)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)2 ASTTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration)2