use of net.sourceforge.pmd.RuleContext in project Gargoyle by callakrsos.
the class DoPMD method doPMD.
public int doPMD(GargoylePMDConfiguration configuration, List<ReportListener> listeners) {
renerderWriter = RendererWriterFactory.get(WRITER_TYPE.StringWriter);
// Load the RuleSets
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration);
RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(), ruleSetFactory);
if (ruleSets == null) {
return 0;
}
Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
List<DataSource> files = getApplicableFiles(configuration, languages);
long reportStart = System.nanoTime();
try {
// Renderer renderer = RendererFactory.createRenderer(configuration.getReportFormat(), configuration.getReportProperties()); //configuration.createRenderer();//createDefaultRenderer();
List<Renderer> renderers = new LinkedList<>();
//configuration.createRenderer();
Renderer renderer = new DatabaseXmlRenderer();
renderer.setWriter(renerderWriter.getWriter());
renderer.start();
renderers.add(renderer);
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
RuleContext ctx = new RuleContext();
final AtomicInteger violations = new AtomicInteger(0);
ctx.getReport().addListener(new ReportListener() {
@Override
public void ruleViolationAdded(RuleViolation ruleViolation) {
violations.incrementAndGet();
}
@Override
public void metricAdded(Metric metric) {
}
});
if (listeners != null && !listeners.isEmpty()) {
for (ReportListener l : listeners) ctx.getReport().addListener(l);
}
processFiles(configuration, ruleSetFactory, files, ctx, renderers);
reportStart = System.nanoTime();
// renderer.renderFileReport();
renderer.end();
renderer.flush();
return violations.get();
} catch (Exception e) {
LOGGER.error(ValueUtil.toString(e));
return 0;
} finally {
Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
try {
close();
} catch (IOException e) {
LOGGER.error(ValueUtil.toString(e));
}
}
}
use of net.sourceforge.pmd.RuleContext 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.RuleContext 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.RuleContext 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.RuleContext in project pmd by pmd.
the class XPathRuleTest method getReportForTestString.
private static Report getReportForTestString(Rule r, String test) throws PMDException {
PMD p = new PMD();
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport(report);
ctx.setSourceCodeFilename("n/a");
RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(r);
p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
return report;
}
Aggregations