diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java index 6b6e5cbe9..ffda10cfc 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java @@ -34,6 +34,7 @@ import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.InstanceOfTree; import com.sun.source.tree.LambdaExpressionTree; +import com.sun.source.tree.ModifiersTree; import com.sun.source.tree.ModuleTree; import com.sun.source.tree.SwitchExpressionTree; import com.sun.source.tree.Tree; @@ -80,12 +81,12 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { try { VariableTree variableTree = (VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node); - visitBindingPattern(variableTree.getType(), variableTree.getName()); + visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName()); } catch (ReflectiveOperationException e1) { try { Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node); Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node); - visitBindingPattern(type, name); + visitBindingPattern(/* modifiers= */ null, type, name); } catch (ReflectiveOperationException e2) { e2.addSuppressed(e1); throw new LinkageError(e2.getMessage(), e2); @@ -94,7 +95,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { return null; } - private void visitBindingPattern(Tree type, Name name) { + private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) { + if (modifiers != null) { + builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty())); + } scan(type, null); builder.breakOp(" "); visit(name); diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java index 180cd9167..4711ce22a 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java @@ -41,9 +41,10 @@ @Execution(ExecutionMode.CONCURRENT) public final class FileBasedTests { - // Tests files that are only used when run with java 14 or higher + // Test files that are only used when run with a minimum Java version private static final ImmutableSet JAVA_14_TESTS = ImmutableSet.of("ExpressionSwitch", "RSL", "Records", "Var"); + private static final ImmutableSet JAVA_16_TESTS = ImmutableSet.of("I588"); private final Class testClass; /** The path prefix for all tests if loaded as resources. */ @@ -62,9 +63,11 @@ public FileBasedTests(Class testClass, String testDirName) { this.fullTestPath = Paths.get("src/test/resources").resolve(resourcePrefix); } - public static void assumeJava14ForJava14Tests(String testName) { + public static void assumeJavaVersionForTest(String testName) { if (JAVA_14_TESTS.contains(testName)) { Assumptions.assumeTrue(Formatter.getRuntimeVersion() >= 14, "Not running on jdk 14 or later"); + } else if (JAVA_16_TESTS.contains(testName)) { + Assumptions.assumeTrue(Formatter.getRuntimeVersion() >= 16, "Not running on jdk 16 or later"); } } diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java index ce7ecd3a2..ae7a4710a 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java @@ -14,7 +14,7 @@ package com.palantir.javaformat.java; -import static com.palantir.javaformat.java.FileBasedTests.assumeJava14ForJava14Tests; +import static com.palantir.javaformat.java.FileBasedTests.assumeJavaVersionForTest; import static com.palantir.javaformat.java.FileBasedTests.isRecreate; import static org.assertj.core.api.Assertions.assertThat; @@ -69,7 +69,7 @@ private static boolean isDebugMode() { @TestTemplate public void format() { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); try { String output = createFormatter().formatSource(input); if (isRecreate()) { @@ -93,7 +93,7 @@ private static Formatter createFormatter() { @TestTemplate public void idempotentLF() { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); try { String mangled = expected.replace(separator, "\n"); @@ -106,7 +106,7 @@ public void idempotentLF() { @TestTemplate public void idempotentCR() { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); try { String mangled = expected.replace(separator, "\r"); @@ -119,7 +119,7 @@ public void idempotentCR() { @TestTemplate public void idempotentCRLF() { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); try { String mangled = expected.replace(separator, "\r\n"); diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperIntegrationTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperIntegrationTest.java index e8e2cda51..f5fa126b0 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperIntegrationTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperIntegrationTest.java @@ -15,7 +15,7 @@ package com.palantir.javaformat.java; import static com.google.common.truth.Truth.assertThat; -import static com.palantir.javaformat.java.FileBasedTests.assumeJava14ForJava14Tests; +import static com.palantir.javaformat.java.FileBasedTests.assumeJavaVersionForTest; import static com.palantir.javaformat.java.FileBasedTests.isRecreate; import com.palantir.javaformat.jupiter.ParameterizedClass; @@ -52,7 +52,7 @@ public StringWrapperIntegrationTest(String name, String input, String output) { @TestTemplate public void test() throws Exception { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); String actualOutput = StringWrapper.wrap(40, formatter.formatSource(input), formatter); if (isRecreate()) { tests.writeFormatterOutput(name, actualOutput); @@ -63,7 +63,7 @@ public void test() throws Exception { @TestTemplate public void testCR() throws Exception { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); assertThat(StringWrapper.wrap(40, formatter.formatSource(input.replace("\n", "\r")), formatter)) .isEqualTo(output.replace("\n", "\r")); @@ -71,7 +71,7 @@ public void testCR() throws Exception { @TestTemplate public void testCRLF() throws Exception { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); assertThat(StringWrapper.wrap(40, formatter.formatSource(input.replace("\n", "\r\n")), formatter)) .isEqualTo(output.replace("\n", "\r\n")); @@ -79,7 +79,7 @@ public void testCRLF() throws Exception { @TestTemplate public void idempotent() throws Exception { - assumeJava14ForJava14Tests(name); + assumeJavaVersionForTest(name); Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs"); String wrap = StringWrapper.wrap(40, formatter.formatSource(input), formatter); assertThat(formatter.formatSource(wrap)).isEqualTo(wrap); diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.input b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.input new file mode 100644 index 000000000..9c8f992ea --- /dev/null +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.input @@ -0,0 +1,8 @@ +class T { + int f(Object x) { + if (x instanceof final Integer i) { + return i; + } + return -1; + } +} \ No newline at end of file diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.output b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.output new file mode 100644 index 000000000..37ff2f50d --- /dev/null +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I588.output @@ -0,0 +1,8 @@ +class T { + int f(Object x) { + if (x instanceof final Integer i) { + return i; + } + return -1; + } +}