Search in sources :

Example 71 with ASTCompilationUnit

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

the class GeneralFiddlingTest method innerClassShouldWork.

/**
 * Unit test for https://sourceforge.net/p/pmd/bugs/1325/
 */
@Test
public void innerClassShouldWork() {
    ASTCompilationUnit acu = buildDFA("class Foo {" + "    void bar() {" + "        class X {}" + "        int i;" + "    }" + "}");
    assertNotNull(acu);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 72 with ASTCompilationUnit

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

the class GeneralFiddlingTest method test1.

@Test
public void test1() {
    ASTCompilationUnit acu = buildDFA(TEST1);
    ASTMethodDeclarator meth = acu.findDescendantsOfType(ASTMethodDeclarator.class).get(0);
    DataFlowNode n = meth.getDataFlowNode();
    List<DataFlowNode> f = n.getFlow();
    assertEquals(6, f.size());
    assertEquals("Undefinition(x)", String.valueOf(f.get(0).getVariableAccess().get(0)));
    assertEquals(0, f.get(1).getVariableAccess().size());
    assertEquals("Definition(x)", String.valueOf(f.get(2).getVariableAccess().get(0)));
    assertEquals("Reference(x)", String.valueOf(f.get(3).getVariableAccess().get(0)));
    assertEquals("Definition(x)", String.valueOf(f.get(4).getVariableAccess().get(0)));
    assertEquals("Undefinition(x)", String.valueOf(f.get(5).getVariableAccess().get(0)));
// for (DataFlowNode dfan : f) {
// System.out.println("Flow starting on line " + dfan.getLine());
// List<VariableAccess> va = dfan.getVariableAccess();
// for (VariableAccess o : va) {
// System.out.println(" variable: " + o);
// }
// }
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Test(org.junit.Test)

Example 73 with ASTCompilationUnit

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

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

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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)

Aggregations

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2