use of net.sourceforge.pmd.SourceCodeProcessor in project Gargoyle by callakrsos.
the class PMDGargoyleThreadProcessor method processFiles.
public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files, RuleContext ctx, List<Renderer> renderers) {
// single threaded execution
RuleSets rs = createRuleSets(ruleSetFactory);
SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
for (DataSource dataSource : files) {
String niceFileName = filenameFrom(dataSource);
Report report = PMD.setupReport(rs, ctx, niceFileName);
// overtake the listener
//bug fix 2016-10-05 by kyj. 결과가 중복되서 출력됨.
// report.addSynchronizedListeners(ctx.getReport().getSynchronizedListeners());
// ctx.setReport(report);
// ctx.setSourceCodeFilename(niceFileName);
// if (LOG.isLoggable(Level.FINE)) {
// LOG.fine("Processing " + ctx.getSourceCodeFilename());
// }
rs.start(ctx);
for (Renderer r : renderers) {
r.startFileAnalysis(dataSource);
}
try {
InputStream stream = new BufferedInputStream(dataSource.getInputStream());
// ctx.setLanguageVersion(null);
processor.processSourceCode(stream, rs, ctx);
} catch (PMDException pmde) {
// LOGGER.error(ValueUtil.toString(pmde));
// if (LOG.isLoggable(Level.FINE)) {
// LOG.log(Level.FINE, "Error while processing file: " + niceFileName, pmde.getCause());
// }
report.addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
} catch (IOException ioe) {
// LOGGER.error(ValueUtil.toString(ioe));
// unexpected exception: log and stop executor service
addError(report, "Unable to read source file", ioe, niceFileName);
} catch (RuntimeException re) {
// LOGGER.error(ValueUtil.toString(re));
// unexpected exception: log and stop executor service
addError(report, "RuntimeException while processing file", re, niceFileName);
} catch (Exception e) {
LOGGER.error(ValueUtil.toString(e));
}
rs.end(ctx);
super.renderReports(renderers, ctx.getReport());
}
}
use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.
the class ReviewResourceForRuleCommand method execute.
/*
* (non-Javadoc)
*
* @see
* net.sourceforge.pmd.eclipse.runtime.cmd.AbstractDefaultCommand#execute()
*/
@Override
public void execute() throws CommandException {
// IProject project = resource.getProject();
IFile file = (IFile) resource.getAdapter(IFile.class);
beginTask("PMD checking for rule: " + rule.getName(), 1);
if (file != null) {
RuleSet ruleSet = RuleSetUtil.newSingle(rule);
// final PMDEngine pmdEngine = getPmdEngineForProject(project);
File sourceCodeFile = file.getFullPath().toFile();
if (ruleSet.applies(sourceCodeFile)) {
try {
context = PMD.newRuleContext(file.getName(), sourceCodeFile);
// Reader input = new InputStreamReader(file.getContents(),
// file.getCharset());
RuleSets rSets = new RuleSets(ruleSet);
new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(file.getContents(), rSets, context);
// input.close();
// } catch (CoreException e) {
// throw new CommandException(e);
} catch (PMDException e) {
throw new CommandException(e);
} catch (CoreException e) {
throw new CommandException(e);
}
// trigger event propertyChanged for all listeners
Display.getDefault().asyncExec(new Runnable() {
public void run() {
for (IPropertyListener listener : listenerList) {
listener.propertyChanged(context.getReport().iterator(), PMDRuntimeConstants.PROPERTY_REVIEW);
}
}
});
}
}
}
use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.
the class BasicPMDTest method testRunPmdJdk15.
/**
* Let see with Java 1.5
*/
@Test
public void testRunPmdJdk15() {
try {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.5"));
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
final RuleContext context = new RuleContext();
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
RuleSets rSets = new RuleSets(codeStyleRuleSet);
new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
final Iterator<RuleViolation> iter = context.getReport().iterator();
Assert.assertTrue("There should be at least one violation", iter.hasNext());
final RuleViolation violation = iter.next();
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
Assert.assertEquals(3, violation.getBeginLine());
} catch (final RuleSetNotFoundException e) {
e.printStackTrace();
Assert.fail();
} catch (final PMDException e) {
e.printStackTrace();
Assert.fail();
}
}
use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.
the class BasicPMDTest method testRunPmdJdk13.
/**
* One first thing the plugin must be able to do is to run PMD
*/
@Test
public void testRunPmdJdk13() {
try {
PMDConfiguration configuration = new PMDConfiguration();
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.3"));
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
final RuleContext context = new RuleContext();
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
RuleSets rSets = new RuleSets(codeStyleRuleSet);
new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
final Iterator<RuleViolation> iter = context.getReport().iterator();
Assert.assertTrue("There should be at least one violation", iter.hasNext());
final RuleViolation violation = iter.next();
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
Assert.assertEquals(3, violation.getBeginLine());
} catch (final RuleSetNotFoundException e) {
e.printStackTrace();
Assert.fail();
} catch (final PMDException e) {
e.printStackTrace();
Assert.fail();
}
}
use of net.sourceforge.pmd.SourceCodeProcessor in project eclipse-pmd by acanda.
the class Analyzer method runPMD.
private Iterable<RuleViolation> runPMD(final IFile file, final RuleSets ruleSets) {
try {
if (isValidFile(file, ruleSets)) {
final Language language = LANGUAGES.get(file.getFileExtension().toLowerCase());
if (isValidLanguage(language)) {
final PMDConfiguration configuration = new PMDConfiguration();
final InputStreamReader reader = new InputStreamReader(file.getContents(), file.getCharset());
final RuleContext context = PMD.newRuleContext(file.getName(), file.getRawLocation().toFile());
context.setLanguageVersion(language.getDefaultVersion());
context.setIgnoreExceptions(false);
new SourceCodeProcessor(configuration).processSourceCode(reader, ruleSets, context);
return ImmutableList.copyOf(context.getReport().iterator());
}
}
} catch (CoreException | IOException e) {
PMDPlugin.getDefault().error("Could not run PMD on file " + file.getRawLocation(), e);
} catch (final PMDException e) {
if (isIncorrectSyntaxCause(e)) {
PMDPlugin.getDefault().info("Could not run PMD because of incorrect syntax of file " + file.getRawLocation(), e);
} else {
PMDPlugin.getDefault().warn("Could not run PMD on file " + file.getRawLocation(), e);
}
}
return ImmutableList.<RuleViolation>of();
}
Aggregations