Search in sources :

Example 11 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd by pmd.

the class Benchmarker method stress.

/**
 * @param languageVersion
 *            LanguageVersion
 * @param ruleSet
 *            RuleSet
 * @param dataSources
 *            List<DataSource>
 * @param results
 *            Set<RuleDuration>
 * @param debug
 *            boolean
 * @throws PMDException
 * @throws IOException
 */
private static void stress(LanguageVersion languageVersion, RuleSet ruleSet, List<DataSource> dataSources, Set<RuleDuration> results, boolean debug) throws PMDException, IOException {
    for (Rule rule : ruleSet.getRules()) {
        if (debug) {
            System.out.println("Starting " + rule.getName());
        }
        final RuleSet working = RuleSet.forSingleRule(rule);
        RuleSets ruleSets = new RuleSets(working);
        PMDConfiguration config = new PMDConfiguration();
        config.setDefaultLanguageVersion(languageVersion);
        RuleContext ctx = new RuleContext();
        long start = System.currentTimeMillis();
        for (DataSource ds : dataSources) {
            try (DataSource dataSource = ds;
                InputStream stream = new BufferedInputStream(dataSource.getInputStream())) {
                ctx.setSourceCodeFile(new File(dataSource.getNiceFileName(false, null)));
                new SourceCodeProcessor(config).processSourceCode(stream, ruleSets, ctx);
            }
        }
        long end = System.currentTimeMillis();
        long elapsed = end - start;
        results.add(new RuleDuration(elapsed, rule));
        if (debug) {
            System.out.println("Done timing " + rule.getName() + "; elapsed time was " + elapsed);
        }
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) BufferedInputStream(java.io.BufferedInputStream) RuleSets(net.sourceforge.pmd.RuleSets) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) Rule(net.sourceforge.pmd.Rule) File(java.io.File) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) DataSource(net.sourceforge.pmd.util.datasource.DataSource)

Example 12 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd by pmd.

the class XPathMetricFunctionTest method getViolations.

private Iterator<RuleViolation> getViolations(Rule rule, String code) throws PMDException {
    RuleContext ctx = new RuleContext();
    Report report = new Report();
    ctx.setReport(report);
    ctx.setSourceCodeFile(new File("n/a"));
    // for test, we want immediate exceptions thrown and not collect them
    ctx.setIgnoreExceptions(false);
    RuleSet rules = RuleSet.forSingleRule(rule);
    SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(new PMDConfiguration());
    sourceCodeProcessor.processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
    return report.iterator();
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) RuleSets(net.sourceforge.pmd.RuleSets) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) StringReader(java.io.StringReader) File(java.io.File) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Example 13 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.

the class AbstractStructureInspectorPage method getPMDMethods.

/**
 * Gets a List of all PMD-Methods.
 *
 * @return an List of ASTMethodDeclarations
 */
private List<ASTMethodDeclaration> getPMDMethods() {
    List<ASTMethodDeclaration> methodList = new ArrayList<>();
    // PMD needs this Resource as a String
    try {
        DFAGraphRule dfaGraphRule = new JavaDFAGraphRule();
        RuleSet rs = RuleSetUtil.newSingle(dfaGraphRule);
        RuleContext ctx = new RuleContext();
        ctx.setSourceCodeFile(new File("[scratchpad]"));
        // StringReader reader = new StringReader(getDocument().get());
        // run PMD using the DFAGraphRule and the Text of the Resource
        // new PMDEngine().processFile(reader, rs, ctx);
        byte[] bytes = getDocument().get().getBytes();
        InputStream input = new ByteArrayInputStream(bytes);
        new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(input, InternalRuleSetUtil.toRuleSets(Collections.singletonList(rs)), ctx);
        // the Rule then can give us the Methods
        for (DFAGraphMethod m : dfaGraphRule.getMethods()) {
            if (m instanceof ASTMethodDeclaration) {
                methodList.add((ASTMethodDeclaration) m);
            }
        }
        Collections.sort(methodList, ASTUtil.METHOD_COMPARATOR);
    } catch (PMDException pmde) {
        logError(StringKeys.ERROR_PMD_EXCEPTION + toString(), pmde);
    }
    return methodList;
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) RuleContext(net.sourceforge.pmd.RuleContext) JavaDFAGraphRule(net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) DFAGraphMethod(net.sourceforge.pmd.lang.dfa.DFAGraphMethod) JavaDFAGraphRule(net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule) DFAGraphRule(net.sourceforge.pmd.lang.dfa.DFAGraphRule) ByteArrayInputStream(java.io.ByteArrayInputStream) PMDException(net.sourceforge.pmd.PMDException) File(java.io.File) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Aggregations

SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)13 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)11 RuleContext (net.sourceforge.pmd.RuleContext)10 RuleSet (net.sourceforge.pmd.RuleSet)9 RuleSets (net.sourceforge.pmd.RuleSets)9 PMDException (net.sourceforge.pmd.PMDException)8 File (java.io.File)7 Report (net.sourceforge.pmd.Report)6 RuleViolation (net.sourceforge.pmd.RuleViolation)5 StringReader (java.io.StringReader)4 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)3 DataSource (net.sourceforge.pmd.util.datasource.DataSource)3 BufferedInputStream (java.io.BufferedInputStream)2 Language (net.sourceforge.pmd.lang.Language)2 CoreException (org.eclipse.core.runtime.CoreException)2 Range (ch.acanda.eclipse.pmd.marker.MarkerUtil.Range)1