Search in sources :

Example 21 with ASTCompilationUnit

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

the class JavaRuleViolationTest method testMethodName.

/**
 * Tests that the method name is taken correctly from the given node.
 *
 * @see <a href="https://sourceforge.net/p/pmd/bugs/1250/">#1250</a>
 */
@Test
public void testMethodName() {
    ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
    ASTMethodDeclaration md = ast.getFirstDescendantOfType(ASTMethodDeclaration.class);
    final RuleContext context = new RuleContext();
    final JavaRuleViolation violation = new JavaRuleViolation(null, context, md, null);
    assertEquals("bar", violation.getMethodName());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) RuleContext(net.sourceforge.pmd.RuleContext) Test(org.junit.Test)

Example 22 with ASTCompilationUnit

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

the class JavaRuleViolationTest method testASTFormalParameterVariableName.

/**
 * Verifies that {@link JavaRuleViolation} sets the variable name for an
 * {@link ASTFormalParameter} node.
 */
@Test
public void testASTFormalParameterVariableName() {
    ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
    final ASTFormalParameter node = ast.getFirstDescendantOfType(ASTFormalParameter.class);
    final RuleContext context = new RuleContext();
    final JavaRuleViolation violation = new JavaRuleViolation(null, context, node, null);
    assertEquals("x", violation.getVariableName());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 23 with ASTCompilationUnit

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

the class JavaRuleViolationTest method testPackageAndClassName.

/**
 * Tests that the class name is taken correctly, even if the node is outside
 * of a class scope, e.g. a import declaration.
 *
 * @see <a href="https://sourceforge.net/p/pmd/bugs/1529/">#1529</a>
 */
@Test
public void testPackageAndClassName() {
    ASTCompilationUnit ast = parse("package pkg; import java.util.List; public class Foo { }");
    ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
    JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
    assertEquals("pkg", 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 24 with ASTCompilationUnit

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

the class JavaRuleViolationTest method parse.

private ASTCompilationUnit parse(final String code) {
    final LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
    final ParserOptions options = languageVersionHandler.getDefaultParserOptions();
    final ASTCompilationUnit ast = (ASTCompilationUnit) languageVersionHandler.getParser(options).parse(null, new StringReader(code));
    // set scope of AST nodes
    ast.jjtAccept(new ScopeAndDeclarationFinder(), null);
    return ast;
}
Also used : ScopeAndDeclarationFinder(net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder) ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) StringReader(java.io.StringReader) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

Example 25 with ASTCompilationUnit

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

the class ParserTstUtil method buildDFA.

public static ASTCompilationUnit buildDFA(String javaCode) {
    LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
    ASTCompilationUnit cu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
    JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[] { JavaParserVisitor.class }, new Collector<>(ASTCompilationUnit.class));
    jpv.visit(cu, null);
    new SymbolFacade().initializeWith(cu);
    new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
    return cu;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) SymbolFacade(net.sourceforge.pmd.lang.java.symboltable.SymbolFacade) StringReader(java.io.StringReader) DataFlowFacade(net.sourceforge.pmd.lang.java.dfa.DataFlowFacade) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) JavaParserVisitor(net.sourceforge.pmd.lang.java.ast.JavaParserVisitor)

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