1 /*
2 * Copyright (c) 2002, 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. 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.jvm;
27
28 import java.util.*;
29
30 import com.sun.tools.javac.code.Flags;
31 import com.sun.tools.javac.code.Symbol;
32 import com.sun.tools.javac.util.*;
33
34 import static com.sun.tools.javac.main.Option.TARGET;
35
36 /** The classfile version target.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own risk.
40 * This code and its internal interfaces are subject to change or
41 * deletion without notice.</b>
42 */
43 public enum Target {
44 JDK1_1("1.1", 45, 3),
45 JDK1_2("1.2", 46, 0),
46 JDK1_3("1.3", 47, 0),
47
48 /** J2SE1.4 = Merlin. */
49 JDK1_4("1.4", 48, 0),
50
51 /** JDK 5, codename Tiger. */
52 JDK1_5("5", 49, 0),
53
54 /** JDK 6. */
55 JDK1_6("6", 50, 0),
56
57 /** JDK 7. */
58 JDK1_7("7", 51, 0),
59
60 /** JDK 8. */
61 JDK1_8("8", 52, 0),
62
63 /** JDK 9. */
64 JDK1_9("9", 53, 0),
65
66 /** JDK 10. */
67 JDK1_10("10", 54, 0),
68
69 /** JDK 11. */
70 JDK1_11("11", 55, 0),
71
72 /** JDK 12. */
73 JDK1_12("12", 56, 0),
74
75 /** JDK 13. */
76 JDK1_13("13", 57, 0),
77
78 /** JDK 14. */
79 JDK1_14("14", 58, 0),
80
81 /** JDK 15. */
82 JDK1_15("15", 59, 0),
83
84 /** JDK 16. */
85 JDK1_16("16", 60, 0),
86
87 /** JDK 17. */
88 JDK1_17("17", 61, 0),
89
90 /** JDK 18. */
91 JDK1_18("18", 62, 0),
92
93 /** JDK 19. */
94 JDK1_19("19", 63, 0),
95
96 /** JDK 20. */
97 JDK1_20("20", 64, 0),
98
99 /** JDK 21. */
100 JDK1_21("21", 65, 0),
101
102 /** JDK 22. */
103 JDK1_22("22", 66, 0),
104
105 /** JDK 23. */
106 JDK1_23("23", 67, 0),
107
108 /** JDK 24. */
109 JDK1_24("24", 68, 0),
110
111 /** JDK 25. */
112 JDK1_25("25", 69, 0),
113
114 /** JDK 26. */
115 JDK1_26("26", 70, 0),
116 ; // Reduce code churn when appending new constants
117
118 private static final Context.Key<Target> targetKey = new Context.Key<>();
119
120 public static Target instance(Context context) {
121 Target instance = context.get(targetKey);
122 if (instance == null) {
123 Options options = Options.instance(context);
124 String targetString = options.get(TARGET);
125 if (targetString != null) instance = lookup(targetString);
126 if (instance == null) instance = DEFAULT;
127 context.put(targetKey, instance);
128 }
129 return instance;
130 }
131
132 public static final Target MIN = Target.JDK1_8;
133
134 private static final Target MAX = values()[values().length - 1];
135
136 public static final Target DEFAULT = MAX;
137
138 private static final Map<String,Target> tab = new HashMap<>();
139 static {
140 for (Target t : values()) {
141 tab.put(t.name, t);
142 }
143 tab.put("1.5", JDK1_5);
144 tab.put("1.6", JDK1_6);
145 tab.put("1.7", JDK1_7);
146 tab.put("1.8", JDK1_8);
147 tab.put("1.9", JDK1_9);
148 tab.put("1.10", JDK1_10);
149 }
150
151 public final String name;
152 public final int majorVersion;
153 public final int minorVersion;
154 private Target(String name, int majorVersion, int minorVersion) {
155 this.name = name;
156 this.majorVersion = majorVersion;
157 this.minorVersion = minorVersion;
158 }
159
160 public static Target lookup(String name) {
161 return tab.get(name);
162 }
163
164 public boolean isSupported() {
165 return this.compareTo(MIN) >= 0;
166 }
167
168 /** Return the character to be used in constructing synthetic
169 * identifiers, where not specified by the JLS.
170 */
171 public char syntheticNameChar() {
172 return '$';
173 }
174
175 /** Does the target VM expect MethodParameters attributes?
176 */
177 public boolean hasMethodParameters() {
178 return compareTo(JDK1_8) >= 0;
179 }
180
181 /** Does the target JDK contain StringConcatFactory class?
182 */
183 public boolean hasStringConcatFactory() {
184 return compareTo(JDK1_9) >= 0;
185 }
186
187 /** Value of platform release used to access multi-release jar files
188 */
189 public String multiReleaseValue() {
190 return Integer.toString(this.ordinal() - Target.JDK1_1.ordinal() + 1);
191 }
192
193 /** All modules that export an API are roots when compiling code in the unnamed
194 * module and targeting 11 or newer.
195 */
196 public boolean allApiModulesAreRoots() {
197 return compareTo(JDK1_11) >= 0;
198 }
199
200 /** Does the target VM support nestmate access?
201 */
202 public boolean hasNestmateAccess() {
203 return compareTo(JDK1_11) >= 0;
204 }
205
206 /** language runtime uses nest-based access.
207 * e.g. lambda and string concat spin dynamic proxy class as a nestmate
208 * of the target class
209 */
210 public boolean runtimeUseNestAccess() {
211 return compareTo(JDK1_15) >= 0;
212 }
213
214 /** Does the target VM support virtual private invocations?
215 */
216 public boolean hasVirtualPrivateInvoke() {
217 return compareTo(JDK1_11) >= 0;
218 }
219
220 /** Does the target VM support sealed types
221 */
222 public boolean hasSealedClasses() {
223 return compareTo(JDK1_15) >= 0;
224 }
225
226 /** Does the target VM support value classes
227 */
228 public boolean hasValueClasses() {
229 return compareTo(JDK1_23) >= 0;
230 }
231
232 /** Is the ACC_STRICT bit redundant and obsolete
233 */
234 public boolean obsoleteAccStrict() {
235 return compareTo(JDK1_17) >= 0;
236 }
237
238 /** Omit unused enclosing instance fields from inner classes that don't access enclosing
239 * instance state.
240 */
241 public boolean optimizeOuterThis() {
242 return compareTo(JDK1_18) >= 0;
243 }
244
245 /** Releases prior to JDK 23 expect a less precise SwitchBootstraps.typeSwitch signature on the selectorType
246 */
247 public boolean usesReferenceOnlySelectorTypes() {
248 return compareTo(Target.JDK1_23) < 0;
249 }
250
251 /**
252 * Should we emit a null check against incoming outer this argument by default?
253 */
254 public boolean nullCheckOuterThisByDefault() {
255 return compareTo(JDK1_25) >= 0;
256 }
257
258 /** Releases prior to JDK 23 don't allow primitive types as case labels in
259 * SwitchBootstrap.typeSwitch
260 */
261 public boolean switchBootstrapOnlyAllowsReferenceTypesAsCaseLabels() {
262 return compareTo(Target.JDK1_23) < 0;
263 }
264 }