1 /*
2 * Copyright (c) 2002, 2026, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.code;
27
28 import java.util.*;
29
30 import javax.lang.model.SourceVersion;
31 import static javax.lang.model.SourceVersion.*;
32
33 import com.sun.tools.javac.jvm.Target;
34 import com.sun.tools.javac.resources.CompilerProperties.Errors;
35 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
36 import com.sun.tools.javac.util.*;
37 import com.sun.tools.javac.util.JCDiagnostic.Error;
38 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
39
40 import static com.sun.tools.javac.main.Option.*;
41
42 /** The source language version accepted.
43 *
44 * <p><b>This is NOT part of any supported API.
45 * If you write code that depends on this, you do so at your own risk.
46 * This code and its internal interfaces are subject to change or
47 * deletion without notice.</b>
48 */
49 public enum Source {
50 /* 1.0 had no inner classes, and so could not pass the JCK. */
51 // public static final Source JDK1_0 = new Source("1.0");
52
53 /* 1.1 did not have strictfp, and so could not pass the JCK. */
54 // public static final Source JDK1_1 = new Source("1.1");
55
56 /** 1.2 introduced strictfp. */
57 JDK1_2("1.2"),
58
59 /** 1.3 is the same language as 1.2. */
60 JDK1_3("1.3"),
61
62 /** 1.4 introduced assert. */
63 JDK1_4("1.4"),
64
65 /** 1.5 introduced generics, attributes, foreach, boxing, static import,
66 * covariant return, enums, varargs, et al. */
67 JDK5("5"),
68
69 /** 1.6 reports encoding problems as errors instead of warnings. */
70 JDK6("6"),
71
72 /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */
73 JDK7("7"),
74
75 /** 1.8 lambda expressions and default methods. */
76 JDK8("8"),
77
78 /** 1.9 modularity. */
79 JDK9("9"),
80
81 /** 1.10 local-variable type inference (var). */
82 JDK10("10"),
83
84 /** 1.11 local-variable syntax for lambda parameters */
85 JDK11("11"),
86
87 /** 12, no language features; switch expression in preview */
88 JDK12("12"),
89
90 /**
91 * 13, no language features; text blocks and revised switch
92 * expressions in preview
93 */
94 JDK13("13"),
95
96 /**
97 * 14, switch expressions; pattern matching, records, and revised
98 * text blocks in preview
99 */
100 JDK14("14"),
101
102 /**
103 * 15, text blocks
104 */
105 JDK15("15"),
106
107 /**
108 * 16, records and pattern matching for instanceof
109 */
110 JDK16("16"),
111
112 /**
113 * 17, sealed classes, restoration of always-strict floating-point
114 */
115 JDK17("17"),
116
117 /**
118 * 18, no major changes
119 */
120 JDK18("18"),
121
122 /**
123 * 19, no major changes
124 */
125 JDK19("19"),
126
127 /**
128 * 20, no major changes
129 */
130 JDK20("20"),
131
132 /**
133 * 21, tbd
134 */
135 JDK21("21"),
136
137 /**
138 * 22, tbd
139 */
140 JDK22("22"),
141
142 /**
143 * 23, tbd
144 */
145 JDK23("23"),
146
147 /**
148 * 24, tbd
149 */
150 JDK24("24"),
151
152 /**
153 * 25, tbd
154 */
155 JDK25("25"),
156
157 /**
158 * 26, tbd
159 */
160 JDK26("26"),
161
162 /**
163 * 27, tbd
164 */
165 JDK27("27"),
166
167 /**
168 * 28, tbd
169 */
170 JDK28("28"),
171 ; // Reduce code churn when appending new constants
172
173 private static final Context.Key<Source> sourceKey = new Context.Key<>();
174
175 public static Source instance(Context context) {
176 Source instance = context.get(sourceKey);
177 if (instance == null) {
178 Options options = Options.instance(context);
179 String sourceString = options.get(SOURCE);
180 if (sourceString != null) instance = lookup(sourceString);
181 if (instance == null) instance = DEFAULT;
182 context.put(sourceKey, instance);
183 }
184 return instance;
185 }
186
187 public final String name;
188
189 private static final Map<String,Source> tab = new HashMap<>();
190 static {
191 for (Source s : values()) {
192 tab.put(s.name, s);
193 }
194 tab.put("1.5", JDK5); // Make 5 an alias for 1.5
195 tab.put("1.6", JDK6); // Make 6 an alias for 1.6
196 tab.put("1.7", JDK7); // Make 7 an alias for 1.7
197 tab.put("1.8", JDK8); // Make 8 an alias for 1.8
198 tab.put("1.9", JDK9); // Make 9 an alias for 1.9
199 tab.put("1.10", JDK10); // Make 10 an alias for 1.10
200 // Decline to make 1.11 an alias for 11.
201 }
202
203 private Source(String name) {
204 this.name = name;
205 }
206
207 public static final Source MIN = Source.JDK8;
208
209 private static final Source MAX = values()[values().length - 1];
210
211 public static final Source DEFAULT = MAX;
212
213 public static Source lookup(String name) {
214 return tab.get(name);
215 }
216
217 public boolean isSupported() {
218 return this.compareTo(MIN) >= 0;
219 }
220
221 public static boolean isSupported(Feature feature, int majorVersion) {
222 Source source = null;
223 for (Target target : Target.values()) {
224 if (majorVersion == target.majorVersion) {
225 source = lookup(target.name);
226 }
227 }
228 if (source != null) {
229 return feature.allowedInSource(source);
230 }
231 return false;
232 }
233
234 public Target requiredTarget() {
235 return switch(this) {
236 case JDK28 -> Target.JDK1_28;
237 case JDK27 -> Target.JDK1_27;
238 case JDK26 -> Target.JDK1_26;
239 case JDK25 -> Target.JDK1_25;
240 case JDK24 -> Target.JDK1_24;
241 case JDK23 -> Target.JDK1_23;
242 case JDK22 -> Target.JDK1_22;
243 case JDK21 -> Target.JDK1_21;
244 case JDK20 -> Target.JDK1_20;
245 case JDK19 -> Target.JDK1_19;
246 case JDK18 -> Target.JDK1_18;
247 case JDK17 -> Target.JDK1_17;
248 case JDK16 -> Target.JDK1_16;
249 case JDK15 -> Target.JDK1_15;
250 case JDK14 -> Target.JDK1_14;
251 case JDK13 -> Target.JDK1_13;
252 case JDK12 -> Target.JDK1_12;
253 case JDK11 -> Target.JDK1_11;
254 case JDK10 -> Target.JDK1_10;
255 case JDK9 -> Target.JDK1_9;
256 case JDK8 -> Target.JDK1_8;
257 case JDK7 -> Target.JDK1_7;
258 case JDK6 -> Target.JDK1_6;
259 case JDK5 -> Target.JDK1_5;
260 case JDK1_4 -> Target.JDK1_4;
261 default -> Target.JDK1_1;
262 };
263 }
264
265 /**
266 * Models a feature of the Java programming language. Each feature can be associated with a
267 * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
268 * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
269 */
270 public enum Feature {
271
272 MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL),
273 EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL),
274 DEPRECATION_ON_IMPORT(MIN, JDK8),
275 PRIVATE_SAFE_VARARGS(JDK9),
276 DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
277 UNDERSCORE_IDENTIFIER(MIN, JDK8),
278 PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
279 LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
280 VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
281 IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8),
282 SWITCH_MULTIPLE_CASE_LABELS(JDK14, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
283 SWITCH_RULE(JDK14, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
284 SWITCH_EXPRESSION(JDK14, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
285 NO_TARGET_ANNOTATION_APPLICABILITY(JDK14),
286 TEXT_BLOCKS(JDK15, Fragments.FeatureTextBlocks, DiagKind.PLURAL),
287 PATTERN_MATCHING_IN_INSTANCEOF(JDK16, Fragments.FeaturePatternMatchingInstanceof, DiagKind.NORMAL),
288 REIFIABLE_TYPES_INSTANCEOF(JDK16, Fragments.FeatureReifiableTypesInstanceof, DiagKind.PLURAL),
289 RECORDS(JDK16, Fragments.FeatureRecords, DiagKind.PLURAL),
290 SEALED_CLASSES(JDK17, Fragments.FeatureSealedClasses, DiagKind.PLURAL),
291 CASE_NULL(JDK21, Fragments.FeatureCaseNull, DiagKind.NORMAL),
292 PATTERN_SWITCH(JDK21, Fragments.FeaturePatternSwitch, DiagKind.PLURAL),
293 REDUNDANT_STRICTFP(JDK17),
294 TYPE_ANNOTATIONS_ON_VAR_LAMBDA_PARAMETER(MIN, JDK19),
295 UNCONDITIONAL_PATTERN_IN_INSTANCEOF(JDK21, Fragments.FeatureUnconditionalPatternsInInstanceof, DiagKind.PLURAL),
296 RECORD_PATTERNS(JDK21, Fragments.FeatureDeconstructionPatterns, DiagKind.PLURAL),
297 IMPLICIT_CLASSES(JDK25, Fragments.FeatureImplicitClasses, DiagKind.PLURAL),
298 WARN_ON_ILLEGAL_UTF8(MIN, JDK21),
299 UNNAMED_VARIABLES(JDK22, Fragments.FeatureUnnamedVariables, DiagKind.PLURAL),
300 PRIMITIVE_PATTERNS(JDK23, Fragments.FeaturePrimitivePatterns, DiagKind.PLURAL),
301 FLEXIBLE_CONSTRUCTORS(JDK25, Fragments.FeatureFlexibleConstructors, DiagKind.NORMAL),
302 MODULE_IMPORTS(JDK25, Fragments.FeatureModuleImports, DiagKind.PLURAL),
303 JAVA_BASE_TRANSITIVE(JDK25, Fragments.FeatureJavaBaseTransitive, DiagKind.PLURAL),
304 PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
305 ERASE_POLY_SIG_RETURN_TYPE(JDK24),
306 CAPTURE_MREF_RETURN_TYPE(JDK26),
307 VALUE_CLASSES(DEFAULT, Fragments.FeatureValueClasses, DiagKind.PLURAL),
308 ;
309
310 enum DiagKind {
311 NORMAL,
312 PLURAL;
313 }
314
315 private final Source minLevel;
316 private final Source maxLevel;
317 private final Fragment optFragment;
318 private final DiagKind optKind;
319
320 Feature(Source minLevel) {
321 this(minLevel, null, null);
322 }
323
324 Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
325 this(minLevel, MAX, optFragment, optKind);
326 }
327
328 Feature(Source minLevel, Source maxLevel) {
329 this(minLevel, maxLevel, null, null);
330 }
331
332 Feature(Source minLevel, Source maxLevel, Fragment optFragment, DiagKind optKind) {
333 this.minLevel = minLevel;
334 this.maxLevel = maxLevel;
335 this.optFragment = optFragment;
336 this.optKind = optKind;
337 }
338
339 public boolean allowedInSource(Source source) {
340 return source.compareTo(minLevel) >= 0 &&
341 source.compareTo(maxLevel) <= 0;
342 }
343
344 public boolean isPlural() {
345 Assert.checkNonNull(optKind);
346 return optKind == DiagKind.PLURAL;
347 }
348
349 public Fragment nameFragment() {
350 Assert.checkNonNull(optFragment);
351 return optFragment;
352 }
353
354 public Fragment fragment(String sourceName) {
355 Assert.checkNonNull(optFragment);
356 return optKind == DiagKind.NORMAL ?
357 Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
358 Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
359 }
360
361 public Error error(String sourceName) {
362 Assert.checkNonNull(optFragment);
363 return optKind == DiagKind.NORMAL ?
364 Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
365 Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
366 }
367 }
368
369 public static SourceVersion toSourceVersion(Source source) {
370 return switch(source) {
371 case JDK1_2 -> RELEASE_2;
372 case JDK1_3 -> RELEASE_3;
373 case JDK1_4 -> RELEASE_4;
374 case JDK5 -> RELEASE_5;
375 case JDK6 -> RELEASE_6;
376 case JDK7 -> RELEASE_7;
377 case JDK8 -> RELEASE_8;
378 case JDK9 -> RELEASE_9;
379 case JDK10 -> RELEASE_10;
380 case JDK11 -> RELEASE_11;
381 case JDK12 -> RELEASE_12;
382 case JDK13 -> RELEASE_13;
383 case JDK14 -> RELEASE_14;
384 case JDK15 -> RELEASE_15;
385 case JDK16 -> RELEASE_16;
386 case JDK17 -> RELEASE_17;
387 case JDK18 -> RELEASE_18;
388 case JDK19 -> RELEASE_19;
389 case JDK20 -> RELEASE_20;
390 case JDK21 -> RELEASE_21;
391 case JDK22 -> RELEASE_22;
392 case JDK23 -> RELEASE_23;
393 case JDK24 -> RELEASE_24;
394 case JDK25 -> RELEASE_25;
395 case JDK26 -> RELEASE_26;
396 case JDK27 -> RELEASE_27;
397 case JDK28 -> RELEASE_28;
398 default -> null;
399 };
400 }
401 }