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