Skip to content

Commit 30e53e9

Browse files
committed
Avoid warnings about not providing cache for incremental analysis
1 parent 2fd502b commit 30e53e9

File tree

2 files changed

+16
-43
lines changed
  • net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse
  • net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd

2 files changed

+16
-43
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/BasicPMDTest.java

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,24 @@
55
package net.sourceforge.pmd.eclipse;
66

77
import java.io.ByteArrayInputStream;
8-
import java.io.File;
98
import java.io.IOException;
109
import java.io.InputStream;
11-
import java.io.UnsupportedEncodingException;
10+
import java.nio.charset.StandardCharsets;
1211
import java.util.ArrayList;
1312
import java.util.Collections;
14-
import java.util.Iterator;
1513
import java.util.List;
1614

1715
import org.junit.Assert;
1816
import org.junit.Test;
1917

2018
import net.sourceforge.pmd.PMD;
2119
import net.sourceforge.pmd.PMDConfiguration;
22-
import net.sourceforge.pmd.RuleContext;
20+
import net.sourceforge.pmd.Report;
2321
import net.sourceforge.pmd.RuleSet;
24-
import net.sourceforge.pmd.RuleSetFactory;
25-
import net.sourceforge.pmd.RuleSetNotFoundException;
22+
import net.sourceforge.pmd.RuleSetLoader;
2623
import net.sourceforge.pmd.RuleViolation;
27-
import net.sourceforge.pmd.RulesetsFactoryUtils;
28-
import net.sourceforge.pmd.ThreadSafeReportListener;
2924
import net.sourceforge.pmd.lang.LanguageRegistry;
3025
import net.sourceforge.pmd.renderers.Renderer;
31-
import net.sourceforge.pmd.stat.Metric;
3226
import net.sourceforge.pmd.util.datasource.DataSource;
3327

3428
/**
@@ -43,11 +37,7 @@ static class StringDataSource implements DataSource {
4337
private final ByteArrayInputStream is;
4438

4539
StringDataSource(final String source) {
46-
try {
47-
this.is = new ByteArrayInputStream(source.getBytes("UTF-8"));
48-
} catch (UnsupportedEncodingException e) {
49-
throw new RuntimeException(e);
50-
}
40+
this.is = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
5141
}
5242

5343
@Override
@@ -72,49 +62,29 @@ public void close() throws IOException {
7262
*/
7363
@Test
7464
public void testDefaulltRuleSets() {
75-
try {
76-
final RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory();
77-
final Iterator<RuleSet> iterator = factory.getRegisteredRuleSets();
78-
while (iterator.hasNext()) {
79-
iterator.next();
80-
}
81-
} catch (final RuleSetNotFoundException e) {
82-
e.printStackTrace();
83-
Assert.fail("unable to load registered rulesets ");
84-
}
65+
RuleSetLoader rulesetloader = new RuleSetLoader();
66+
List<RuleSet> standardRuleSets = rulesetloader.getStandardRuleSets();
67+
Assert.assertFalse("No Rulesets found", standardRuleSets.isEmpty());
8568
}
8669

8770
private void runPmd(String javaVersion) {
8871
PMDConfiguration configuration = new PMDConfiguration();
8972
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion(javaVersion));
9073
configuration.setRuleSets("category/java/codestyle.xml/UnnecessaryReturn");
91-
RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.createFactory(configuration);
74+
configuration.setIgnoreIncrementalAnalysis(true);
75+
RuleSetLoader rulesetLoader = RuleSetLoader.fromPmdConfig(configuration);
76+
List<RuleSet> rulesets = rulesetLoader.loadFromResources(configuration.getRuleSets());
9277

9378
List<DataSource> files = new ArrayList<>();
9479
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
9580
files.add(new StringDataSource(sourceCode));
9681

97-
final List<RuleViolation> violations = new ArrayList<>();
98-
final RuleContext ctx = new RuleContext();
99-
ctx.setSourceCodeFile(new File("foo.java"));
100-
ctx.getReport().addListener(new ThreadSafeReportListener() {
101-
@Override
102-
public void ruleViolationAdded(RuleViolation ruleViolation) {
103-
violations.add(ruleViolation);
104-
}
105-
106-
@Override
107-
public void metricAdded(Metric metric) {
108-
}
109-
});
110-
111-
112-
PMD.processFiles(configuration, ruleSetFactory, files, ctx,
82+
Report result = PMD.processFiles(configuration, rulesets, files,
11383
Collections.<Renderer>emptyList());
11484

115-
Assert.assertFalse("There should be at least one violation", violations.isEmpty());
85+
Assert.assertFalse("There should be at least one violation", result.getViolations().isEmpty());
11686

117-
final RuleViolation violation = violations.get(0);
87+
final RuleViolation violation = result.getViolations().get(0);
11888
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
11989
Assert.assertEquals(3, violation.getBeginLine());
12090
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/BaseVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ protected final void reviewResource(IResource resource) {
316316
configuration().setClassLoader(projectProperties.getAuxClasspath());
317317
}
318318

319+
// Avoid warnings about not providing cache for incremental analysis
320+
configuration().setIgnoreIncrementalAnalysis(true);
321+
319322
final File sourceCodeFile = file.getRawLocation().toFile();
320323
if (included && InternalRuleSetUtil.ruleSetsApplies(ruleSets, sourceCodeFile) && isFileInWorkingSet(file)
321324
&& languageVersion != null) {

0 commit comments

Comments
 (0)