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    public enum Source {
042        /** 1.0 had no inner classes, and so could not pass the JCK. */
043        // public static final Source JDK1_0 =              new Source("1.0");
044    
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    
048        /** 1.2 introduced strictfp. */
049        JDK1_2("1.2"),
050    
051        /** 1.3 is the same language as 1.2. */
052        JDK1_3("1.3"),
053    
054        /** 1.4 introduced assert. */
055        JDK1_4("1.4"),
056    
057        /** 1.5 introduced generics, attributes, foreach, boxing, static import,
058         *  covariant return, enums, varargs, et al. */
059        JDK1_5("1.5"),
060    
061        /** 1.6 reports encoding problems as errors instead of warnings. */
062        JDK1_6("1.6"),
063    
064        /** 1.7 covers the to be determined language features that will be added in JDK 7. */
065        JDK1_7("1.7");
066    
067        private static final Context.Key<Source> sourceKey
068            = new Context.Key<Source>();
069    
070        public static Source instance(Context context) {
071            Source instance = context.get(sourceKey);
072            if (instance == null) {
073                Options options = Options.instance(context);
074                String sourceString = options.get("-source");
075                if (sourceString != null) instance = lookup(sourceString);
076                if (instance == null) 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) return Target.JDK1_7;
106            if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
107            if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
108            if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
109            return Target.JDK1_1;
110        }
111    
112        /** Allow encoding errors, giving only warnings. */
113        public boolean allowEncodingErrors() {
114            return compareTo(JDK1_6) < 0;
115        }
116        public boolean allowAsserts() {
117            return compareTo(JDK1_4) >= 0;
118        }
119        public boolean allowCovariantReturns() {
120            return compareTo(JDK1_5) >= 0;
121        }
122        public boolean allowGenerics() {
123            return compareTo(JDK1_5) >= 0;
124        }
125        public boolean allowEnums() {
126            return compareTo(JDK1_5) >= 0;
127        }
128        public boolean allowForeach() {
129            return compareTo(JDK1_5) >= 0;
130        }
131        public boolean allowStaticImport() {
132            return compareTo(JDK1_5) >= 0;
133        }
134        public boolean allowBoxing() {
135            return compareTo(JDK1_5) >= 0;
136        }
137        public boolean allowVarargs() {
138            return compareTo(JDK1_5) >= 0;
139        }
140        public boolean allowAnnotations() {
141            return compareTo(JDK1_5) >= 0;
142        }
143        // hex floating-point literals supported?
144        public boolean allowHexFloats() {
145            return compareTo(JDK1_5) >= 0;
146        }
147        public boolean allowAnonOuterThis() {
148            return compareTo(JDK1_5) >= 0;
149        }
150        public boolean addBridges() {
151            return compareTo(JDK1_5) >= 0;
152        }
153        public boolean enforceMandatoryWarnings() {
154            return compareTo(JDK1_5) >= 0;
155        }
156        public static SourceVersion toSourceVersion(Source source) {
157            switch(source) {
158            case JDK1_2:
159                return RELEASE_2;
160            case JDK1_3:
161                return RELEASE_3;
162            case JDK1_4:
163                return RELEASE_4;
164            case JDK1_5:
165                return RELEASE_5;
166            case JDK1_6:
167                return RELEASE_6;
168            case JDK1_7:
169                return RELEASE_7;
170            default:
171                return null;
172            }
173        }
174    }