1/*
2 * Copyright (c) 2002, 2017, 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
26package com.sun.tools.javac.code;
27
28import java.util.*;
29
30import javax.lang.model.SourceVersion;
31import static javax.lang.model.SourceVersion.*;
32
33import com.sun.tools.javac.jvm.Target;
34import com.sun.tools.javac.util.*;
35import static com.sun.tools.javac.main.Option.*;
36
37/** The source language version accepted.
38 *
39 *  <p><b>This is NOT part of any supported API.
40 *  If you write code that depends on this, you do so at your own risk.
41 *  This code and its internal interfaces are subject to change or
42 *  deletion without notice.</b>
43 */
44public enum Source {
45    /** 1.0 had no inner classes, and so could not pass the JCK. */
46    // public static final Source JDK1_0 =              new Source("1.0");
47
48    /** 1.1 did not have strictfp, and so could not pass the JCK. */
49    // public static final Source JDK1_1 =              new Source("1.1");
50
51    /** 1.2 introduced strictfp. */
52    JDK1_2("1.2"),
53
54    /** 1.3 is the same language as 1.2. */
55    JDK1_3("1.3"),
56
57    /** 1.4 introduced assert. */
58    JDK1_4("1.4"),
59
60    /** 1.5 introduced generics, attributes, foreach, boxing, static import,
61     *  covariant return, enums, varargs, et al. */
62    JDK1_5("1.5"),
63
64    /** 1.6 reports encoding problems as errors instead of warnings. */
65    JDK1_6("1.6"),
66
67    /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */
68    JDK1_7("1.7"),
69
70    /** 1.8 lambda expressions and default methods. */
71    JDK1_8("1.8"),
72
73    /** 1.9 modularity. */
74    JDK1_9("1.9"),
75
76    /** 1.10 covers the to be determined language features that will be added in JDK 10. */
77    JDK1_10("1.10");
78
79    private static final Context.Key<Source> sourceKey = new Context.Key<>();
80
81    public static Source instance(Context context) {
82        Source instance = context.get(sourceKey);
83        if (instance == null) {
84            Options options = Options.instance(context);
85            String sourceString = options.get(SOURCE);
86            if (sourceString != null) instance = lookup(sourceString);
87            if (instance == null) instance = DEFAULT;
88            context.put(sourceKey, instance);
89        }
90        return instance;
91    }
92
93    public final String name;
94
95    private static final Map<String,Source> tab = new HashMap<>();
96    static {
97        for (Source s : values()) {
98            tab.put(s.name, s);
99        }
100        tab.put("5", JDK1_5); // Make 5 an alias for 1.5
101        tab.put("6", JDK1_6); // Make 6 an alias for 1.6
102        tab.put("7", JDK1_7); // Make 7 an alias for 1.7
103        tab.put("8", JDK1_8); // Make 8 an alias for 1.8
104        tab.put("9", JDK1_9); // Make 9 an alias for 1.9
105        tab.put("10", JDK1_10); // Make 10 an alias for 1.10
106    }
107
108    private Source(String name) {
109        this.name = name;
110    }
111
112    public static final Source MIN = Source.JDK1_6;
113
114    private static final Source MAX = values()[values().length - 1];
115
116    public static final Source DEFAULT = MAX;
117
118    public static Source lookup(String name) {
119        return tab.get(name);
120    }
121
122    public Target requiredTarget() {
123        if (this.compareTo(JDK1_10) >= 0) return Target.JDK1_10;
124        if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9;
125        if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8;
126        if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7;
127        if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
128        if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
129        if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
130        return Target.JDK1_1;
131    }
132
133    public boolean allowDiamond() {
134        return compareTo(JDK1_7) >= 0;
135    }
136    public boolean allowMulticatch() {
137        return compareTo(JDK1_7) >= 0;
138    }
139    public boolean allowImprovedRethrowAnalysis() {
140        return compareTo(JDK1_7) >= 0;
141    }
142    public boolean allowImprovedCatchAnalysis() {
143        return compareTo(JDK1_7) >= 0;
144    }
145    public boolean allowModules() {
146        return compareTo(JDK1_9) >= 0;
147    }
148    public boolean allowTryWithResources() {
149        return compareTo(JDK1_7) >= 0;
150    }
151    public boolean allowEffectivelyFinalVariablesInTryWithResources() {
152        return compareTo(JDK1_9) >= 0;
153    }
154    public boolean allowBinaryLiterals() {
155        return compareTo(JDK1_7) >= 0;
156    }
157    public boolean allowUnderscoresInLiterals() {
158        return compareTo(JDK1_7) >= 0;
159    }
160    public boolean allowStringsInSwitch() {
161        return compareTo(JDK1_7) >= 0;
162    }
163    public boolean allowDeprecationOnImport() {
164        return compareTo(JDK1_9) < 0;
165    }
166    public boolean allowSimplifiedVarargs() {
167        return compareTo(JDK1_7) >= 0;
168    }
169    public boolean allowObjectToPrimitiveCast() {
170        return compareTo(JDK1_7) >= 0;
171    }
172    public boolean enforceThisDotInit() {
173        return compareTo(JDK1_7) >= 0;
174    }
175    public boolean allowPoly() {
176        return compareTo(JDK1_8) >= 0;
177    }
178    public boolean allowLambda() {
179        return compareTo(JDK1_8) >= 0;
180    }
181    public boolean allowMethodReferences() {
182        return compareTo(JDK1_8) >= 0;
183    }
184    public boolean allowDefaultMethods() {
185        return compareTo(JDK1_8) >= 0;
186    }
187    public boolean allowStaticInterfaceMethods() {
188        return compareTo(JDK1_8) >= 0;
189    }
190    public boolean allowStrictMethodClashCheck() {
191        return compareTo(JDK1_8) >= 0;
192    }
193    public boolean allowEffectivelyFinalInInnerClasses() {
194        return compareTo(JDK1_8) >= 0;
195    }
196    public boolean allowTypeAnnotations() {
197        return compareTo(JDK1_8) >= 0;
198    }
199    public boolean allowAnnotationsAfterTypeParams() {
200        return compareTo(JDK1_8) >= 0;
201    }
202    public boolean allowRepeatedAnnotations() {
203        return compareTo(JDK1_8) >= 0;
204    }
205    public boolean allowIntersectionTypesInCast() {
206        return compareTo(JDK1_8) >= 0;
207    }
208    public boolean allowGraphInference() {
209        return compareTo(JDK1_8) >= 0;
210    }
211    public boolean allowFunctionalInterfaceMostSpecific() {
212        return compareTo(JDK1_8) >= 0;
213    }
214    public boolean allowPostApplicabilityVarargsAccessCheck() {
215        return compareTo(JDK1_8) >= 0;
216    }
217    public boolean mapCapturesToBounds() {
218        return compareTo(JDK1_8) < 0;
219    }
220    public boolean allowPrivateSafeVarargs() {
221        return compareTo(JDK1_9) >= 0;
222    }
223    public boolean allowDiamondWithAnonymousClassCreation() {
224        return compareTo(JDK1_9) >= 0;
225    }
226    public boolean allowUnderscoreIdentifier() {
227        return compareTo(JDK1_8) <= 0;
228    }
229    public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; }
230    public static SourceVersion toSourceVersion(Source source) {
231        switch(source) {
232        case JDK1_2:
233            return RELEASE_2;
234        case JDK1_3:
235            return RELEASE_3;
236        case JDK1_4:
237            return RELEASE_4;
238        case JDK1_5:
239            return RELEASE_5;
240        case JDK1_6:
241            return RELEASE_6;
242        case JDK1_7:
243            return RELEASE_7;
244        case JDK1_8:
245            return RELEASE_8;
246        case JDK1_9:
247            return RELEASE_9;
248        case JDK1_10:
249            return RELEASE_10;
250        default:
251            return null;
252        }
253    }
254}
255