You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by mb...@apache.org on 2022/10/11 10:33:04 UTC
[netbeans] branch master updated: Fixing error recovery when j.l.MatchException is missing, and patterns are present.
This is an automated email from the ASF dual-hosted git repository.
mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 7654eb9b3f Fixing error recovery when j.l.MatchException is missing, and patterns are present.
new b545758804 Merge pull request #4756 from jlahoda/fix-patterns-without-match-exception
7654eb9b3f is described below
commit 7654eb9b3f8882d5743c7280f3c00721d591209b
Author: Jan Lahoda <jl...@netbeans.org>
AuthorDate: Sat Oct 8 08:13:07 2022 +0200
Fixing error recovery when j.l.MatchException is missing, and patterns are present.
---
.../java/source/indexing/VanillaCompileWorker.java | 18 ++++
.../source/indexing/VanillaCompileWorkerTest.java | 102 +++++++++++++++++++++
2 files changed, 120 insertions(+)
diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java b/java/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java
index bf74c02e8f..3dac12f243 100644
--- a/java/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java
+++ b/java/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java
@@ -19,9 +19,11 @@
package org.netbeans.modules.java.source.indexing;
+import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.DeconstructionPatternTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
@@ -59,6 +61,7 @@ import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.comp.Modules;
import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
@@ -103,6 +106,7 @@ import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
@@ -531,6 +535,8 @@ final class VanillaCompileWorker extends CompileWorker {
Trees trees = Trees.instance(BasicJavacTask.instance(ctx));
Types types = Types.instance(ctx);
TreeMaker make = TreeMaker.instance(ctx);
+ Elements el = JavacElements.instance(ctx);
+ boolean hasMatchException = el.getTypeElement("java.lang.MatchException") != null;
//TODO: should preserve error types!!!
new TreePathScanner<Void, Void>() {
private Set<JCNewClass> anonymousClasses = Collections.newSetFromMap(new LinkedHashMap<>());
@@ -884,6 +890,18 @@ final class VanillaCompileWorker extends CompileWorker {
return super.visitMethodInvocation(node, p);
}
+ @Override
+ public Void visitBindingPattern(BindingPatternTree node, Void p) {
+ errorFound |= !hasMatchException;
+ return super.visitBindingPattern(node, p);
+ }
+
+ @Override
+ public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void p) {
+ errorFound |= !hasMatchException;
+ return super.visitDeconstructionPattern(node, p);
+ }
+
@Override
public Void scan(Tree tree, Void p) {
if (tree != null && ExpressionTree.class.isAssignableFrom(tree.getClass())) {
diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorkerTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorkerTest.java
index 31de6b839a..90a41117ff 100644
--- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorkerTest.java
+++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorkerTest.java
@@ -1954,6 +1954,108 @@ public class VanillaCompileWorkerTest extends CompileWorkerTestBase {
"cache/s1/java/15/classes/test/Test.sig")), createdFiles);
}
+ public void testEnhancedSwitch1() throws Exception {
+ setSourceLevel(SourceVersion.latest().name().substring("RELEASE_".length()));
+ setCompilerOptions(Arrays.asList("--enable-preview"));
+ ParsingOutput result = runIndexing(Arrays.asList(compileTuple("test/Test.java", "package test;\n"
+ + "record Rect(ColoredPoint upperLeft) {}\n"
+ + "record ColoredPoint(Point p) {}\n"
+ + "record Point(int x){}\n"
+ + "public class Test {\n"
+ + " private void test(Object o) {\n"
+ + " switch (o) {\n"
+ + " case Rect r: \n"
+ + " System.out.println(\"Hello\");\n"
+ + " break;\n"
+ + " default:\n"
+ + " break;\n"
+ + " }\n"
+ + " }\n"
+ + "}\n")), Arrays.asList());
+
+ assertFalse(result.lowMemory);
+ assertTrue(result.success);
+
+ Set<String> createdFiles = new HashSet<String>();
+
+ for (File created : result.createdFiles) {
+ createdFiles.add(getWorkDir().toURI().relativize(created.toURI()).getPath());
+ }
+
+ assertEquals(new HashSet<String>(Arrays.asList("cache/s1/java/15/classes/test/Rect.sig",
+ "cache/s1/java/15/classes/test/ColoredPoint.sig",
+ "cache/s1/java/15/classes/test/Point.sig",
+ "cache/s1/java/15/classes/test/Test.sig")), createdFiles);
+ }
+
+ public void testEnhancedSwitch2() throws Exception {
+ setSourceLevel(SourceVersion.latest().name().substring("RELEASE_".length()));
+ setCompilerOptions(Arrays.asList("--enable-preview"));
+ ParsingOutput result = runIndexing(Arrays.asList(compileTuple("test/Test.java", "package test;\n"
+ + "record Rect(ColoredPoint upperLeft) {}\n"
+ + "record ColoredPoint(Point p) {}\n"
+ + "record Point(int x){}\n"
+ + "public class Test {\n"
+ + " private void test(Object o) {\n"
+ + " switch (o) {\n"
+ + " case null: \n"
+ + " System.out.println(\"Hello\");\n"
+ + " break;\n"
+ + " default:\n"
+ + " break;\n"
+ + " }\n"
+ + " }\n"
+ + "}\n")), Arrays.asList());
+
+ assertFalse(result.lowMemory);
+ assertTrue(result.success);
+
+ Set<String> createdFiles = new HashSet<String>();
+
+ for (File created : result.createdFiles) {
+ createdFiles.add(getWorkDir().toURI().relativize(created.toURI()).getPath());
+ }
+
+ assertEquals(new HashSet<String>(Arrays.asList("cache/s1/java/15/classes/test/Rect.sig",
+ "cache/s1/java/15/classes/test/ColoredPoint.sig",
+ "cache/s1/java/15/classes/test/Point.sig",
+ "cache/s1/java/15/classes/test/Test.sig")), createdFiles);
+ }
+
+ public void testEnhancedSwitch3() throws Exception {
+ setSourceLevel(SourceVersion.latest().name().substring("RELEASE_".length()));
+ setCompilerOptions(Arrays.asList("--enable-preview"));
+ ParsingOutput result = runIndexing(Arrays.asList(compileTuple("test/Test.java", "package test;\n"
+ + "record Rect(ColoredPoint upperLeft) {}\n"
+ + "record ColoredPoint(Point p) {}\n"
+ + "record Point(int x){}\n"
+ + "public class Test {\n"
+ + " private void test(Object o) {\n"
+ + " switch (o) {\n"
+ + " case (String s): \n"
+ + " System.out.println(\"Hello\");\n"
+ + " break;\n"
+ + " default:\n"
+ + " break;\n"
+ + " }\n"
+ + " }\n"
+ + "}\n")), Arrays.asList());
+
+ assertFalse(result.lowMemory);
+ assertTrue(result.success);
+
+ Set<String> createdFiles = new HashSet<String>();
+
+ for (File created : result.createdFiles) {
+ createdFiles.add(getWorkDir().toURI().relativize(created.toURI()).getPath());
+ }
+
+ assertEquals(new HashSet<String>(Arrays.asList("cache/s1/java/15/classes/test/Rect.sig",
+ "cache/s1/java/15/classes/test/ColoredPoint.sig",
+ "cache/s1/java/15/classes/test/Point.sig",
+ "cache/s1/java/15/classes/test/Test.sig")), createdFiles);
+ }
+
public static void noop() {}
@Override
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists