Skip to content

Commit 48f3fca

Browse files
committed
8275308: Add valueOf(Runtime.Version) factory to SourceVersion
Reviewed-by: jjg
1 parent c6339cb commit 48f3fca

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

src/java.compiler/share/classes/javax/lang/model/SourceVersion.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,58 @@ public static boolean isKeyword(CharSequence s, SourceVersion version) {
492492
return false;
493493
}
494494
}
495+
496+
/**
497+
* {@return the latest source version that is usable under the
498+
* runtime version argument} If the runtime version's {@linkplain
499+
* Runtime.Version#feature() feature} is greater than the feature
500+
* of the {@linkplain #runtimeVersion() runtime version} of the
501+
* {@linkplain #latest() latest source version}, an {@code
502+
* IllegalArgumentException} is thrown.
503+
*
504+
* <p>Because the source versions of the Java programming language
505+
* have so far followed a linear progression, only the feature
506+
* component of a runtime version is queried to determine the
507+
* mapping to a source version. If that linearity changes in the
508+
* future, other components of the runtime version may influence
509+
* the result.
510+
*
511+
* @apiNote
512+
* An expression to convert from a string value, for example
513+
* {@code "17"}, to the corresponding source version, {@code
514+
* RELEASE_17}, is:
515+
*
516+
* <pre>{@code SourceVersion.valueOf(Runtime.Version.parse("17"))}</pre>
517+
*
518+
* @param rv runtime version to map to a source version
519+
* @throws IllegalArgumentException if the feature of version
520+
* argument is greater than the feature of the platform version.
521+
* @since 18
522+
*/
523+
public static SourceVersion valueOf(Runtime.Version rv) {
524+
// Could also implement this as a switch where a case was
525+
// added with each new release.
526+
return valueOf("RELEASE_" + rv.feature());
527+
}
528+
529+
/**
530+
* {@return the least runtime version that supports this source
531+
* version; otherwise {@code null}} The returned runtime version
532+
* has a {@linkplain Runtime.Version#feature() feature} large
533+
* enough to support this source version and has no other elements
534+
* set.
535+
*
536+
* Source versions greater than or equal to {@link RELEASE_6}
537+
* have non-{@code null} results.
538+
* @since 18
539+
*/
540+
public Runtime.Version runtimeVersion() {
541+
// The javax.lang.model API was added in JDK 6; for now,
542+
// limiting supported range to 6 and up.
543+
if (this.compareTo(RELEASE_6) >= 0) {
544+
return Runtime.Version.parse(Integer.toString(ordinal()));
545+
} else {
546+
return null;
547+
}
548+
}
495549
}

test/langtools/tools/javac/processing/model/TestSourceVersion.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096
26+
* @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096 8275308
2727
* @summary Test latest, latestSupported, underscore as keyword, etc.
2828
* @author Joseph D. Darcy
2929
* @modules java.compiler
@@ -45,6 +45,8 @@ public static void main(String... args) {
4545
testRestrictedKeywords();
4646
testVar();
4747
testYield();
48+
testValueOfRV();
49+
testRuntimeVersion();
4850
}
4951

5052
private static void testLatestSupported() {
@@ -147,4 +149,61 @@ private static void check(boolean expected,
147149
" on " + version);
148150
}
149151
}
152+
153+
/**
154+
* Test that SourceVersion.valueOf() maps a Runtime.Version to a
155+
* SourceVersion properly. The SourceVersion result is only a
156+
* function of the feature() component of a Runtime.Version.
157+
*/
158+
private static void testValueOfRV() {
159+
for (SourceVersion sv : SourceVersion.values()) {
160+
if (sv == RELEASE_0) {
161+
continue;
162+
} else {
163+
// Plain mapping; e.g. "17" -> RELEASE_17
164+
String featureBase = Integer.toString(sv.ordinal());
165+
checkValueOfResult(sv, featureBase);
166+
167+
// More populated runtime version, N.N
168+
checkValueOfResult(sv, featureBase + "." + featureBase);
169+
}
170+
}
171+
172+
// Out of range test
173+
try {
174+
int latestFeature = SourceVersion.latest().runtimeVersion().feature();
175+
SourceVersion.valueOf(Runtime.Version.parse(Integer.toString(latestFeature +1)));
176+
throw new RuntimeException("Should not reach");
177+
} catch (IllegalArgumentException iae) {
178+
; // Expected
179+
}
180+
}
181+
182+
private static void checkValueOfResult(SourceVersion expected, String versionString) {
183+
Runtime.Version rv = Runtime.Version.parse(versionString);
184+
SourceVersion result = SourceVersion.valueOf(rv);
185+
if (result != expected) {
186+
throw new RuntimeException("Unexpected result " + result +
187+
" of mapping Runtime.Version " + versionString +
188+
" intead of " + expected);
189+
}
190+
}
191+
192+
private static void testRuntimeVersion() {
193+
for (SourceVersion sv : SourceVersion.values()) {
194+
Runtime.Version result = sv.runtimeVersion();
195+
if (sv.compareTo(RELEASE_6) < 0) {
196+
if (result != null) {
197+
throw new RuntimeException("Unexpected result non-null " + result +
198+
" as runtime version of " + sv);
199+
}
200+
} else {
201+
Runtime.Version expected = Runtime.Version.parse(Integer.toString(sv.ordinal()));
202+
if (!result.equals(expected)) {
203+
throw new RuntimeException("Unexpected result " + result +
204+
" as runtime version of " + sv);
205+
}
206+
}
207+
}
208+
}
150209
}

0 commit comments

Comments
 (0)