Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ subprojects {

javaVersions {
libraryTarget = 17
runtime = 21
runtime = 23
}

jdks {
Expand Down
2 changes: 1 addition & 1 deletion palantir-java-format-spi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ tasks.withType(JavaCompile).named('compileJava') {
}

javaVersion {
target = 21
target = 23
}
12 changes: 11 additions & 1 deletion palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ moduleJvmArgs {
'jdk.compiler/com.sun.tools.javac.api')
}

applicationDefaultJvmArgs = [
'--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED'
]

tasks.withType(JavaCompile).named('compileJava') {
// By setting the source and target compatibility to 11, while still using a higher level
// JDK for compilation, we can compile against the AST classes we need to support >11 source
Expand All @@ -62,5 +72,5 @@ tasks.named("test") {
}

javaVersion {
target = 21
target = 23
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.sun.tools.javac.parser.Tokens.TokenKind;
import com.sun.tools.javac.parser.UnicodeReader;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import java.util.Set;
import javax.annotation.Nullable;

/** A wrapper around javac's lexer. */
class JavacTokens {
Expand Down Expand Up @@ -178,6 +180,12 @@ public int getSourcePos(int index) {
return pos + index;
}

@Override
@Nullable
public DiagnosticPosition getPos() {
return null;
}

@Override
public CommentStyle getStyle() {
return style;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ private static RangeMap<Integer, String> buildReplacements(
Set<String> usedNames,
Multimap<String, Range<Integer>> usedInJavadoc) {
RangeMap<Integer, String> replacements = TreeRangeMap.create();
for (JCImport importTree : unit.getImports()) {
for (Object o : unit.getImports()) {
if (!(o instanceof JCImport)) {
// TODO: In Java >=23, getImports() returns JCImportBase
continue;
}
JCImport importTree = (JCImport) o;
String simpleName = getSimpleName(importTree);
if (!isUnused(unit, usedNames, usedInJavadoc, importTree, simpleName)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.palantir.javaformat.OpsBuilder;
import com.palantir.javaformat.java.java14.Java14InputAstVisitor;
import com.sun.source.tree.AnyPatternTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ConstantCaseLabelTree;
import com.sun.source.tree.DeconstructionPatternTree;
Expand Down Expand Up @@ -88,4 +89,10 @@ protected void variableName(Name name) {
visit(name);
}
}

@Override
public Void visitAnyPattern(AnyPatternTree node, Void unused) {
token("_");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public final class FileBasedTests {
"SwitchUnderscore",
"I880",
"I1309",
"Unnamed")
"Unnamed",
"UnnamedPattern")
.build();

private final Class<?> testClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class UnnamedPattern {
public static void main(String[] args) {
var cp = new ColoredPoint(new Point(1, 2), Color.RED);

// The colour doesn't matter, so use an unnamed pattern
// (as opposed to the unnamed type pattern `var _`).
if (cp instanceof ColoredPoint(Point(int x, int y), _)) {
System.out.println(x + "," + y);
}

if (cp instanceof ColoredPoint(_, var c)) {
System.out.println(c);
}
}
}

record Point(int x, int y) { }
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class UnnamedPattern {
public static void main(String[] args) {
var cp = new ColoredPoint(new Point(1, 2), Color.RED);

// The colour doesn't matter, so use an unnamed pattern
// (as opposed to the unnamed type pattern `var _`).
if (cp instanceof ColoredPoint(Point(int x, int y), _)) {
System.out.println(x + "," + y);
}

if (cp instanceof ColoredPoint(_, var c)) {
System.out.println(c);
}
}
}

record Point(int x, int y) {}

enum Color {
RED,
GREEN,
BLUE
}

record ColoredPoint(Point p, Color c) {}