1 /*
  2  * Copyright (c) 2002, 2021, 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, tbd
109       */
110     JDK16("16"),
111 
112     /**
113       * 17, tbd
114       */
115     JDK17("17"),
116 
117     /**
118       * 18, tbd
119       */
120     JDK18("18");
121 
122     private static final Context.Key<Source> sourceKey = new Context.Key<>();
123 
124     public static Source instance(Context context) {
125         Source instance = context.get(sourceKey);
126         if (instance == null) {
127             Options options = Options.instance(context);
128             String sourceString = options.get(SOURCE);
129             if (sourceString != null) instance = lookup(sourceString);
130             if (instance == null) instance = DEFAULT;
131             context.put(sourceKey, instance);
132         }
133         return instance;
134     }
135 
136     public final String name;
137 
138     private static final Map<String,Source> tab = new HashMap<>();
139     static {
140         for (Source s : values()) {
141             tab.put(s.name, s);
142         }
143         tab.put("1.5", JDK5); // Make 5 an alias for 1.5
144         tab.put("1.6", JDK6); // Make 6 an alias for 1.6
145         tab.put("1.7", JDK7); // Make 7 an alias for 1.7
146         tab.put("1.8", JDK8); // Make 8 an alias for 1.8
147         tab.put("1.9", JDK9); // Make 9 an alias for 1.9
148         tab.put("1.10", JDK10); // Make 10 an alias for 1.10
149         // Decline to make 1.11 an alias for 11.
150     }
151 
152     private Source(String name) {
153         this.name = name;
154     }
155 
156     public static final Source MIN = Source.JDK7;
157 
158     private static final Source MAX = values()[values().length - 1];
159 
160     public static final Source DEFAULT = MAX;
161 
162     public static Source lookup(String name) {
163         return tab.get(name);
164     }
165 
166     public boolean isSupported() {
167         return this.compareTo(MIN) >= 0;
168     }
169 
170     public Target requiredTarget() {
171         return switch(this) {
172         case JDK18  -> Target.JDK1_18;
173         case JDK17  -> Target.JDK1_17;
174         case JDK16  -> Target.JDK1_16;
175         case JDK15  -> Target.JDK1_15;
176         case JDK14  -> Target.JDK1_14;
177         case JDK13  -> Target.JDK1_13;
178         case JDK12  -> Target.JDK1_12;
179         case JDK11  -> Target.JDK1_11;
180         case JDK10  -> Target.JDK1_10;
181         case JDK9   -> Target.JDK1_9;
182         case JDK8   -> Target.JDK1_8;
183         case JDK7   -> Target.JDK1_7;
184         case JDK6   -> Target.JDK1_6;
185         case JDK5   -> Target.JDK1_5;
186         case JDK1_4 -> Target.JDK1_4;
187         default     -> Target.JDK1_1;
188         };
189     }
190 
191     /**
192      * Models a feature of the Java programming language. Each feature can be associated with a
193      * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
194      * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
195      */
196     public enum Feature {
197 
198         DIAMOND(JDK7, Fragments.FeatureDiamond, DiagKind.NORMAL),
199         MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL),
200         EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL),
201         DEPRECATION_ON_IMPORT(MIN, JDK8),
202         POLY(JDK8),
203         LAMBDA(JDK8, Fragments.FeatureLambda, DiagKind.PLURAL),
204         METHOD_REFERENCES(JDK8, Fragments.FeatureMethodReferences, DiagKind.PLURAL),
205         DEFAULT_METHODS(JDK8, Fragments.FeatureDefaultMethods, DiagKind.PLURAL),
206         STATIC_INTERFACE_METHODS(JDK8, Fragments.FeatureStaticIntfMethods, DiagKind.PLURAL),
207         STATIC_INTERFACE_METHODS_INVOKE(JDK8, Fragments.FeatureStaticIntfMethodInvoke, DiagKind.PLURAL),
208         STRICT_METHOD_CLASH_CHECK(JDK8),
209         EFFECTIVELY_FINAL_IN_INNER_CLASSES(JDK8),
210         TYPE_ANNOTATIONS(JDK8, Fragments.FeatureTypeAnnotations, DiagKind.PLURAL),
211         ANNOTATIONS_AFTER_TYPE_PARAMS(JDK8, Fragments.FeatureAnnotationsAfterTypeParams, DiagKind.PLURAL),
212         REPEATED_ANNOTATIONS(JDK8, Fragments.FeatureRepeatableAnnotations, DiagKind.PLURAL),
213         INTERSECTION_TYPES_IN_CAST(JDK8, Fragments.FeatureIntersectionTypesInCast, DiagKind.PLURAL),
214         GRAPH_INFERENCE(JDK8),
215         FUNCTIONAL_INTERFACE_MOST_SPECIFIC(JDK8),
216         POST_APPLICABILITY_VARARGS_ACCESS_CHECK(JDK8),
217         MAP_CAPTURES_TO_BOUNDS(MIN, JDK7),
218         PRIVATE_SAFE_VARARGS(JDK9),
219         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
220         UNDERSCORE_IDENTIFIER(MIN, JDK8),
221         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
222         LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
223         VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
224         IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8),
225         SWITCH_MULTIPLE_CASE_LABELS(JDK14, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
226         SWITCH_RULE(JDK14, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
227         SWITCH_EXPRESSION(JDK14, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
228         TEXT_BLOCKS(JDK15, Fragments.FeatureTextBlocks, DiagKind.PLURAL),
229         PATTERN_MATCHING_IN_INSTANCEOF(JDK16, Fragments.FeaturePatternMatchingInstanceof, DiagKind.NORMAL),
230         REIFIABLE_TYPES_INSTANCEOF(JDK16, Fragments.FeatureReifiableTypesInstanceof, DiagKind.PLURAL),
231         RECORDS(JDK16, Fragments.FeatureRecords, DiagKind.PLURAL),
232         SEALED_CLASSES(JDK17, Fragments.FeatureSealedClasses, DiagKind.PLURAL),
233         CASE_NULL(JDK17, Fragments.FeatureCaseNull, DiagKind.NORMAL),
234         PATTERN_SWITCH(JDK17, Fragments.FeaturePatternSwitch, DiagKind.PLURAL),
235         REDUNDANT_STRICTFP(JDK17),
236         ;
237 
238         enum DiagKind {
239             NORMAL,
240             PLURAL;
241         }
242 
243         private final Source minLevel;
244         private final Source maxLevel;
245         private final Fragment optFragment;
246         private final DiagKind optKind;
247 
248         Feature(Source minLevel) {
249             this(minLevel, null, null);
250         }
251 
252         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
253             this(minLevel, MAX, optFragment, optKind);
254         }
255 
256         Feature(Source minLevel, Source maxLevel) {
257             this(minLevel, maxLevel, null, null);
258         }
259 
260         Feature(Source minLevel, Source maxLevel, Fragment optFragment, DiagKind optKind) {
261             this.minLevel = minLevel;
262             this.maxLevel = maxLevel;
263             this.optFragment = optFragment;
264             this.optKind = optKind;
265         }
266 
267         public boolean allowedInSource(Source source) {
268             return source.compareTo(minLevel) >= 0 &&
269                     source.compareTo(maxLevel) <= 0;
270         }
271 
272         public boolean isPlural() {
273             Assert.checkNonNull(optKind);
274             return optKind == DiagKind.PLURAL;
275         }
276 
277         public Fragment nameFragment() {
278             Assert.checkNonNull(optFragment);
279             return optFragment;
280         }
281 
282         public Fragment fragment(String sourceName) {
283             Assert.checkNonNull(optFragment);
284             return optKind == DiagKind.NORMAL ?
285                     Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
286                     Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
287         }
288 
289         public Error error(String sourceName) {
290             Assert.checkNonNull(optFragment);
291             return optKind == DiagKind.NORMAL ?
292                     Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
293                     Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
294         }
295     }
296 
297     public static SourceVersion toSourceVersion(Source source) {
298         return switch(source) {
299         case JDK1_2 -> RELEASE_2;
300         case JDK1_3 -> RELEASE_3;
301         case JDK1_4 -> RELEASE_4;
302         case JDK5   -> RELEASE_5;
303         case JDK6   -> RELEASE_6;
304         case JDK7   -> RELEASE_7;
305         case JDK8   -> RELEASE_8;
306         case JDK9   -> RELEASE_9;
307         case JDK10  -> RELEASE_10;
308         case JDK11  -> RELEASE_11;
309         case JDK12  -> RELEASE_12;
310         case JDK13  -> RELEASE_13;
311         case JDK14  -> RELEASE_14;
312         case JDK15  -> RELEASE_15;
313         case JDK16  -> RELEASE_16;
314         case JDK17  -> RELEASE_17;
315         case JDK18  -> RELEASE_18;
316         default     -> null;
317         };
318     }
319 }