001: /*
002: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.code;
027:
028: import com.sun.tools.javac.util.*;
029: import com.sun.tools.javac.jvm.Target;
030: import javax.lang.model.SourceVersion;
031: import static javax.lang.model.SourceVersion.*;
032: import java.util.*;
033:
034: /** The source language version accepted.
035: *
036: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
037: * you write code that depends on this, you do so at your own risk.
038: * This code and its internal interfaces are subject to change or
039: * deletion without notice.</b>
040: */
041: @Version("@(#)Source.java 1.51 07/05/05")
042: public enum Source {
043: /** 1.0 had no inner classes, and so could not pass the JCK. */
044: // public static final Source JDK1_0 = new Source("1.0");
045: /** 1.1 did not have strictfp, and so could not pass the JCK. */
046: // public static final Source JDK1_1 = new Source("1.1");
047: /** 1.2 introduced strictfp. */
048: JDK1_2("1.2"),
049:
050: /** 1.3 is the same language as 1.2. */
051: JDK1_3("1.3"),
052:
053: /** 1.4 introduced assert. */
054: JDK1_4("1.4"),
055:
056: /** 1.5 introduced generics, attributes, foreach, boxing, static import,
057: * covariant return, enums, varargs, et al. */
058: JDK1_5("1.5"),
059:
060: /** 1.6 reports encoding problems as errors instead of warnings. */
061: JDK1_6("1.6"),
062:
063: /** 1.7 covers the to be determined language features that will be added in JDK 7. */
064: JDK1_7("1.7");
065:
066: private static final Context.Key<Source> sourceKey = new Context.Key<Source>();
067:
068: public static Source instance(Context context) {
069: Source instance = context.get(sourceKey);
070: if (instance == null) {
071: Options options = Options.instance(context);
072: String sourceString = options.get("-source");
073: if (sourceString != null)
074: instance = lookup(sourceString);
075: if (instance == null)
076: instance = DEFAULT;
077: context.put(sourceKey, instance);
078: }
079: return instance;
080: }
081:
082: public final String name;
083:
084: private static Map<String, Source> tab = new HashMap<String, Source>();
085: static {
086: for (Source s : values()) {
087: tab.put(s.name, s);
088: }
089: tab.put("5", JDK1_5); // Make 5 an alias for 1.5
090: tab.put("6", JDK1_6); // Make 6 an alias for 1.6
091: tab.put("7", JDK1_7); // Make 7 an alias for 1.7
092: }
093:
094: private Source(String name) {
095: this .name = name;
096: }
097:
098: public static final Source DEFAULT = JDK1_5;
099:
100: public static Source lookup(String name) {
101: return tab.get(name);
102: }
103:
104: public Target requiredTarget() {
105: if (this .compareTo(JDK1_7) >= 0)
106: return Target.JDK1_7;
107: if (this .compareTo(JDK1_6) >= 0)
108: return Target.JDK1_6;
109: if (this .compareTo(JDK1_5) >= 0)
110: return Target.JDK1_5;
111: if (this .compareTo(JDK1_4) >= 0)
112: return Target.JDK1_4;
113: return Target.JDK1_1;
114: }
115:
116: /** Allow encoding errors, giving only warnings. */
117: public boolean allowEncodingErrors() {
118: return compareTo(JDK1_6) < 0;
119: }
120:
121: public boolean allowAsserts() {
122: return compareTo(JDK1_4) >= 0;
123: }
124:
125: public boolean allowCovariantReturns() {
126: return compareTo(JDK1_5) >= 0;
127: }
128:
129: public boolean allowGenerics() {
130: return compareTo(JDK1_5) >= 0;
131: }
132:
133: public boolean allowEnums() {
134: return compareTo(JDK1_5) >= 0;
135: }
136:
137: public boolean allowForeach() {
138: return compareTo(JDK1_5) >= 0;
139: }
140:
141: public boolean allowStaticImport() {
142: return compareTo(JDK1_5) >= 0;
143: }
144:
145: public boolean allowBoxing() {
146: return compareTo(JDK1_5) >= 0;
147: }
148:
149: public boolean allowVarargs() {
150: return compareTo(JDK1_5) >= 0;
151: }
152:
153: public boolean allowAnnotations() {
154: return compareTo(JDK1_5) >= 0;
155: }
156:
157: // hex floating-point literals supported?
158: public boolean allowHexFloats() {
159: return compareTo(JDK1_5) >= 0;
160: }
161:
162: public boolean allowAnonOuterThis() {
163: return compareTo(JDK1_5) >= 0;
164: }
165:
166: public boolean addBridges() {
167: return compareTo(JDK1_5) >= 0;
168: }
169:
170: public boolean enforceMandatoryWarnings() {
171: return compareTo(JDK1_5) >= 0;
172: }
173:
174: public static SourceVersion toSourceVersion(Source source) {
175: switch (source) {
176: case JDK1_2:
177: return RELEASE_2;
178: case JDK1_3:
179: return RELEASE_3;
180: case JDK1_4:
181: return RELEASE_4;
182: case JDK1_5:
183: return RELEASE_5;
184: case JDK1_6:
185: return RELEASE_6;
186: case JDK1_7:
187: return RELEASE_7;
188: default:
189: return null;
190: }
191: }
192: }
|