Skip to content

Commit b720389

Browse files
committed
Fix more deprecations
1 parent bdbf9c8 commit b720389

File tree

7 files changed

+77
-91
lines changed

7 files changed

+77
-91
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleUIUtilTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.sourceforge.pmd.Rule;
1212
import net.sourceforge.pmd.eclipse.ui.IndexedString;
1313
import net.sourceforge.pmd.lang.rule.XPathRule;
14+
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
1415

1516
public class RuleUIUtilTest {
1617

@@ -19,7 +20,7 @@ public class RuleUIUtilTest {
1920
*/
2021
@Test
2122
public void testTndexedPropertyStringFromRule() {
22-
Rule rule = new XPathRule();
23+
Rule rule = new XPathRule(XPathVersion.XPATH_2_0, "");
2324
IndexedString s = RuleUIUtil.indexedPropertyStringFrom(rule);
2425
assertNotNull(s);
2526
}

net.sourceforge.pmd.eclipse.plugin/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Export-Package: ch.qos.logback.classic;x-friends:="net.sourceforge.pmd.eclipse.p
162162
net.sourceforge.pmd.lang,
163163
net.sourceforge.pmd.lang.java.rule,
164164
net.sourceforge.pmd.lang.rule,
165+
net.sourceforge.pmd.lang.rule.xpath,
165166
net.sourceforge.pmd.properties,
166167
net.sourceforge.pmd.properties.builders,
167168
net.sourceforge.pmd.renderers,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ private void render(Report report, IFolder folder, String reportName, Renderer r
105105

106106
LOG.debug(" Creating the report file");
107107
IFile reportFile = folder.getFile(reportName);
108-
InputStream contentsStream = new ByteArrayInputStream(reportString.getBytes());
109-
if (reportFile.exists()) {
110-
reportFile.setContents(contentsStream, true, false, getMonitor());
111-
} else {
112-
reportFile.create(contentsStream, true, getMonitor());
108+
try (InputStream contentsStream = new ByteArrayInputStream(reportString.getBytes())) {
109+
if (reportFile.exists()) {
110+
reportFile.setContents(contentsStream, true, false, getMonitor());
111+
} else {
112+
reportFile.create(contentsStream, true, getMonitor());
113+
}
113114
}
114115
reportFile.refreshLocal(IResource.DEPTH_INFINITE, getMonitor());
115-
contentsStream.close();
116116
}
117117

118118
@Override

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/AbstractStructureInspectorPage.java

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
package net.sourceforge.pmd.eclipse.ui.views;
66

7-
import java.io.ByteArrayInputStream;
8-
import java.io.File;
9-
import java.io.InputStream;
107
import java.util.ArrayList;
118
import java.util.Collections;
129
import java.util.List;
@@ -36,22 +33,14 @@
3633
import org.eclipse.ui.themes.ITheme;
3734
import org.eclipse.ui.themes.IThemeManager;
3835

39-
import net.sourceforge.pmd.PMDConfiguration;
40-
import net.sourceforge.pmd.PMDException;
41-
import net.sourceforge.pmd.RuleContext;
42-
import net.sourceforge.pmd.RuleSet;
4336
import net.sourceforge.pmd.RuleViolation;
44-
import net.sourceforge.pmd.SourceCodeProcessor;
4537
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
46-
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
47-
import net.sourceforge.pmd.eclipse.ui.actions.internal.InternalRuleSetUtil;
4838
import net.sourceforge.pmd.eclipse.ui.model.FileRecord;
4939
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
5040
import net.sourceforge.pmd.eclipse.ui.views.ast.ASTUtil;
51-
import net.sourceforge.pmd.lang.dfa.DFAGraphMethod;
52-
import net.sourceforge.pmd.lang.dfa.DFAGraphRule;
41+
import net.sourceforge.pmd.eclipse.ui.views.ast.XPathEvaluator;
42+
import net.sourceforge.pmd.lang.ast.Node;
5343
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
54-
import net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule;
5544

5645
/**
5746
*
@@ -62,8 +51,9 @@ public abstract class AbstractStructureInspectorPage extends Page
6251

6352
private Combo methodSelector;
6453
private FileRecord resourceRecord;
54+
protected Node classNode;
6555
private List<ASTMethodDeclaration> pmdMethodList;
66-
private ITextEditor textEditor;
56+
protected ITextEditor textEditor;
6757

6858
protected AbstractStructureInspectorPage(IWorkbenchPart part, FileRecord record) {
6959
super();
@@ -79,6 +69,9 @@ public void refresh(IResource resource) {
7969
if (resource.getType() == IResource.FILE) {
8070
// set a new filerecord
8171
resourceRecord = new FileRecord(resource);
72+
73+
String source = getDocument().get();
74+
classNode = XPathEvaluator.INSTANCE.getCompilationUnit(source);
8275
}
8376
}
8477

@@ -91,6 +84,10 @@ protected void refreshMethodSelector() {
9184
}
9285

9386
protected void highlight(int beginLine, int beginColumn, int endLine, int endColumn) {
87+
if (textEditor == null) {
88+
return;
89+
}
90+
9491
try {
9592
int offset = getDocument().getLineOffset(beginLine) + beginColumn;
9693
int length = getDocument().getLineOffset(endLine) + endColumn - offset;
@@ -105,7 +102,6 @@ protected IResource getResource() {
105102
}
106103

107104
protected void highlight(int offset, int length) {
108-
109105
if (textEditor == null) {
110106
return;
111107
}
@@ -194,6 +190,10 @@ protected RuleViolation selectedViolationFrom(SelectionChangedEvent event) {
194190

195191
@Override
196192
public void selectionChanged(SelectionChangedEvent event) {
193+
if (textEditor == null) {
194+
return;
195+
}
196+
197197
RuleViolation violation = selectedViolationFrom(event);
198198
if (violation == null) {
199199
return;
@@ -311,38 +311,8 @@ public void methodPicked() {
311311
*/
312312
private List<ASTMethodDeclaration> getPMDMethods() {
313313
List<ASTMethodDeclaration> methodList = new ArrayList<>();
314-
315-
// we need PMD to run over the given Resource
316-
// with the DFAGraphRule to get the Methods;
317-
// PMD needs this Resource as a String
318-
try {
319-
DFAGraphRule dfaGraphRule = new JavaDFAGraphRule();
320-
RuleSet rs = RuleSetUtil.newSingle(dfaGraphRule);
321-
322-
RuleContext ctx = new RuleContext();
323-
ctx.setSourceCodeFile(new File("[scratchpad]"));
324-
325-
// StringReader reader = new StringReader(getDocument().get());
326-
// run PMD using the DFAGraphRule and the Text of the Resource
327-
// new PMDEngine().processFile(reader, rs, ctx);
328-
329-
byte[] bytes = getDocument().get().getBytes();
330-
InputStream input = new ByteArrayInputStream(bytes);
331-
332-
new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(input,
333-
InternalRuleSetUtil.toRuleSets(Collections.singletonList(rs)), ctx);
334-
335-
// the Rule then can give us the Methods
336-
for (DFAGraphMethod m : dfaGraphRule.getMethods()) {
337-
if (m instanceof ASTMethodDeclaration) {
338-
methodList.add((ASTMethodDeclaration) m);
339-
}
340-
}
341-
Collections.sort(methodList, ASTUtil.METHOD_COMPARATOR);
342-
} catch (PMDException pmde) {
343-
logError(StringKeys.ERROR_PMD_EXCEPTION + toString(), pmde);
344-
}
345-
314+
methodList.addAll(classNode.findDescendantsOfType(ASTMethodDeclaration.class));
315+
Collections.sort(methodList, ASTUtil.METHOD_COMPARATOR);
346316
return methodList;
347317
}
348318

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/ast/ASTViewPage.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import net.sourceforge.pmd.lang.ast.ParseException;
5050
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
5151
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
52-
import net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery;
52+
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
5353

5454
/**
5555
* A combined abstract syntax tree viewer for a whole class or selected methods
@@ -66,7 +66,6 @@ public class ASTViewPage extends AbstractStructureInspectorPage {
6666
private StyledText xpathField;
6767
private TableViewer resultsViewer;
6868
private Button goButton;
69-
private Node classNode;
7069
private ASTPainterHelper helper;
7170
private ASTContentProvider contentProvider;
7271

@@ -243,14 +242,18 @@ private void validateXPath(String xpathString) {
243242
}
244243

245244
private void evaluateXPath() {
245+
if (textEditor == null) {
246+
return;
247+
}
248+
246249
if (!setupTest()) {
247250
return;
248251
}
249252

250253
List<Node> results = null;
251254
try {
252255
results = XPathEvaluator.INSTANCE.evaluate(getDocument().get(), xpathField.getText(),
253-
XPathRuleQuery.XPATH_1_0 // TODO derive from future combo widget
256+
XPathVersion.XPATH_2_0.getXmlName() // TODO derive from future combo widget
254257
);
255258
} catch (ParseException pe) {
256259
// TODO showError(pe.fillInStackTrace().getMessage());
@@ -332,11 +335,6 @@ public Control getControl() {
332335
}
333336

334337
protected void showClass() {
335-
if (classNode == null) {
336-
String source = getDocument().get();
337-
classNode = XPathEvaluator.INSTANCE.getCompilationUnit(source);
338-
}
339-
340338
astViewer.setInput(classNode);
341339
astViewer.expandAll();
342340
}
@@ -402,8 +400,6 @@ protected void showMethod(ASTMethodDeclaration pmdMethod) {
402400
public void refresh(IResource newResource) {
403401
super.refresh(newResource);
404402

405-
classNode = null;
406-
407403
// if (isTableShown) {
408404
// refreshDFATable(newResource);
409405
// } else {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/ast/XPathEvaluator.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
package net.sourceforge.pmd.eclipse.ui.views.ast;
66

7-
import java.io.StringReader;
87
import java.util.ArrayList;
98
import java.util.List;
109

10+
import net.sourceforge.pmd.PMDConfiguration;
11+
import net.sourceforge.pmd.PmdAnalysis;
1112
import net.sourceforge.pmd.RuleContext;
1213
import net.sourceforge.pmd.RuleSet;
1314
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
1415
import net.sourceforge.pmd.lang.LanguageRegistry;
1516
import net.sourceforge.pmd.lang.LanguageVersion;
16-
import net.sourceforge.pmd.lang.LanguageVersionHandler;
17-
import net.sourceforge.pmd.lang.Parser;
1817
import net.sourceforge.pmd.lang.ast.Node;
1918
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
20-
import net.sourceforge.pmd.lang.java.ast.ParseException;
19+
import net.sourceforge.pmd.lang.rule.AbstractRule;
2120
import net.sourceforge.pmd.lang.rule.XPathRule;
21+
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
2222

2323
/**
2424
*
@@ -33,17 +33,30 @@ private XPathEvaluator() {
3333

3434
public Node getCompilationUnit(String source) {
3535

36-
LanguageVersionHandler languageVersionHandler = getLanguageVersionHandler();
37-
Parser parser = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions());
38-
Node node = parser.parse(null, new StringReader(source));
39-
languageVersionHandler.getSymbolFacade().start(node);
40-
languageVersionHandler.getTypeResolutionFacade(null).start(node);
41-
return node;
42-
}
36+
final List<Node> result = new ArrayList<>();
37+
38+
PMDConfiguration configuration = new PMDConfiguration();
39+
configuration.setIgnoreIncrementalAnalysis(true);
40+
configuration.setForceLanguageVersion(getLanguageVersion());
4341

44-
private LanguageVersionHandler getLanguageVersionHandler() {
45-
LanguageVersion languageVersion = getLanguageVersion();
46-
return languageVersion.getLanguageVersionHandler();
42+
AbstractRule rule = new XPathRule(XPathVersion.XPATH_2_0, "") {
43+
@Override
44+
public void apply(List<? extends Node> nodes, RuleContext ctx) {
45+
result.addAll(nodes);
46+
}
47+
};
48+
RuleSet ruleset = RuleSetUtil.newSingle(rule);
49+
50+
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
51+
pmd.addRuleSet(ruleset);
52+
pmd.files().addSourceFile(source, "[snippet]");
53+
pmd.performAnalysis();
54+
}
55+
56+
if (!result.isEmpty()) {
57+
return result.get(0);
58+
}
59+
return null;
4760
}
4861

4962
private LanguageVersion getLanguageVersion() {
@@ -59,14 +72,12 @@ private LanguageVersion getLanguageVersion() {
5972
* @param xpathQuery
6073
* @param xpathVersion
6174
* @return
62-
* @throws ParseException
6375
*/
64-
public List<Node> evaluate(String source, String xpathQuery, String xpathVersion) throws ParseException {
65-
Node c = getCompilationUnit(source);
76+
public List<Node> evaluate(String source, String xpathQuery, String xpathVersion) {
6677

6778
final List<Node> results = new ArrayList<>();
6879

69-
XPathRule xpathRule = new XPathRule() {
80+
XPathRule xpathRule = new XPathRule(XPathVersion.ofId(xpathVersion), xpathQuery) {
7081
@Override
7182
public void addViolation(Object data, Node node, String arg) {
7283
results.add(node);
@@ -75,18 +86,17 @@ public void addViolation(Object data, Node node, String arg) {
7586

7687
xpathRule.setMessage("");
7788
xpathRule.setLanguage(getLanguageVersion().getLanguage());
78-
xpathRule.setProperty(XPathRule.XPATH_DESCRIPTOR, xpathQuery);
79-
xpathRule.setProperty(XPathRule.VERSION_DESCRIPTOR, xpathVersion);
8089

8190
RuleSet ruleSet = RuleSetUtil.newSingle(xpathRule);
8291

83-
RuleContext ruleContext = new RuleContext();
84-
ruleContext.setLanguageVersion(getLanguageVersion());
85-
86-
List<Node> nodes = new ArrayList<>(1);
87-
nodes.add(c);
88-
89-
ruleSet.apply(nodes, ruleContext);
92+
PMDConfiguration configuration = new PMDConfiguration();
93+
configuration.setIgnoreIncrementalAnalysis(true);
94+
configuration.setForceLanguageVersion(getLanguageVersion());
95+
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
96+
pmd.addRuleSet(ruleSet);
97+
pmd.files().addSourceFile(source, "[snippet]");
98+
pmd.performAnalysis();
99+
}
90100

91101
return results;
92102
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/dataflow/DataflowViewPage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ public Control getControl() {
151151
*/
152152
@Override
153153
protected void showMethod(final ASTMethodDeclaration pmdMethod) {
154+
if (textEditor == null) {
155+
return;
156+
}
157+
154158
if (pmdMethod != null) {
155159

156160
String resourceString = getDocument().get();
@@ -202,6 +206,10 @@ private void showTableArea(boolean isShown) {
202206

203207
@Override
204208
public void selectionChanged(SelectionChangedEvent event) {
209+
if (textEditor == null) {
210+
return;
211+
}
212+
205213
RuleViolation violation = selectedViolationFrom(event);
206214
if (violation == null) {
207215
return;

0 commit comments

Comments
 (0)