Search in sources :

Example 1 with RuleContext

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));
        }
    }
}
Also used : ReportListener(net.sourceforge.pmd.ReportListener) RuleContext(net.sourceforge.pmd.RuleContext) RuleViolation(net.sourceforge.pmd.RuleViolation) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) Language(net.sourceforge.pmd.lang.Language) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuleSets(net.sourceforge.pmd.RuleSets) Renderer(net.sourceforge.pmd.renderers.Renderer) Metric(net.sourceforge.pmd.stat.Metric)

Example 2 with RuleContext

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());
}
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 3 with RuleContext

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());
}
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 4 with RuleContext

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());
}
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 5 with RuleContext

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;
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) PMD(net.sourceforge.pmd.PMD) RuleSets(net.sourceforge.pmd.RuleSets) StringReader(java.io.StringReader)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4