Skip to content

Commit

Permalink
Java 21: Pattern Matching + StringTemplate JEP-430 + Unnamed JEP-445
Browse files Browse the repository at this point in the history
  • Loading branch information
npeters committed Oct 3, 2023
1 parent 8627fd2 commit c8e90f3
Show file tree
Hide file tree
Showing 54 changed files with 2,698 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ generated/
# Blueprint theme
__init__.pyc

node_modules/
node_modules/
target
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
buildscript {
repositories {
mavenLocal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
}

dependencies {
classpath 'com.palantir.jakartapackagealignment:jakarta-package-alignment:0.5.0'
classpath 'com.gradle.publish:plugin-publish-plugin:1.2.1'
classpath 'com.palantir.baseline:gradle-baseline-java:5.20.0'
classpath 'com.palantir.baseline:gradle-baseline-java:5.21.0'
classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.15.0'
classpath 'com.palantir.gradle.externalpublish:gradle-external-publish-plugin:1.12.0'
classpath 'com.palantir.gradle.gitversion:gradle-git-version:3.0.0'
Expand Down Expand Up @@ -37,6 +38,7 @@ allprojects {
version = rootProject.version

repositories {
mavenLocal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
}
}
Expand All @@ -45,8 +47,7 @@ subprojects {
apply plugin: 'java-library'
apply plugin: 'org.inferred.processors'

sourceCompatibility = 11
targetCompatibility = 11


tasks.withType(Checkstyle) {
enabled = false
Expand Down
4 changes: 4 additions & 0 deletions gradle-palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ configurations {
implicitDependencies
pluginClasspath
}
java{
sourceCompatibility = 11
targetCompatibility = 11
}

dependencies {
implementation gradleApi()
Expand Down
5 changes: 5 additions & 0 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ configurations {
}
}

java{
sourceCompatibility = 11
targetCompatibility = 11
}

dependencies {
implementation project(':palantir-java-format-jdk-bootstrap')
implementation 'com.github.ben-manes.caffeine:caffeine'
Expand Down
5 changes: 5 additions & 0 deletions palantir-java-format-jdk-bootstrap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ apply plugin: 'java-library'
apply plugin: 'com.palantir.external-publish-jar'
apply plugin: 'com.palantir.revapi'

java{
sourceCompatibility = 11
targetCompatibility = 11
}

dependencies {
annotationProcessor "org.immutables:value"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,17 @@ final class Builder extends ImmutableFormatterCliArgs.Builder {
Builder withJvmArgsForVersion(Integer majorJvmVersion) {
if (majorJvmVersion >= 16) {
addJvmArgs(
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=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");
"--enable-preview",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.file=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");
}
return this;
}
Expand Down
5 changes: 5 additions & 0 deletions palantir-java-format-spi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ apply plugin: 'java-library'
apply plugin: 'com.palantir.external-publish-jar'
apply plugin: 'com.palantir.revapi'

java{
sourceCompatibility = 11
targetCompatibility = 11
}

dependencies {
api 'com.google.guava:guava'
api 'com.fasterxml.jackson.core:jackson-annotations'
Expand Down
20 changes: 18 additions & 2 deletions palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def exports = [
'jdk.compiler/com.sun.tools.javac.api'
]

java{
sourceCompatibility = 11
targetCompatibility = 11
}

def jvmArgList = exports.stream().map(new Function<String, String>() {
@Override
String apply(String value) {
Expand All @@ -57,12 +62,14 @@ tasks.withType(JavaCompile).configureEach {
options.compilerArgs += jvmArgList

if (JavaVersion.current() < JavaVersion.VERSION_14) {
excludes = ['**/Java14InputAstVisitor.java']
exclude '**/Java14InputAstVisitor.java'
}

}

tasks.withType(Test).configureEach {
jvmArgs = jvmArgList
maxParallelForks = 1
}

tasks.withType(Javadoc).configureEach {
Expand All @@ -72,6 +79,7 @@ tasks.withType(Javadoc).configureEach {
if (JavaVersion.current() < JavaVersion.VERSION_14) {
exclude '**/Java14InputAstVisitor.java'
}

}

// false positives due to org.junit.runners.* in the test cases
Expand All @@ -85,7 +93,7 @@ tasks.test {

// necessary to compile Java14InputAstVisitor
idea {
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(14)
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(17)
}

// This block may be replaced by BaselineExportsExtension exports
Expand All @@ -95,3 +103,11 @@ jar {
attributes('Add-Exports': exports.stream().collect(Collectors.joining(' ')))
}
}

tasks.named('jar', Jar){
// hack to get java21 Class ...
// the build script should be completely review to handle java 21
from ('build/classes/java/main', '../palantir-java-format21/build/classes/java/main')

include('**/*')
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ static JavaOutput format(
OpsBuilder opsBuilder = new OpsBuilder(javaInput);

JavaInputAstVisitor visitor;
if (getRuntimeVersion() >= 14) {

if (getRuntimeVersion() >= 21) {
try {
visitor = Class.forName("com.palantir.javaformat.java.java21.Java21InputAstVisitor")
.asSubclass(JavaInputAstVisitor.class)
.getConstructor(OpsBuilder.class, int.class)
.newInstance(opsBuilder, options.indentationMultiplier());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e.getMessage(), e);
}
} else if (getRuntimeVersion() >= 14) {
try {
visitor = Class.forName("com.palantir.javaformat.java.java14.Java14InputAstVisitor")
.asSubclass(JavaInputAstVisitor.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,60 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
List<Tok> toks = new ArrayList<>();
int charI = 0;
int columnI = 0;
int stringFragmentEndPos = -1;

for (RawTok t : rawToks) {
if (stopTokens.contains(t.kind())) {
break;
}
int charI0 = t.pos();

/**
* String Templates : https://openjdk.org/jeps/430
* "String Template" tokenize like that : {STRINGFRAGMENT}{literal}{STRINGFRAGMENT} and after list of parameters
* for exemple:
* `String s = STR."\{fruit[0]} is nice";`
* will give this token (STRINGFRAGMENT)""" , (literal)fruit[0] ,(STRINGFRAGMENT)" is nice" and after "fruit[0]" again with a potion of fruit[0] on file
*
* for this reason we comme back after the position of the last STRINGFRAGMENT
*/
if (t.pos() < stringFragmentEndPos && stringFragmentEndPos < t.endPos()) {
// case System.out.println(STR." \{o} ---" );
// on this case tokTex == " ---\" " but we already add the token " ---\""
// it is why we recreate a new token with only " " (after \" )

charI0 = stringFragmentEndPos;
t = new RawTok("", t.kind(), stringFragmentEndPos, t.endPos());
}

if (t.pos() == stringFragmentEndPos) {
// reset when it come back exactly to the same position: System.out.println(STR." \{o}");
stringFragmentEndPos = -1;
}

// drop token when after STRINGFRAGMENT when it comme back
if (t.pos() < stringFragmentEndPos) {
continue;
}
boolean stringFragmentKind =
(t.kind() != null && "STRINGFRAGMENT".equals(t.kind().name()));

// end String Templates

// Get string, possibly with Unicode escapes.
String originalTokText = text.substring(charI0, t.endPos());
String tokText = t.kind() == TokenKind.STRINGLITERAL

String tokText = (t.kind() == TokenKind.STRINGLITERAL)
? t.stringVal() // Unicode escapes removed.
: originalTokText;

char tokText0 = tokText.charAt(0); // The token's first character.
final boolean isToken; // Is this tok a token?
final boolean isNumbered; // Is this tok numbered? (tokens and comments)
String extraNewline = null; // Extra newline at end?
List<String> strings = new ArrayList<>();
if (Character.isWhitespace(tokText0)) {

if (Character.isWhitespace(tokText0) && !stringFragmentKind) {
isToken = false;
isNumbered = false;
Iterator<String> it = Newlines.lineIterator(originalTokText);
Expand All @@ -402,10 +440,14 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
strings.add(line);
}
}
} else if (tokText.startsWith("'") || tokText.startsWith("\"")) {
} else if (tokText.startsWith("'") || tokText.startsWith("\"") || stringFragmentKind) {
isToken = true;
isNumbered = true;
strings.add(originalTokText);
if (stringFragmentKind) {
stringFragmentEndPos = t.endPos();
}

} else if (tokText.startsWith("//") || tokText.startsWith("/*")) {
// For compatibility with an earlier lexer, the newline after a // comment is its own tok.
if (tokText.startsWith("//") && (originalTokText.endsWith("\n") || originalTokText.endsWith("\r"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ protected void handleModule(boolean first, CompilationUnitTree node) {}

/** Skips over extra semi-colons at the top-level, or in a class member declaration lists. */
protected void dropEmptyDeclarations() {

if (builder.peekToken().equals(Optional.of(";"))) {
while (builder.peekToken().equals(Optional.of(";"))) {
builder.forcedBreak();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public Void visitCase(CaseTree node, Void unused) {
builder.space();
token("-");
token(">");

builder.space();
if (node.getBody().getKind() == BLOCK) {
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class FileBasedTests {
.putAll(15, "I603")
.putAll(16, "I588")
.putAll(17, "I683", "I684", "I696")
// .putAll(19, "I959", "I960", "I961")
.putAll(21, "I959", "I960", "I961")
.build();

private final Class<?> testClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import a.A;;
import a.A;
import b.B;

class Test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import a.A;
;
import b.B;

class Test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package foo;;
package foo;

import com.google.second.Foo;
import com.google.first.Bar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package foo;
;

import com.google.second.Foo;
import com.google.first.Bar;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class I960 {

record Result(String a){}

public void thisIsNotFormattedCorrectly(Object something){

if(something instanceof Result(String somethingAsString) ){
return;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class I960 {

record Result(String a) {}

public void thisIsNotFormattedCorrectly(Object something) {

if (something instanceof Result(String somethingAsString)) {
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class I961 {


public void thisIsNotFormattedCorrectly(Object something){



switch (something){
case Integer i when i > 10-> System.out.println(i.toString());
case Integer i -> System.out.println(i.toString());
case String s -> System.out.println(s);
default -> System.out.println(something.toString());
}




}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class I961 {

public void thisIsNotFormattedCorrectly(Object something) {

switch (something) {
case Integer i when i > 10 -> System.out.println(i.toString());
case Integer i -> System.out.println(i.toString());
case String s -> System.out.println(s);
default -> System.out.println(something.toString());
}
}
}
Loading

0 comments on commit c8e90f3

Please sign in to comment.