Skip to content

Commit e8e4cc4

Browse files
committed
Implement JctCompiler.useRuntimeRelease()
This sets the compiler to use the same language version as the current runtime feature version in the running JVM.
1 parent 223268b commit e8e4cc4

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ default JctCompiler release(SourceVersion release) {
520520
return release(Integer.toString(release.ordinal()));
521521
}
522522

523+
/**
524+
* Request that the compiler uses a language version that corresponds to the
525+
* runtime language version in use on the JVM running tests.
526+
*
527+
* <p>For example, running this on JRE 19 would set the release to "19".
528+
*
529+
* <p>This calls {@link #release(int) internally}.
530+
*
531+
* @return this compiler object for further call chaining.
532+
*/
533+
@API(since = "1.1.0", status = Status.STABLE)
534+
default JctCompiler useRuntimeRelease() {
535+
return release(Runtime.version().feature());
536+
}
537+
523538
/**
524539
* Get the current source version that is set, or {@code null} if left to the compiler to decide.
525540
* default.

java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JavacJctCompilerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public JctFileManagerFactory getFileManagerFactory() {
7373
* @return the minimum supported version.
7474
* @since 1.0.0
7575
*/
76-
@API(since = "1.0.0", status = Status.STABLE)
76+
@API(since = "1.0.0", status = Status.INTERNAL)
7777
public static int getEarliestSupportedVersionInt() {
7878
// Purposely do not hardcode members of the SourceVersion enum here other
7979
// than utility methods, as this prevents compilation problems on various
@@ -97,7 +97,7 @@ public static int getEarliestSupportedVersionInt() {
9797
*
9898
* @return the maximum supported version.
9999
*/
100-
@API(since = "1.0.0", status = Status.STABLE)
100+
@API(since = "1.0.0", status = Status.INTERNAL)
101101
public static int getLatestSupportedVersionInt() {
102102
return SourceVersion.latestSupported().ordinal();
103103
}

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/JctCompilerTest.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ void addCompilerOptionsCallsTheExpectedMethod() {
115115
assertThat(result).isSameAs(compiler);
116116
}
117117

118-
@DisplayName("releaseVersion(int) should call releaseVersion(String)")
118+
@DisplayName("release(int) should call release(String)")
119119
@ValueSource(ints = {11, 12, 13, 14, 15, 16, 17})
120120
@ParameterizedTest(name = "for version = {0}")
121-
void releaseVersionIntCallsReleaseVersionString(int versionInt) {
121+
void releaseIntCallsReleaseVersionString(int versionInt) {
122122
// Given
123123
var versionString = "" + versionInt;
124124
given(compiler.release(anyInt())).willCallRealMethod();
@@ -132,10 +132,10 @@ void releaseVersionIntCallsReleaseVersionString(int versionInt) {
132132
assertThat(result).isSameAs(compiler);
133133
}
134134

135-
@DisplayName("releaseVersion(int) throws an IllegalArgumentException for negative versions")
135+
@DisplayName("release(int) throws an IllegalArgumentException for negative versions")
136136
@ValueSource(ints = {-1, -2, -5, -100_000})
137137
@ParameterizedTest(name = "for version = {0}")
138-
void releaseVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
138+
void releaseIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
139139
// Given
140140
given(compiler.release(anyInt())).willCallRealMethod();
141141

@@ -145,10 +145,10 @@ void releaseVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int vers
145145
.hasMessage("Cannot provide a release version less than 0");
146146
}
147147

148-
@DisplayName("releaseVersion(SourceVersion) should call releaseVersion(String)")
148+
@DisplayName("release(SourceVersion) should call release(String)")
149149
@MethodSource("sourceVersions")
150150
@ParameterizedTest(name = "for version = {0}")
151-
void releaseVersionSourceVersionCallsReleaseVersionString(
151+
void releaseSourceVersionCallsReleaseVersionString(
152152
SourceVersion versionEnum,
153153
String versionString
154154
) {
@@ -164,10 +164,25 @@ void releaseVersionSourceVersionCallsReleaseVersionString(
164164
assertThat(result).isSameAs(compiler);
165165
}
166166

167-
@DisplayName("sourceVersion(int) should call sourceVersion(String)")
167+
@DisplayName(".useRuntimeRelease() should call .release(int) with the runtime feature version")
168+
@Test
169+
void useRuntimeReleaseShouldSetTheRuntimeFeatureVersion() {
170+
// Given
171+
given(compiler.useRuntimeRelease()).willCallRealMethod();
172+
given(compiler.release(anyInt())).will(ctx -> compiler);
173+
174+
// When
175+
var result = compiler.useRuntimeRelease();
176+
177+
// Then
178+
then(compiler).should().release(Runtime.version().feature());
179+
assertThat(result).isSameAs(compiler);
180+
}
181+
182+
@DisplayName("source(int) should call source(String)")
168183
@ValueSource(ints = {11, 12, 13, 14, 15, 16, 17})
169184
@ParameterizedTest(name = "for version = {0}")
170-
void sourceVersionIntCallsReleaseVersionString(int versionInt) {
185+
void sourceIntCallsReleaseVersionString(int versionInt) {
171186
// Given
172187
var versionString = "" + versionInt;
173188
given(compiler.source(anyInt())).willCallRealMethod();
@@ -181,10 +196,10 @@ void sourceVersionIntCallsReleaseVersionString(int versionInt) {
181196
assertThat(result).isSameAs(compiler);
182197
}
183198

184-
@DisplayName("sourceVersion(int) throws an IllegalArgumentException for negative versions")
199+
@DisplayName("source(int) throws an IllegalArgumentException for negative versions")
185200
@ValueSource(ints = {-1, -2, -5, -100_000})
186201
@ParameterizedTest(name = "for version = {0}")
187-
void sourceVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
202+
void sourceIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
188203
// Given
189204
given(compiler.source(anyInt())).willCallRealMethod();
190205

@@ -194,10 +209,10 @@ void sourceVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int versi
194209
.hasMessage("Cannot provide a source version less than 0");
195210
}
196211

197-
@DisplayName("sourceVersion(SourceVersion) should call sourceVersion(String)")
212+
@DisplayName("source(SourceVersion) should call source(String)")
198213
@MethodSource("sourceVersions")
199214
@ParameterizedTest(name = "for version = {0}")
200-
void sourceVersionSourceVersionCallsReleaseVersionString(
215+
void sourceSourceVersionCallsReleaseVersionString(
201216
SourceVersion versionEnum,
202217
String versionString
203218
) {
@@ -213,10 +228,10 @@ void sourceVersionSourceVersionCallsReleaseVersionString(
213228
assertThat(result).isSameAs(compiler);
214229
}
215230

216-
@DisplayName("targetVersion(int) should call targetVersion(String)")
231+
@DisplayName("target(int) should call target(String)")
217232
@ValueSource(ints = {11, 12, 13, 14, 15, 16, 17})
218233
@ParameterizedTest(name = "for version = {0}")
219-
void targetVersionIntCallsReleaseVersionString(int versionInt) {
234+
void targetIntCallsReleaseVersionString(int versionInt) {
220235
// Given
221236
var versionString = "" + versionInt;
222237
given(compiler.target(anyInt())).willCallRealMethod();
@@ -230,10 +245,10 @@ void targetVersionIntCallsReleaseVersionString(int versionInt) {
230245
assertThat(result).isSameAs(compiler);
231246
}
232247

233-
@DisplayName("targetVersion(int) throws an IllegalArgumentException for negative versions")
248+
@DisplayName("target(int) throws an IllegalArgumentException for negative versions")
234249
@ValueSource(ints = {-1, -2, -5, -100_000})
235250
@ParameterizedTest(name = "for version = {0}")
236-
void targetVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
251+
void targetIntThrowsIllegalArgumentExceptionForNegativeVersions(int versionInt) {
237252
// Given
238253
given(compiler.target(anyInt())).willCallRealMethod();
239254

@@ -243,10 +258,10 @@ void targetVersionIntThrowsIllegalArgumentExceptionForNegativeVersions(int versi
243258
.hasMessage("Cannot provide a target version less than 0");
244259
}
245260

246-
@DisplayName("targetVersion(SourceVersion) should call targetVersion(String)")
261+
@DisplayName("target(SourceVersion) should call target(String)")
247262
@MethodSource("sourceVersions")
248263
@ParameterizedTest(name = "for version = {0}")
249-
void targetVersionSourceVersionCallsReleaseVersionString(
264+
void targetSourceVersionCallsReleaseVersionString(
250265
SourceVersion versionEnum,
251266
String versionString
252267
) {

0 commit comments

Comments
 (0)