Skip to content

Commit 7766776

Browse files
committed
Use PmdAnalysis
1 parent de8246c commit 7766776

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
lines changed

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

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import java.io.Reader;
1212
import java.util.ArrayList;
1313
import java.util.Arrays;
14-
import java.util.Collections;
1514
import java.util.HashSet;
1615
import java.util.List;
1716
import java.util.Locale;
1817
import java.util.Map;
1918
import java.util.Set;
2019
import java.util.Stack;
2120

21+
import org.apache.commons.io.IOUtils;
2222
import org.apache.commons.lang3.StringUtils;
2323
import org.eclipse.core.resources.IFile;
2424
import org.eclipse.core.resources.IMarker;
@@ -30,15 +30,13 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33-
import net.sourceforge.pmd.PMD;
3433
import net.sourceforge.pmd.PMDConfiguration;
35-
import net.sourceforge.pmd.PMDException;
34+
import net.sourceforge.pmd.PmdAnalysis;
3635
import net.sourceforge.pmd.Report;
3736
import net.sourceforge.pmd.Report.ConfigurationError;
3837
import net.sourceforge.pmd.Report.ProcessingError;
3938
import net.sourceforge.pmd.Rule;
4039
import net.sourceforge.pmd.RuleSet;
41-
import net.sourceforge.pmd.RuleSets;
4240
import net.sourceforge.pmd.RuleViolation;
4341
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
4442
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
@@ -49,9 +47,6 @@
4947
import net.sourceforge.pmd.lang.LanguageVersion;
5048
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
5149
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
52-
import net.sourceforge.pmd.renderers.Renderer;
53-
import net.sourceforge.pmd.util.datasource.DataSource;
54-
import net.sourceforge.pmd.util.datasource.ReaderDataSource;
5550

5651
/**
5752
* Factor some useful features for visitors
@@ -167,27 +162,12 @@ public void worked(final int work) {
167162
}
168163
}
169164

170-
// /**
171-
// * @return Returns the pmdEngine.
172-
// */
173-
// public PMDEngine getPmdEngine() {
174-
// return pmdEngine;
175-
// }
176-
//
177-
// /**
178-
// * @param pmdEngine
179-
// * The pmdEngine to set.
180-
// */
181-
// public void setPmdEngine(final PMDEngine pmdEngine) {
182-
// this.pmdEngine = pmdEngine;
183-
// }
184-
185165
/**
186166
* @return Returns all the ruleSets.
187167
* @deprecated Use {@link #getRuleSetList()}
188168
*/
189169
@Deprecated
190-
public RuleSets getRuleSets() {
170+
public net.sourceforge.pmd.RuleSets getRuleSets() {
191171
return InternalRuleSetUtil.toRuleSets(this.ruleSets);
192172
}
193173

@@ -217,7 +197,7 @@ public void setRuleSet(RuleSet ruleSet) {
217197
* @deprecated Use {@link #setRuleSetList(List)}
218198
*/
219199
@Deprecated
220-
public void setRuleSets(final RuleSets ruleSets) {
200+
public void setRuleSets(final net.sourceforge.pmd.RuleSets ruleSets) {
221201
setRuleSetList(Arrays.asList(ruleSets.getAllRuleSets()));
222202
}
223203

@@ -316,24 +296,24 @@ protected final void reviewResource(IResource resource) {
316296

317297
long start = System.currentTimeMillis();
318298

299+
// need to disable multi threading, as the ruleset is
300+
// not recreated and shared between threads...
301+
// but as we anyway have only one file to process, it won't hurt
302+
// here.
303+
configuration().setThreads(0);
304+
319305
Report collectingReport = null;
320306

321307
try (Reader input = new InputStreamReader(file.getContents(), file.getCharset());
322-
DataSource dataSource = new ReaderDataSource(input, file.getRawLocation().toFile().getPath());) {
323-
324-
// getPmdEngine().processFile(input, getRuleSet(), context);
325-
// getPmdEngine().processFile(sourceCodeFile, getRuleSet(),
326-
// context);
327-
328-
329-
// need to disable multi threading, as the ruleset is
330-
// not recreated and shared between threads...
331-
// but as we anyway have only one file to process, it won't hurt
332-
// here.
333-
configuration().setThreads(0);
308+
PmdAnalysis pmdAnalysis = PmdAnalysis.create(configuration());) {
309+
310+
String sourceContents = IOUtils.toString(input);
311+
pmdAnalysis.files().addSourceFile(sourceContents, sourceCodeFile.getAbsolutePath());
312+
313+
pmdAnalysis.addRuleSets(getRuleSetList());
314+
334315
LOG.debug("PMD running on file {}", file.getName());
335-
collectingReport = PMD.processFiles(configuration(), ruleSets, Arrays.asList(dataSource),
336-
Collections.<Renderer>emptyList());
316+
collectingReport = pmdAnalysis.performAnalysisAndCollectReport();
337317
LOG.debug("PMD run finished.");
338318
}
339319

@@ -356,7 +336,7 @@ protected final void reviewResource(IResource resource) {
356336
.append("\n");
357337
}
358338
PMDPlugin.getDefault().logWarn(message.toString());
359-
throw new PMDException(message.toString());
339+
throw new RuntimeException(message.toString());
360340
}
361341

362342
updateMarkers(file, collectingReport.getViolations());
@@ -370,17 +350,16 @@ protected final void reviewResource(IResource resource) {
370350
} catch (CoreException e) {
371351
// TODO: complete message
372352
LOG.error("Core exception visiting " + file.getName(), e);
373-
} catch (PMDException e) {
374-
// TODO: complete message
375-
LOG.error("PMD exception visiting " + file.getName(), e);
376353
} catch (IOException e) {
377354
// TODO: complete message
378355
LOG.error("IO exception visiting " + file.getName(), e);
379356
} catch (PropertiesException e) {
380357
// TODO: complete message
381-
LOG.error("Properties exception visiting " + file.getName(), e);
358+
LOG.error("Properties exception visiting {}", file.getName(), e);
382359
} catch (IllegalArgumentException e) {
383360
LOG.error("Illegal argument: {}", e.toString(), e);
361+
} catch (RuntimeException e) {
362+
LOG.error("Runtime exception visiting {}", file.getName(), e);
384363
}
385364

386365
}

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,36 @@
55
package net.sourceforge.pmd.eclipse.runtime.cmd;
66

77
import java.io.File;
8+
import java.io.InputStreamReader;
9+
import java.io.Reader;
810
import java.util.ArrayList;
9-
import java.util.Collections;
1011
import java.util.List;
1112

13+
import org.apache.commons.io.IOUtils;
1214
import org.eclipse.core.resources.IFile;
1315
import org.eclipse.core.resources.IResource;
14-
import org.eclipse.core.runtime.CoreException;
1516
import org.eclipse.swt.widgets.Display;
1617
import org.eclipse.ui.IPropertyListener;
1718

18-
import net.sourceforge.pmd.PMD;
1919
import net.sourceforge.pmd.PMDConfiguration;
20-
import net.sourceforge.pmd.PMDException;
20+
import net.sourceforge.pmd.PmdAnalysis;
21+
import net.sourceforge.pmd.Report;
2122
import net.sourceforge.pmd.Rule;
22-
import net.sourceforge.pmd.RuleContext;
2323
import net.sourceforge.pmd.RuleSet;
24-
import net.sourceforge.pmd.SourceCodeProcessor;
2524
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
2625
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
27-
import net.sourceforge.pmd.eclipse.ui.actions.internal.InternalRuleSetUtil;
2826

2927
/**
3028
* This command reviews a resource - a file - for a specific rule.
3129
*
30+
* <p>Used for the DFA table.
31+
*
3232
* @author Sven
3333
*
3434
*/
3535
public class ReviewResourceForRuleCommand extends AbstractDefaultCommand {
3636

3737
private IResource resource;
38-
private RuleContext context;
3938
private Rule rule;
4039
private List<IPropertyListener> listenerList;
4140

@@ -80,36 +79,35 @@ public void reset() {
8079

8180
@Override
8281
public void execute() {
83-
// IProject project = resource.getProject();
8482
IFile file = (IFile) resource.getAdapter(IFile.class);
8583
beginTask("PMD checking for rule: " + rule.getName(), 1);
8684

8785
if (file != null) {
8886
RuleSet ruleSet = RuleSetUtil.newSingle(rule);
89-
// final PMDEngine pmdEngine = getPmdEngineForProject(project);
87+
9088
File sourceCodeFile = file.getFullPath().toFile();
9189
if (ruleSet.applies(sourceCodeFile)) {
92-
try {
93-
context = PMD.newRuleContext(file.getName(), sourceCodeFile);
94-
95-
// Reader input = new InputStreamReader(file.getContents(),
96-
// file.getCharset());
97-
new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(file.getContents(),
98-
InternalRuleSetUtil.toRuleSets(Collections.singletonList(ruleSet)),
99-
context);
100-
// input.close();
101-
// } catch (CoreException e) {
102-
// throw new CommandException(e);
103-
} catch (PMDException | CoreException e) {
90+
PMDConfiguration configuration = new PMDConfiguration();
91+
Report report = null;
92+
93+
try (Reader input = new InputStreamReader(file.getContents(), file.getCharset());
94+
PmdAnalysis pmdAnalysis = PmdAnalysis.create(configuration)) {
95+
96+
pmdAnalysis.addRuleSet(ruleSet);
97+
pmdAnalysis.files().addSourceFile(IOUtils.toString(input), sourceCodeFile.getAbsolutePath());
98+
99+
report = pmdAnalysis.performAnalysisAndCollectReport();
100+
} catch (Exception e) {
104101
throw new RuntimeException(e);
105102
}
106103

104+
final Report finalResult = report;
107105
// trigger event propertyChanged for all listeners
108106
Display.getDefault().asyncExec(new Runnable() {
109107
@Override
110108
public void run() {
111109
for (IPropertyListener listener : listenerList) {
112-
listener.propertyChanged(context.getReport().getViolations().iterator(),
110+
listener.propertyChanged(finalResult.getViolations().iterator(),
113111
PMDRuntimeConstants.PROPERTY_REVIEW);
114112
}
115113
}

0 commit comments

Comments
 (0)