Skip to content

Commit 02a598d

Browse files
authored
Parsers should target the previous LTS to support non-LTS versions (#6055)
* Parsers should target the previous LTS to support non-LTS versions * Document why we're setting lower target compatibility * `Java17ParserVisitor` requires 17 for `ClassTree.getPermitsClause` * Catch `Throwable` in `JavaParser` for `UnsupportedClassVersionError`
1 parent f5b00eb commit 02a598d

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

rewrite-java-17/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ java {
3131
}
3232

3333
tasks.withType<JavaCompile>().configureEach {
34-
// allows --add-exports to in spite of the JDK's restrictions on this
3534
sourceCompatibility = JavaVersion.VERSION_17.toString()
3635
targetCompatibility = JavaVersion.VERSION_17.toString()
3736

rewrite-java-21/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ java {
3232
}
3333

3434
tasks.withType<JavaCompile>().configureEach {
35-
// allows --add-exports to in spite of the JDK's restrictions on this
36-
sourceCompatibility = JavaVersion.VERSION_21.toString()
37-
targetCompatibility = JavaVersion.VERSION_21.toString()
35+
// Allow non-LTS versions before Java 21 to use this parser
36+
sourceCompatibility = JavaVersion.VERSION_17.toString()
37+
targetCompatibility = JavaVersion.VERSION_17.toString()
3838

3939
options.release.set(null as Int?) // remove `--release 8` set in `org.openrewrite.java-base`
4040
options.compilerArgs.addAll(

rewrite-java-25/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ java {
3434
tasks.named("licenseFormat") { enabled = false }
3535

3636
tasks.withType<JavaCompile>().configureEach {
37-
// allows --add-exports to in spite of the JDK's restrictions on this
38-
sourceCompatibility = JavaVersion.VERSION_25.toString()
39-
targetCompatibility = JavaVersion.VERSION_25.toString()
37+
// Allow non-LTS versions before Java 25 to use this parser
38+
sourceCompatibility = JavaVersion.VERSION_21.toString()
39+
targetCompatibility = JavaVersion.VERSION_21.toString()
4040

4141
options.release.set(null as Int?) // remove `--release 8` set in `org.openrewrite.java-base`
4242
options.compilerArgs.addAll(

rewrite-java-25/src/main/java/org/openrewrite/java/isolated/ReloadableJava25ParserVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,17 +2232,17 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
22322232
return delimIndex > source.length() - untilDelim.length() ? -1 : delimIndex;
22332233
}
22342234

2235-
private final Function<Tree, Space> semiDelim = _ -> {
2235+
private final Function<Tree, Space> semiDelim = t -> {
22362236
Space prefix = whitespace();
22372237
skip(";");
22382238
return prefix;
22392239
};
2240-
private final Function<Tree, Space> commaDelim = _ -> {
2240+
private final Function<Tree, Space> commaDelim = t -> {
22412241
Space prefix = whitespace();
22422242
skip(",");
22432243
return prefix;
22442244
};
2245-
private final Function<Tree, Space> noDelim = _ -> EMPTY;
2245+
private final Function<Tree, Space> noDelim = t -> EMPTY;
22462246

22472247
private Space whitespace() {
22482248
int nextNonWhitespace = indexOfNextNonWhitespace(cursor, source);

rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
168168
.forName("org.openrewrite.java.Java25Parser")
169169
.getDeclaredMethod("builder")
170170
.invoke(null);
171-
} catch (Exception e) {
171+
} catch (Throwable e) {
172172
//Fall through, look for a parser on an older version.
173173
}
174174
}
@@ -179,7 +179,7 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
179179
.forName("org.openrewrite.java.Java21Parser")
180180
.getDeclaredMethod("builder")
181181
.invoke(null);
182-
} catch (Exception e) {
182+
} catch (Throwable e) {
183183
//Fall through, look for a parser on an older version.
184184
}
185185
}
@@ -190,7 +190,7 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
190190
.forName("org.openrewrite.java.Java17Parser")
191191
.getDeclaredMethod("builder")
192192
.invoke(null);
193-
} catch (Exception e) {
193+
} catch (Throwable e) {
194194
//Fall through, look for a parser on an older version.
195195
}
196196
}
@@ -201,7 +201,7 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
201201
.forName("org.openrewrite.java.Java11Parser")
202202
.getDeclaredMethod("builder")
203203
.invoke(null);
204-
} catch (Exception e) {
204+
} catch (Throwable e) {
205205
//Fall through, look for a parser on an older version.
206206
}
207207
}
@@ -211,7 +211,7 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
211211
.forName("org.openrewrite.java.Java8Parser")
212212
.getDeclaredMethod("builder")
213213
.invoke(null);
214-
} catch (Exception e) {
214+
} catch (Throwable e) {
215215
//Fall through to an exception without making this the "cause".
216216
}
217217

0 commit comments

Comments
 (0)