Search in sources :

Example 6 with ASTImportDeclaration

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

the class ClassTypeResolverTest method acceptanceTest.

@Test
public void acceptanceTest() {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(ArrayListFound.class);
    assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTTypeDeclaration.class).getType());
    assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    ASTImportDeclaration id = acu.getFirstDescendantOfType(ASTImportDeclaration.class);
    assertEquals("java.util", id.getPackage().getName());
    assertEquals(ArrayList.class, id.getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTReferenceType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclarator.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTFieldDeclaration.class).getType());
    acu = parseAndTypeResolveForClass15(DefaultJavaLangImport.class);
    assertEquals(String.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
    assertEquals(Override.class, acu.findDescendantsOfType(ASTName.class).get(1).getType());
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) DefaultJavaLangImport(net.sourceforge.pmd.typeresolution.testdata.DefaultJavaLangImport) Test(org.junit.Test)

Example 7 with ASTImportDeclaration

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

the class JavaRuleViolationTest method testPackageAndEnumName.

@Test
public void testPackageAndEnumName() {
    ASTCompilationUnit ast = parse("package pkg; import java.util.List; public enum FooE { }");
    ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
    JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
    assertEquals("pkg", violation.getPackageName());
    assertEquals("FooE", violation.getClassName());
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) Test(org.junit.Test)

Example 8 with ASTImportDeclaration

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

the class JavaRuleViolationTest method testPackageAndMultipleClassesName.

@Test
public void testPackageAndMultipleClassesName() {
    ASTCompilationUnit ast = parse("package pkg; import java.util.List; class Foo { } public class Bar { }");
    ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
    JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
    assertEquals("pkg", violation.getPackageName());
    assertEquals("Bar", violation.getClassName());
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) Test(org.junit.Test)

Example 9 with ASTImportDeclaration

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

the class JavaRuleViolationTest method testDefaultPackageAndClassName.

@Test
public void testDefaultPackageAndClassName() {
    ASTCompilationUnit ast = parse("import java.util.List; public class Foo { }");
    ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
    JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
    assertEquals("", violation.getPackageName());
    assertEquals("Foo", violation.getClassName());
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) Test(org.junit.Test)

Example 10 with ASTImportDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration in project pmd-eclipse-plugin by pmd.

the class ASTContentProvider method withoutHiddenOnes.

private List<Node> withoutHiddenOnes(Object parent) {
    List<Node> kids = new ArrayList<Node>();
    if (includeComments && parent instanceof ASTCompilationUnit) {
        // if (!hiddenNodeTypes.contains(Comment.class)) {
        List<Comment> comments = ((ASTCompilationUnit) parent).getComments();
        kids.addAll(comments);
    // }
    }
    AbstractNode node = (AbstractNode) parent;
    int kidCount = node.jjtGetNumChildren();
    for (int i = 0; i < kidCount; i++) {
        Node kid = node.jjtGetChild(i);
        // if (hiddenNodeTypes.contains(kid.getClass())) continue;
        if (!includeImports && kid instanceof ASTImportDeclaration) {
            continue;
        }
        if (!includeComments && kid instanceof Comment) {
            continue;
        }
        kids.add(kid);
    }
    Collections.sort(kids, BY_LINE_NUMBER);
    return kids;
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment) ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList)

Aggregations

ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)10 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)7 Test (org.junit.Test)5 RuleContext (net.sourceforge.pmd.RuleContext)4 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 Comment (net.sourceforge.pmd.lang.java.ast.Comment)1 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)1 DefaultJavaLangImport (net.sourceforge.pmd.typeresolution.testdata.DefaultJavaLangImport)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1