1 /*
  2  * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096 8275308
 27  * @summary Test latest, latestSupported, underscore as keyword, etc.
 28  * @modules java.compiler
 29  *          jdk.compiler
 30  */
 31 
 32 import java.util.*;
 33 import java.util.function.Predicate;
 34 import javax.lang.model.SourceVersion;
 35 import static javax.lang.model.SourceVersion.*;
 36 
 37 /**
 38  * Verify behavior of latest[Supported] and other methods.
 39  */
 40 public class TestSourceVersion {
 41     public static void main(String... args) {
 42         testLatestSupported();
 43         testVersionVaryingKeywords();
 44         testRestrictedKeywords();
 45         testVar();
 46         testYield();
 47         testValueOfRV();
 48         testRuntimeVersion();
 49     }
 50 
 51     private static void testLatestSupported() {
 52         SourceVersion[] values = SourceVersion.values();
 53         SourceVersion last = values[values.length - 1];
 54         SourceVersion latest = SourceVersion.latest();
 55         SourceVersion latestSupported = SourceVersion.latestSupported();
 56 
 57         if (latest == last &&
 58             latestSupported == SourceVersion.valueOf("RELEASE_" +
 59                                                      Runtime.version().feature()) &&
 60             (latest == latestSupported ||
 61              (latest.ordinal() - latestSupported.ordinal() == 1)) )
 62             return;
 63         else {
 64             throw new RuntimeException("Unexpected release value(s) found:\n" +
 65                                        "latest:\t" + latest + "\n" +
 66                                        "latestSupported:\t" + latestSupported);
 67         }
 68     }
 69 
 70     private static void testVersionVaryingKeywords() {
 71         Map<String, SourceVersion> keyWordStart =
 72             Map.of("strictfp", RELEASE_2,
 73                    "assert",   RELEASE_4,
 74                    "enum",     RELEASE_5,
 75                    "_",        RELEASE_9);
 76 
 77         for (Map.Entry<String, SourceVersion> entry : keyWordStart.entrySet()) {
 78             String key = entry.getKey();
 79             SourceVersion value = entry.getValue();
 80 
 81             check(true,  key, (String s) -> isKeyword(s), "keyword", latest());
 82             check(false, key, (String s) -> isName(s),    "name",    latest());
 83 
 84             for(SourceVersion version : SourceVersion.values()) {
 85                 boolean isKeyword = version.compareTo(value) >= 0;
 86 
 87                 check(isKeyword,  key, (String s) -> isKeyword(s, version), "keyword", version);
 88                 check(!isKeyword, key, (String s) -> isName(s, version),    "name",    version);
 89             }
 90         }
 91     }
 92 
 93     private static void testRestrictedKeywords() {
 94         // Restricted keywords are not full keywords
 95 
 96         /*
 97          * JLS 3.9
 98          * " A further ten character sequences are restricted
 99          * keywords: open, module, requires, transitive, exports,
100          * opens, to, uses, provides, and with"
101          */
102         Set<String> restrictedKeywords =
103             Set.of("open", "module", "requires", "transitive", "exports",
104                    "opens", "to", "uses", "provides", "with",
105                    // Assume "record" and "sealed" will be restricted keywords.
106                    "record", "sealed", "value");
107 
108         for (String key : restrictedKeywords) {
109             for (SourceVersion version : SourceVersion.values()) {
110                 check(false, key, (String s) -> isKeyword(s, version), "keyword", version);
111                 check(true,  key, (String s) -> isName(s, version),    "name",    version);
112             }
113         }
114     }
115 
116     private static void testVar() {
117         for (SourceVersion version : SourceVersion.values()) {
118             Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
119             Predicate<String> isNameVersion = (String s) -> isName(s, version);
120 
121             for (String name : List.of("var", "foo.var", "var.foo")) {
122                 check(false, name, isKeywordVersion, "keyword", version);
123                 check(true, name,  isNameVersion, "name", version);
124             }
125         }
126     }
127 
128     private static void testYield() {
129         for (SourceVersion version : SourceVersion.values()) {
130             Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
131             Predicate<String> isNameVersion = (String s) -> isName(s, version);
132 
133             for  (String name : List.of("yield", "foo.yield", "yield.foo")) {
134                 check(false, name, isKeywordVersion, "keyword", version);
135                 check(true, name,  isNameVersion, "name", version);
136             }
137         }
138     }
139 
140     private static void testValue() {
141         for (SourceVersion version : SourceVersion.values()) {
142             Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
143             Predicate<String> isNameVersion = (String s) -> isName(s, version);
144 
145             for  (String name : List.of("value", "foo.value", "value.foo")) {
146                 check(false, name, isKeywordVersion, "keyword", version);
147                 check(true, name,  isNameVersion, "name", version);
148             }
149         }
150     }
151 
152     private static void check(boolean expected,
153                               String input,
154                               Predicate<String> predicate,
155                               String message,
156                               SourceVersion version) {
157         boolean result  = predicate.test(input);
158         if (result != expected) {
159             throw new RuntimeException("Unexpected " + message +  "-ness of " + input +
160                                        " on " + version);
161         }
162     }
163 
164     /**
165      * Test that SourceVersion.valueOf() maps a Runtime.Version to a
166      * SourceVersion properly. The SourceVersion result is only a
167      * function of the feature() component of a Runtime.Version.
168      */
169     private static void testValueOfRV() {
170         for (SourceVersion sv : SourceVersion.values()) {
171             if (sv == RELEASE_0) {
172                 continue;
173             } else {
174                 // Plain mapping; e.g. "17" -> RELEASE_17
175                 String featureBase = Integer.toString(sv.ordinal());
176                 checkValueOfResult(sv, featureBase);
177 
178                 // More populated runtime version, N.N
179                 checkValueOfResult(sv, featureBase + "." + featureBase);
180             }
181         }
182 
183         // Out of range test
184         try {
185             int latestFeature = SourceVersion.latest().runtimeVersion().feature();
186             SourceVersion.valueOf(Runtime.Version.parse(Integer.toString(latestFeature +1)));
187             throw new RuntimeException("Should not reach");
188         } catch (IllegalArgumentException iae) {
189             ; // Expected
190         }
191     }
192 
193     private static void checkValueOfResult(SourceVersion expected, String versionString) {
194         Runtime.Version rv = Runtime.Version.parse(versionString);
195         SourceVersion  result = SourceVersion.valueOf(rv);
196         if (result != expected) {
197             throw new RuntimeException("Unexpected result " + result +
198                                        " of mapping Runtime.Version " + versionString +
199                                        " intead of " + expected);
200         }
201     }
202 
203     private static void testRuntimeVersion() {
204         for (SourceVersion sv : SourceVersion.values()) {
205             Runtime.Version result = sv.runtimeVersion();
206             if (sv.compareTo(RELEASE_6) < 0) {
207                 if (result != null) {
208                     throw new RuntimeException("Unexpected result non-null " + result +
209                                                " as runtime version of  " + sv);
210                 }
211             } else {
212                 Runtime.Version expected = Runtime.Version.parse(Integer.toString(sv.ordinal()));
213                 if (!result.equals(expected)) {
214                     throw new RuntimeException("Unexpected result " + result +
215                                                " as runtime version of " + sv);
216                 }
217             }
218         }
219     }
220 }