3333import java .util .List ;
3434import java .util .Optional ;
3535import java .util .stream .Collectors ;
36- import org .immutables .value .Value ;
3736
3837public final class BootstrappingFormatterService implements FormatterService {
3938 private static final ObjectMapper MAPPER =
@@ -80,13 +79,12 @@ public String fixImports(String input) throws FormatterException {
8079
8180 private ImmutableList <Replacement > getFormatReplacementsInternal (String input , Collection <Range <Integer >> ranges )
8281 throws IOException {
83- FormatterCliArgs command = FormatterCliArgs .builder ()
84- .jdkPath (jdkPath )
85- .withJvmArgsForVersion (jdkMajorVersion )
86- .implementationClasspath (implementationClassPath )
87- .outputReplacements (true )
88- .characterRanges (ranges .stream ().map (RangeUtils ::toStringRange ).collect (Collectors .toList ()))
89- .build ();
82+ FormatterCliArgs command = new FormatterCliArgs (
83+ jdkPath ,
84+ jvmArgsForVersion (jdkMajorVersion ),
85+ implementationClassPath ,
86+ /* outputReplacements= */ true ,
87+ ranges .stream ().map (RangeUtils ::toStringRange ).collect (Collectors .toList ()));
9088
9189 @ SuppressWarnings ("for-rollout:NullAway" )
9290 Optional <String > output =
@@ -98,43 +96,50 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
9896 }
9997
10098 private String runFormatterCommand (String input ) throws IOException {
101- FormatterCliArgs command = FormatterCliArgs . builder ()
102- . jdkPath ( jdkPath )
103- . withJvmArgsForVersion (jdkMajorVersion )
104- . implementationClasspath ( implementationClassPath )
105- . outputReplacements ( false )
106- . build ( );
99+ FormatterCliArgs command = new FormatterCliArgs (
100+ jdkPath ,
101+ jvmArgsForVersion (jdkMajorVersion ),
102+ implementationClassPath ,
103+ /* outputReplacements= */ false ,
104+ /* characterRanges= */ List . of () );
107105 return FormatterCommandRunner .runWithStdin (command .toArgs (), input , Optional .ofNullable (jdkPath .getParent ()))
108106 .orElse (input );
109107 }
110108
111- @ Value .Immutable
112- interface FormatterCliArgs {
113- List <String > characterRanges ();
114-
115- boolean outputReplacements ();
116-
117- Path jdkPath ();
118-
119- List <Path > implementationClasspath ();
109+ private static List <String > jvmArgsForVersion (int majorJvmVersion ) {
110+ if (majorJvmVersion >= 16 ) {
111+ return List .of (
112+ "--add-exports" , "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" ,
113+ "--add-exports" , "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" ,
114+ "--add-exports" , "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" ,
115+ "--add-exports" , "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" ,
116+ "--add-exports" , "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" );
117+ }
118+ return List .of ();
119+ }
120120
121- List <String > jvmArgs ();
121+ record FormatterCliArgs (
122+ Path jdkPath ,
123+ List <String > jvmArgs ,
124+ List <Path > implementationClasspath ,
125+ boolean outputReplacements ,
126+ List <String > characterRanges ) {
122127
123- default List <String > toArgs () {
128+ List <String > toArgs () {
124129 ImmutableList .Builder <String > args = ImmutableList .<String >builder ()
125- .add (jdkPath () .toAbsolutePath ().toString ())
126- .addAll (jvmArgs () )
130+ .add (jdkPath .toAbsolutePath ().toString ())
131+ .addAll (jvmArgs )
127132 .add (
128133 "-cp" ,
129- implementationClasspath () .stream ()
134+ implementationClasspath .stream ()
130135 .map (path -> path .toAbsolutePath ().toString ())
131136 .collect (Collectors .joining (System .getProperty ("path.separator" ))))
132137 .add (FORMATTER_MAIN_CLASS );
133138
134- if (!characterRanges () .isEmpty ()) {
135- args .add ("--character-ranges" , Joiner .on (',' ).join (characterRanges () ));
139+ if (!characterRanges .isEmpty ()) {
140+ args .add ("--character-ranges" , Joiner .on (',' ).join (characterRanges ));
136141 }
137- if (outputReplacements () ) {
142+ if (outputReplacements ) {
138143 args .add ("--output-replacements" );
139144 }
140145
@@ -143,23 +148,5 @@ default List<String> toArgs() {
143148 .add ("-" )
144149 .build ();
145150 }
146-
147- static Builder builder () {
148- return new Builder ();
149- }
150-
151- final class Builder extends ImmutableFormatterCliArgs .Builder {
152- Builder withJvmArgsForVersion (Integer majorJvmVersion ) {
153- if (majorJvmVersion >= 16 ) {
154- addJvmArgs (
155- "--add-exports" , "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" ,
156- "--add-exports" , "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" ,
157- "--add-exports" , "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" ,
158- "--add-exports" , "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" ,
159- "--add-exports" , "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" );
160- }
161- return this ;
162- }
163- }
164151 }
165152}
0 commit comments