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());
}
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());
}
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());
}
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;
}
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;
}
Aggregations