Skip to content

Commit f463c2c

Browse files
committed
Java 21: Pattern Matching + StringTemplate JEP-430 + Unnamed JEP-445
1 parent fb99a93 commit f463c2c

File tree

53 files changed

+2690
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2690
-20
lines changed

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
buildscript {
22
repositories {
3+
mavenLocal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
34
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
45
gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
56
}
@@ -40,6 +41,7 @@ allprojects {
4041
version = rootProject.version
4142

4243
repositories {
44+
mavenLocal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
4345
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
4446
}
4547
}
@@ -48,8 +50,7 @@ subprojects {
4850
apply plugin: 'java-library'
4951
apply plugin: 'org.inferred.processors'
5052

51-
sourceCompatibility = 11
52-
targetCompatibility = 11
53+
5354

5455
tasks.withType(Checkstyle).configureEach {
5556
enabled = false

gradle-palantir-java-format/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ configurations {
1010
canBeResolved = true
1111
}
1212
}
13+
java{
14+
sourceCompatibility = 11
15+
targetCompatibility = 11
16+
}
1317

1418
dependencies {
1519
compileOnly 'com.diffplug.spotless:spotless-plugin-gradle'

idea-plugin/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ configurations {
6161
}
6262
}
6363

64+
java{
65+
sourceCompatibility = 11
66+
targetCompatibility = 11
67+
}
68+
6469
dependencies {
6570
implementation project(':palantir-java-format-jdk-bootstrap')
6671
implementation 'com.github.ben-manes.caffeine:caffeine'

palantir-java-format-jdk-bootstrap/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ apply plugin: 'java-library'
22
apply plugin: 'com.palantir.external-publish-jar'
33
apply plugin: 'com.palantir.revapi'
44

5+
java{
6+
sourceCompatibility = 11
7+
targetCompatibility = 11
8+
}
9+
510
dependencies {
611
annotationProcessor "org.immutables:value"
712

palantir-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,17 @@ final class Builder extends ImmutableFormatterCliArgs.Builder {
163163
Builder withJvmArgsForVersion(Integer majorJvmVersion) {
164164
if (majorJvmVersion >= 16) {
165165
addJvmArgs(
166-
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
167-
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
168-
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
169-
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
170-
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
166+
"--enable-preview",
167+
"--add-exports",
168+
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
169+
"--add-exports",
170+
"jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
171+
"--add-exports",
172+
"jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
173+
"--add-exports",
174+
"jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
175+
"--add-exports",
176+
"jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
171177
}
172178
return this;
173179
}

palantir-java-format-spi/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ apply plugin: 'java-library'
22
apply plugin: 'com.palantir.external-publish-jar'
33
apply plugin: 'com.palantir.revapi'
44

5+
java{
6+
sourceCompatibility = 11
7+
targetCompatibility = 11
8+
}
9+
510
dependencies {
611
api 'com.google.guava:guava'
712
api 'com.fasterxml.jackson.core:jackson-annotations'

palantir-java-format/build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
apply plugin: 'application'
24
apply plugin: 'com.palantir.external-publish-jar'
35

@@ -79,7 +81,7 @@ tasks.named("test") {
7981

8082
// necessary to compile Java14InputAstVisitor
8183
idea {
82-
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(14)
84+
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(17)
8385
}
8486

8587
// This block may be replaced by BaselineExportsExtension exports
@@ -89,3 +91,11 @@ tasks.named("jar", Jar) {
8991
attributes('Add-Exports': exports.join(' '))
9092
}
9193
}
94+
95+
tasks.named('jar', Jar){
96+
// hack to get java21 Class ...
97+
// the build script should be completely review to handle java 21
98+
from ('build/classes/java/main', '../palantir-java-format21/build/classes/java/main')
99+
100+
include('**/*')
101+
}

palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,17 @@ static JavaOutput format(
131131
OpsBuilder opsBuilder = new OpsBuilder(javaInput);
132132

133133
JavaInputAstVisitor visitor;
134-
if (getRuntimeVersion() >= 14) {
134+
135+
if (getRuntimeVersion() >= 21) {
136+
try {
137+
visitor = Class.forName("com.palantir.javaformat.java.java21.Java21InputAstVisitor")
138+
.asSubclass(JavaInputAstVisitor.class)
139+
.getConstructor(OpsBuilder.class, int.class)
140+
.newInstance(opsBuilder, options.indentationMultiplier());
141+
} catch (ReflectiveOperationException e) {
142+
throw new RuntimeException(e.getMessage(), e);
143+
}
144+
} else if (getRuntimeVersion() >= 14) {
135145
try {
136146
visitor = Class.forName("com.palantir.javaformat.java.java14.Java14InputAstVisitor")
137147
.asSubclass(JavaInputAstVisitor.class)

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,60 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
370370
List<Tok> toks = new ArrayList<>();
371371
int charI = 0;
372372
int columnI = 0;
373+
int stringFragmentEndPos = -1;
374+
373375
for (RawTok t : rawToks) {
374376
if (stopTokens.contains(t.kind())) {
375377
break;
376378
}
377379
int charI0 = t.pos();
380+
381+
/**
382+
* String Templates : https://openjdk.org/jeps/430
383+
* "String Template" tokenize like that : {STRINGFRAGMENT}{literal}{STRINGFRAGMENT} and after list of parameters
384+
* for exemple:
385+
* `String s = STR."\{fruit[0]} is nice";`
386+
* will give this token (STRINGFRAGMENT)""" , (literal)fruit[0] ,(STRINGFRAGMENT)" is nice" and after "fruit[0]" again with a potion of fruit[0] on file
387+
*
388+
* for this reason we comme back after the position of the last STRINGFRAGMENT
389+
*/
390+
if (t.pos() < stringFragmentEndPos && stringFragmentEndPos < t.endPos()) {
391+
// case System.out.println(STR." \{o} ---" );
392+
// on this case tokTex == " ---\" " but we already add the token " ---\""
393+
// it is why we recreate a new token with only " " (after \" )
394+
395+
charI0 = stringFragmentEndPos;
396+
t = new RawTok("", t.kind(), stringFragmentEndPos, t.endPos());
397+
}
398+
399+
if (t.pos() == stringFragmentEndPos) {
400+
// reset when it come back exactly to the same position: System.out.println(STR." \{o}");
401+
stringFragmentEndPos = -1;
402+
}
403+
404+
// drop token when after STRINGFRAGMENT when it comme back
405+
if (t.pos() < stringFragmentEndPos) {
406+
continue;
407+
}
408+
boolean stringFragmentKind =
409+
(t.kind() != null && "STRINGFRAGMENT".equals(t.kind().name()));
410+
411+
// end String Templates
412+
378413
// Get string, possibly with Unicode escapes.
379414
String originalTokText = text.substring(charI0, t.endPos());
380-
String tokText = t.kind() == TokenKind.STRINGLITERAL
415+
416+
String tokText = (t.kind() == TokenKind.STRINGLITERAL)
381417
? t.stringVal() // Unicode escapes removed.
382418
: originalTokText;
419+
383420
char tokText0 = tokText.charAt(0); // The token's first character.
384421
final boolean isToken; // Is this tok a token?
385422
final boolean isNumbered; // Is this tok numbered? (tokens and comments)
386423
String extraNewline = null; // Extra newline at end?
387424
List<String> strings = new ArrayList<>();
388-
if (Character.isWhitespace(tokText0)) {
425+
426+
if (Character.isWhitespace(tokText0) && !stringFragmentKind) {
389427
isToken = false;
390428
isNumbered = false;
391429
Iterator<String> it = Newlines.lineIterator(originalTokText);
@@ -402,10 +440,14 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
402440
strings.add(line);
403441
}
404442
}
405-
} else if (tokText.startsWith("'") || tokText.startsWith("\"")) {
443+
} else if (tokText.startsWith("'") || tokText.startsWith("\"") || stringFragmentKind) {
406444
isToken = true;
407445
isNumbered = true;
408446
strings.add(originalTokText);
447+
if (stringFragmentKind) {
448+
stringFragmentEndPos = t.endPos();
449+
}
450+
409451
} else if (tokText.startsWith("//") || tokText.startsWith("/*")) {
410452
// For compatibility with an earlier lexer, the newline after a // comment is its own tok.
411453
if (tokText.startsWith("//") && (originalTokText.endsWith("\n") || originalTokText.endsWith("\r"))) {

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ protected void handleModule(boolean first, CompilationUnitTree node) {}
394394

395395
/** Skips over extra semi-colons at the top-level, or in a class member declaration lists. */
396396
protected void dropEmptyDeclarations() {
397+
397398
if (builder.peekToken().equals(Optional.of(";"))) {
398399
while (builder.peekToken().equals(Optional.of(";"))) {
399400
builder.forcedBreak();

0 commit comments

Comments
 (0)