Skip to content

Commit 272d37f

Browse files
committed
Restore functionality of integrated XPath Designer and Dataflow view
1 parent b720389 commit 272d37f

File tree

4 files changed

+79
-31
lines changed

4 files changed

+79
-31
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected AbstractStructureInspectorPage(IWorkbenchPart part, FileRecord record)
6161
resourceRecord = record;
6262
if (part instanceof ITextEditor) {
6363
textEditor = (ITextEditor) part;
64+
String source = getDocument().get();
65+
classNode = XPathEvaluator.INSTANCE.getCompilationUnit(source);
6466
}
6567
}
6668

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

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

7-
import java.util.ArrayList;
87
import java.util.List;
98

109
import net.sourceforge.pmd.PMDConfiguration;
1110
import net.sourceforge.pmd.PmdAnalysis;
12-
import net.sourceforge.pmd.RuleContext;
1311
import net.sourceforge.pmd.RuleSet;
1412
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
13+
import net.sourceforge.pmd.eclipse.util.internal.SpyingRule;
14+
import net.sourceforge.pmd.eclipse.util.internal.SpyingXPathRule;
1515
import net.sourceforge.pmd.lang.LanguageRegistry;
1616
import net.sourceforge.pmd.lang.LanguageVersion;
1717
import net.sourceforge.pmd.lang.ast.Node;
1818
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
19-
import net.sourceforge.pmd.lang.rule.AbstractRule;
20-
import net.sourceforge.pmd.lang.rule.XPathRule;
2119
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
2220

2321
/**
@@ -32,19 +30,12 @@ private XPathEvaluator() {
3230
}
3331

3432
public Node getCompilationUnit(String source) {
35-
36-
final List<Node> result = new ArrayList<>();
37-
3833
PMDConfiguration configuration = new PMDConfiguration();
3934
configuration.setIgnoreIncrementalAnalysis(true);
4035
configuration.setForceLanguageVersion(getLanguageVersion());
4136

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-
};
37+
SpyingRule rule = new SpyingRule();
38+
rule.setLanguage(getLanguageVersion().getLanguage());
4839
RuleSet ruleset = RuleSetUtil.newSingle(rule);
4940

5041
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
@@ -53,10 +44,7 @@ public void apply(List<? extends Node> nodes, RuleContext ctx) {
5344
pmd.performAnalysis();
5445
}
5546

56-
if (!result.isEmpty()) {
57-
return result.get(0);
58-
}
59-
return null;
47+
return rule.getRootNode();
6048
}
6149

6250
private LanguageVersion getLanguageVersion() {
@@ -74,19 +62,7 @@ private LanguageVersion getLanguageVersion() {
7462
* @return
7563
*/
7664
public List<Node> evaluate(String source, String xpathQuery, String xpathVersion) {
77-
78-
final List<Node> results = new ArrayList<>();
79-
80-
XPathRule xpathRule = new XPathRule(XPathVersion.ofId(xpathVersion), xpathQuery) {
81-
@Override
82-
public void addViolation(Object data, Node node, String arg) {
83-
results.add(node);
84-
}
85-
};
86-
87-
xpathRule.setMessage("");
88-
xpathRule.setLanguage(getLanguageVersion().getLanguage());
89-
65+
SpyingXPathRule xpathRule = new SpyingXPathRule(XPathVersion.ofId(xpathVersion), xpathQuery, getLanguageVersion().getLanguage());
9066
RuleSet ruleSet = RuleSetUtil.newSingle(xpathRule);
9167

9268
PMDConfiguration configuration = new PMDConfiguration();
@@ -98,6 +74,6 @@ public void addViolation(Object data, Node node, String arg) {
9874
pmd.performAnalysis();
9975
}
10076

101-
return results;
77+
return xpathRule.getResult();
10278
}
10379
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
6+
package net.sourceforge.pmd.eclipse.util.internal;
7+
8+
import java.util.List;
9+
10+
import net.sourceforge.pmd.RuleContext;
11+
import net.sourceforge.pmd.lang.ast.Node;
12+
import net.sourceforge.pmd.lang.rule.AbstractRule;
13+
14+
public class SpyingRule extends AbstractRule {
15+
16+
private static Node rootNode;
17+
18+
@SuppressWarnings("deprecation")
19+
public SpyingRule() {
20+
setUsesDFA();
21+
}
22+
23+
@Override
24+
public void apply(List<? extends Node> nodes, RuleContext ctx) {
25+
rootNode = nodes.get(0);
26+
}
27+
28+
public Node getRootNode() {
29+
return rootNode;
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
6+
package net.sourceforge.pmd.eclipse.util.internal;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import net.sourceforge.pmd.lang.Language;
12+
import net.sourceforge.pmd.lang.ast.Node;
13+
import net.sourceforge.pmd.lang.rule.XPathRule;
14+
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
15+
16+
public class SpyingXPathRule extends XPathRule {
17+
18+
private static List<Node> result = new ArrayList<>();
19+
20+
@SuppressWarnings("deprecation")
21+
public SpyingXPathRule() {
22+
// default constructor required for deep copy
23+
}
24+
25+
public SpyingXPathRule(XPathVersion version, String expression, Language language) {
26+
super(version, expression);
27+
setMessage("");
28+
setLanguage(language);
29+
}
30+
31+
@Override
32+
public void addViolation(Object data, Node node, String arg) {
33+
result.add(node);
34+
}
35+
36+
public List<Node> getResult() {
37+
return result;
38+
}
39+
}

0 commit comments

Comments
 (0)