1414
1515package com .google .googlejavaformat .java ;
1616
17- import static com .google .common .base .StandardSystemProperty .JAVA_CLASS_VERSION ;
18- import static com .google .common .base .StandardSystemProperty .JAVA_SPECIFICATION_VERSION ;
1917import static java .nio .charset .StandardCharsets .UTF_8 ;
2018
2119import com .google .common .collect .ImmutableList ;
4240import com .sun .tools .javac .util .Options ;
4341import java .io .IOError ;
4442import java .io .IOException ;
45- import java .lang .reflect .Method ;
4643import java .net .URI ;
4744import java .util .ArrayList ;
4845import java .util .Collection ;
@@ -139,9 +136,9 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
139136 JavacParser parser =
140137 parserFactory .newParser (
141138 javaInput .getText (),
142- /*keepDocComments=*/ true ,
143- /*keepEndPos=*/ true ,
144- /*keepLineMap=*/ true );
139+ /* keepDocComments= */ true ,
140+ /* keepEndPos= */ true ,
141+ /* keepLineMap= */ true );
145142 unit = parser .parseCompilationUnit ();
146143 unit .sourcefile = source ;
147144
@@ -154,10 +151,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
154151 OpsBuilder builder = new OpsBuilder (javaInput , javaOutput );
155152 // Output the compilation unit.
156153 JavaInputAstVisitor visitor ;
157- if (getMajor () >= 14 ) {
154+ if (Runtime . version (). feature () >= 17 ) {
158155 try {
159156 visitor =
160- Class .forName ("com.google.googlejavaformat.java.java14.Java14InputAstVisitor " )
157+ Class .forName ("com.google.googlejavaformat.java.java17.Java17InputAstVisitor " )
161158 .asSubclass (JavaInputAstVisitor .class )
162159 .getConstructor (OpsBuilder .class , JavaFormatterOptions .class )
163160 .newInstance (builder , options );
@@ -177,23 +174,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
177174 javaOutput .flush ();
178175 }
179176
180- // Runtime.Version was added in JDK 9, so use reflection to access it to preserve source
181- // compatibility with Java 8.
182- private static int getMajor () {
183- try {
184- Method versionMethod = Runtime .class .getMethod ("version" );
185- Object version = versionMethod .invoke (null );
186- return (int ) version .getClass ().getMethod ("major" ).invoke (version );
187- } catch (Exception e ) {
188- // continue below
189- }
190- int version = (int ) Double .parseDouble (JAVA_CLASS_VERSION .value ());
191- if (49 <= version && version <= 52 ) {
192- return version - (49 - 5 );
193- }
194- throw new IllegalStateException ("Unknown Java version: " + JAVA_SPECIFICATION_VERSION .value ());
195- }
196-
197177 static boolean errorDiagnostic (Diagnostic <?> input ) {
198178 if (input .getKind () != Diagnostic .Kind .ERROR ) {
199179 return false ;
0 commit comments