Skip to content

Commit e3c018b

Browse files
authored
Merge pull request #57 from oowekyala/compat-7.0
Update to latest pmd 7 changes
2 parents 19a8ae9 + bce0074 commit e3c018b

File tree

12 files changed

+131
-116
lines changed

12 files changed

+131
-116
lines changed

src/main/java/net/sourceforge/pmd/util/fxdesigner/MetricPaneController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Val<Integer> numAvailableMetrics() {
8484

8585

8686
private ObservableList<MetricResult<?>> evaluateAllMetrics(Node n) {
87-
LanguageMetricsProvider provider = getGlobalLanguageVersion().getLanguageVersionHandler().getLanguageMetricsProvider();
87+
LanguageMetricsProvider provider = n.getAstInfo().getLanguageProcessor().services().getLanguageMetricsProvider();
8888
if (provider == null) {
8989
return FXCollections.emptyObservableList();
9090
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/ApplicationComponent.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.reactfx.value.Val;
1111

1212
import net.sourceforge.pmd.lang.Language;
13-
import net.sourceforge.pmd.lang.LanguageVersion;
14-
import net.sourceforge.pmd.lang.LanguageVersionHandler;
1513
import net.sourceforge.pmd.util.designerbindings.DesignerBindings;
1614
import net.sourceforge.pmd.util.fxdesigner.SourceEditorController;
1715
import net.sourceforge.pmd.util.fxdesigner.app.services.AppServiceDescriptor;
@@ -50,11 +48,6 @@ default <T> T getService(AppServiceDescriptor<T> descriptor) {
5048
}
5149

5250

53-
default LanguageVersion getGlobalLanguageVersion() {
54-
return getService(DesignerRoot.AST_MANAGER).languageVersionProperty().getValue();
55-
}
56-
57-
5851
/**
5952
* The language is now global to the app.
6053
*/
@@ -65,9 +58,8 @@ default LanguageVersion getGlobalLanguageVersion() {
6558

6659
default Val<DesignerBindings> languageBindingsProperty() {
6760
return getService(DesignerRoot.AST_MANAGER)
68-
.languageVersionProperty()
69-
.map(LanguageVersion::getLanguageVersionHandler)
70-
.map(LanguageVersionHandler::getDesignerBindings);
61+
.languageProcessorProperty()
62+
.map(lp -> lp.services().getDesignerBindings());
7163
}
7264

7365

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/XPathUpdateSubscriber.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public Subscription init(ASTManager astManager) {
5757

5858
try {
5959
List<Node> results = XPathEvaluator.evaluateQuery(compil,
60-
astManager.languageVersionProperty().getValue(),
6160
query.getVersion(),
6261
query.getExpression(),
6362
props,

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/services/ASTManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.reactfx.value.Val;
1111
import org.reactfx.value.Var;
1212

13+
import net.sourceforge.pmd.lang.LanguageProcessor;
1314
import net.sourceforge.pmd.lang.LanguageVersion;
1415
import net.sourceforge.pmd.lang.ast.Node;
1516
import net.sourceforge.pmd.lang.document.TextDocument;
@@ -43,6 +44,8 @@ public interface ASTManager extends ApplicationComponent, SettingsOwner {
4344

4445
Val<LanguageVersion> languageVersionProperty();
4546

47+
Val<LanguageProcessor> languageProcessorProperty();
48+
4649

4750
Val<Node> compilationUnitProperty();
4851

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/services/ASTManagerImpl.java

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,26 @@
99

1010
import java.time.Duration;
1111
import java.util.Collections;
12+
import java.util.HashMap;
1213
import java.util.Map;
1314
import java.util.Objects;
1415
import java.util.Optional;
1516

1617
import org.apache.commons.lang3.StringUtils;
1718
import org.checkerframework.checker.nullness.qual.NonNull;
19+
import org.checkerframework.checker.nullness.qual.Nullable;
1820
import org.reactfx.value.SuspendableVar;
1921
import org.reactfx.value.Val;
2022
import org.reactfx.value.Var;
21-
23+
import org.slf4j.event.Level;
24+
25+
import net.sourceforge.pmd.lang.JvmLanguagePropertyBundle;
26+
import net.sourceforge.pmd.lang.Language;
27+
import net.sourceforge.pmd.lang.LanguageProcessor;
28+
import net.sourceforge.pmd.lang.LanguageProcessorRegistry;
29+
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
30+
import net.sourceforge.pmd.lang.LanguageRegistry;
2231
import net.sourceforge.pmd.lang.LanguageVersion;
23-
import net.sourceforge.pmd.lang.LanguageVersionHandler;
2432
import net.sourceforge.pmd.lang.ast.Node;
2533
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
2634
import net.sourceforge.pmd.lang.ast.RootNode;
@@ -33,6 +41,7 @@
3341
import net.sourceforge.pmd.util.fxdesigner.model.ParseAbortedException;
3442
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
3543
import net.sourceforge.pmd.util.fxdesigner.util.Tuple3;
44+
import net.sourceforge.pmd.util.log.MessageReporter;
3645

3746

3847
/**
@@ -43,6 +52,25 @@
4352
*/
4453
public class ASTManagerImpl implements ASTManager {
4554

55+
public static final MessageReporter NOOP_REPORTER = new MessageReporter() { // todo replace with MessageReporter.noop
56+
@Override
57+
public boolean isLoggable(Level level) {
58+
return false;
59+
}
60+
61+
62+
@Override
63+
public void logEx(Level level, @Nullable String s, Object[] objects, @Nullable Throwable throwable) {
64+
// noop
65+
}
66+
67+
68+
@Override
69+
public int numErrors() {
70+
return 0;
71+
}
72+
};
73+
4674
private final DesignerRoot designerRoot;
4775

4876
private final Var<ClassLoader> auxclasspathClassLoader = Var.newSimpleVar(null);
@@ -60,6 +88,7 @@ public class ASTManagerImpl implements ASTManager {
6088
*/
6189
private final SuspendableVar<String> sourceCode = Var.newSimpleVar("").suspendable();
6290
private final SuspendableVar<TextDocument> sourceDocument = Var.newSimpleVar(TextDocument.readOnlyString("", languageVersion.getValue())).suspendable();
91+
private final SuspendableVar<LanguageProcessorRegistry> lpRegistry = Var.<LanguageProcessorRegistry>newSimpleVar(null).suspendable();
6392

6493
private final Var<ParseAbortedException> currentException = Var.newSimpleVar(null);
6594

@@ -93,7 +122,7 @@ public ASTManagerImpl(DesignerRoot owner) {
93122

94123
Node updated;
95124
try {
96-
updated = refreshAST(this, source, version, classLoader).orElse(null);
125+
updated = refreshAST(this, source, version, refreshRegistry(version, classLoader)).orElse(null);
97126
currentException.setValue(null);
98127
} catch (ParseAbortedException e) {
99128
updated = null;
@@ -158,16 +187,27 @@ public Var<Map<String, String>> ruleProperties() {
158187
return ruleProperties;
159188
}
160189

190+
161191
@Override
162192
public Var<LanguageVersion> languageVersionProperty() {
163193
return languageVersion;
164194
}
165195

166196

197+
@Override
198+
public Val<LanguageProcessor> languageProcessorProperty() {
199+
return lpRegistry.mapDynamic(
200+
languageVersionProperty().map(LanguageVersion::getLanguage)
201+
.map(l -> lp -> lp.getProcessor(l))
202+
);
203+
}
204+
205+
167206
public LanguageVersion getLanguageVersion() {
168207
return languageVersion.getValue();
169208
}
170209

210+
171211
public void setLanguageVersion(LanguageVersion version) {
172212
this.languageVersion.setValue(version);
173213
}
@@ -181,11 +221,51 @@ public Val<Node> compilationUnitProperty() {
181221
return nodeVal;
182222
}
183223

224+
184225
@Override
185226
public Var<ParseAbortedException> currentExceptionProperty() {
186227
return currentException;
187228
}
188229

230+
231+
private LanguageProcessorRegistry refreshRegistry(LanguageVersion version, ClassLoader classLoader) {
232+
LanguageProcessorRegistry current = lpRegistry.getValue();
233+
if (current == null) {
234+
Map<Language, LanguagePropertyBundle> langProperties = new HashMap<>();
235+
LanguagePropertyBundle bundle = version.getLanguage().newPropertyBundle();
236+
bundle.setLanguageVersion(version.getVersion());
237+
if (bundle instanceof JvmLanguagePropertyBundle) {
238+
((JvmLanguagePropertyBundle) bundle).setClassLoader(classLoader);
239+
}
240+
241+
langProperties.put(version.getLanguage(), bundle);
242+
243+
LanguageRegistry languages =
244+
AuxLanguageRegistry.supportedLangs()
245+
.getDependenciesOf(version.getLanguage());
246+
247+
LanguageProcessorRegistry newRegistry =
248+
LanguageProcessorRegistry.create(languages,
249+
langProperties,
250+
NOOP_REPORTER);
251+
lpRegistry.setValue(newRegistry);
252+
return newRegistry;
253+
}
254+
255+
// already created, need to check that the version is the same
256+
if (!current.getLanguages().getLanguages().contains(version.getLanguage())
257+
|| !current.getProcessor(version.getLanguage()).getLanguageVersion().equals(version)) {
258+
// current is invalid, recreate it
259+
current.close();
260+
lpRegistry.setValue(null);
261+
return refreshRegistry(version, classLoader);
262+
}
263+
264+
// the current one is fine
265+
return current;
266+
}
267+
268+
189269
/**
190270
* Refreshes the compilation unit given the current state of the model.
191271
*
@@ -194,28 +274,29 @@ public Var<ParseAbortedException> currentExceptionProperty() {
194274
private static Optional<Node> refreshAST(ApplicationComponent component,
195275
String source,
196276
LanguageVersion version,
197-
ClassLoader classLoader) throws ParseAbortedException {
277+
LanguageProcessorRegistry lpRegistry) throws ParseAbortedException {
198278

199279
String dummyFilePath = "dummy." + version.getLanguage().getExtensions().get(0);
200280
TextDocument textDocument = TextDocument.readOnlyString(source, dummyFilePath, version);
201-
LanguageVersionHandler handler = version.getLanguageVersionHandler();
281+
202282
ParserTask task = new ParserTask(
203283
textDocument,
204284
SemanticErrorReporter.noop(),
205-
classLoader
285+
lpRegistry
206286
);
207287

288+
LanguageProcessor processor = lpRegistry.getProcessor(version.getLanguage());
208289
RootNode node;
209290
try {
210-
node = handler.getParser().parse(task);
291+
node = processor.services().getParser().parse(task);
211292
} catch (Exception e) {
212293
component.logUserException(e, Category.PARSE_EXCEPTION);
213294
throw new ParseAbortedException(e);
214295
}
215296

216297
// Notify that the parse went OK so we can avoid logging very recent exceptions
217298

218-
component.raiseParsableSourceFlag(() -> "Param hash: " + Objects.hash(source, version, classLoader));
299+
component.raiseParsableSourceFlag(() -> "Param hash: " + Objects.hash(source, version, lpRegistry));
219300

220301
return Optional.of(node);
221302
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/model/XPathEvaluator.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
import org.apache.commons.lang3.StringUtils;
1616

17-
import net.sourceforge.pmd.lang.LanguageVersion;
1817
import net.sourceforge.pmd.lang.ast.Node;
1918
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
2019
import net.sourceforge.pmd.lang.rule.xpath.internal.DeprecatedAttrLogger; // NOPMD
2120
import net.sourceforge.pmd.lang.rule.xpath.internal.SaxonXPathRuleQuery; // NOPMD
2221
import net.sourceforge.pmd.properties.PropertyDescriptor;
23-
import net.sourceforge.pmd.util.fxdesigner.app.ApplicationComponent;
2422
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
2523

2624

@@ -44,14 +42,12 @@ private XPathEvaluator() {
4442
* @return The results, or an empty list if there was an error
4543
*/
4644
public static List<Node> simpleEvaluate(DesignerRoot root, String query) {
47-
ApplicationComponent component = () -> root;
4845
return root.getService(DesignerRoot.AST_MANAGER)
4946
.compilationUnitProperty()
5047
.getOpt()
5148
.map(n -> {
5249
try {
5350
return evaluateQuery(n,
54-
component.getGlobalLanguageVersion(),
5551
XPathVersion.DEFAULT,
5652
query,
5753
emptyMap(),
@@ -69,15 +65,13 @@ public static List<Node> simpleEvaluate(DesignerRoot root, String query) {
6965
* no side effects.
7066
*
7167
* @param compilationUnit AST root
72-
* @param languageVersion language version
7368
* @param xpathVersion XPath version
7469
* @param xpathQuery XPath query
7570
* @param properties Properties of the rule
7671
*
7772
* @throws XPathEvaluationException if there was an error during the evaluation. The cause is preserved
7873
*/
7974
public static List<Node> evaluateQuery(Node compilationUnit,
80-
LanguageVersion languageVersion,
8175
XPathVersion xpathVersion,
8276
String xpathQuery,
8377
Map<String, String> propertyValues,
@@ -99,7 +93,7 @@ public static List<Node> evaluateQuery(Node compilationUnit,
9993
xpathQuery,
10094
xpathVersion,
10195
allProperties,
102-
languageVersion.getLanguageVersionHandler().getXPathHandler(),
96+
compilationUnit.getAstInfo().getLanguageProcessor().services().getXPathHandler(),
10397
DeprecatedAttrLogger.noop()
10498
);
10599

src/main/java/net/sourceforge/pmd/util/fxdesigner/model/testing/LiveViolationRecord.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class LiveViolationRecord implements SettingsOwner, Comparable<LiveViolat
2222
private int line;
2323

2424

25+
// this ctor is used by the thing that restores application state
26+
@SuppressWarnings("unused")
27+
public LiveViolationRecord() {
28+
this(-1, TextRegion.caretAt(0), null);
29+
}
30+
2531
public LiveViolationRecord(@NonNull TextRegion region, @Nullable String message) {
2632
this(-1, region, message);
2733
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/model/testing/TestXmlDumper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
import net.sourceforge.pmd.lang.document.TextDocument;
3737
import net.sourceforge.pmd.lang.document.TextRegion;
38+
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
3839
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
39-
import net.sourceforge.pmd.util.fxdesigner.util.PlainTextLanguage;
4040

4141
public class TestXmlDumper {
4242

@@ -100,7 +100,7 @@ private void appendSingle(Element testCode, LiveTestCase descriptor, Document do
100100
Element linenos = doc.createElementNS(NS, "expected-linenumbers");
101101

102102
// create a text doc just to ask for line numbers
103-
TextDocument textDocument = TextDocument.readOnlyString(descriptor.getSource(), PlainTextLanguage.INSTANCE.getDefaultVersion());
103+
TextDocument textDocument = TextDocument.readOnlyString(descriptor.getSource(), AuxLanguageRegistry.plainTextLanguage().getDefaultVersion());
104104

105105
String joined = expectedViolations
106106
.stream()

src/main/java/net/sourceforge/pmd/util/fxdesigner/model/testing/TestXmlParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import net.sourceforge.pmd.util.fxdesigner.model.ObservableRuleBuilder;
3939
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
4040
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
41-
import net.sourceforge.pmd.util.fxdesigner.util.PlainTextLanguage;
4241

4342
public class TestXmlParser {
4443

@@ -188,7 +187,7 @@ private static LiveTestCase fromDescriptor(
188187
// create a document just to map source lines to regions
189188
// language is irrelevant so we use plain text
190189
@SuppressWarnings("PMD.CloseResource")
191-
TextDocument doc = TextDocument.readOnlyString(code, PlainTextLanguage.INSTANCE.getDefaultVersion());
190+
TextDocument doc = TextDocument.readOnlyString(code, AuxLanguageRegistry.plainTextLanguage().getDefaultVersion());
192191

193192
for (int i = 0; i < expectedProblems; i++) {
194193
String m = messages.size() > i ? messages.get(i) : null;

0 commit comments

Comments
 (0)