1 /*
   2  * Copyright (c) 2002, 2011, 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 import javax.lang.model.SourceVersion;
  30 import static javax.lang.model.SourceVersion.*;
  31 
  32 import com.sun.tools.javac.util.*;
  33 import com.sun.tools.javac.jvm.Target;
  34 
  35 import static com.sun.tools.javac.main.OptionName.*;
  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  */
  44 public 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 covers the to be determined language features that will be added in JDK 7. */
  68     JDK1_7("1.7");
  69 
  70     private static final Context.Key<Source> sourceKey
  71         = new Context.Key<Source>();
  72 
  73     public static Source instance(Context context) {
  74         Source instance = context.get(sourceKey);
  75         if (instance == null) {
  76             Options options = Options.instance(context);
  77             String sourceString = options.get(SOURCE);
  78             if (sourceString != null) instance = lookup(sourceString);
  79             if (instance == null) instance = DEFAULT;
  80             context.put(sourceKey, instance);
  81         }
  82         return instance;
  83     }
  84 
  85     public final String name;
  86 
  87     private static Map<String,Source> tab = new HashMap<String,Source>();
  88     static {
  89         for (Source s : values()) {
  90             tab.put(s.name, s);
  91         }
  92         tab.put("5", JDK1_5); // Make 5 an alias for 1.5
  93         tab.put("6", JDK1_6); // Make 6 an alias for 1.6
  94         tab.put("7", JDK1_7); // Make 7 an alias for 1.7
  95     }
  96 
  97     private Source(String name) {
  98         this.name = name;
  99     }
 100 
 101     public static final Source DEFAULT = JDK1_7;
 102 
 103     public static Source lookup(String name) {
 104         return tab.get(name);
 105     }
 106 
 107     public Target requiredTarget() {
 108         if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7;
 109         if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
 110         if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
 111         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
 112         return Target.JDK1_1;
 113     }
 114 
 115     /** Allow encoding errors, giving only warnings. */
 116     public boolean allowEncodingErrors() {
 117         return compareTo(JDK1_6) < 0;
 118     }
 119     public boolean allowAsserts() {
 120         return compareTo(JDK1_4) >= 0;
 121     }
 122     public boolean allowCovariantReturns() {
 123         return compareTo(JDK1_5) >= 0;
 124     }
 125     public boolean allowGenerics() {
 126         return compareTo(JDK1_5) >= 0;
 127     }
 128     public boolean allowDiamond() {
 129         return compareTo(JDK1_7) >= 0;
 130     }
 131     public boolean allowMulticatch() {
 132         return compareTo(JDK1_7) >= 0;
 133     }
 134     public boolean allowImprovedRethrowAnalysis() {
 135         return compareTo(JDK1_7) >= 0;
 136     }
 137     public boolean allowImprovedCatchAnalysis() {
 138         return compareTo(JDK1_7) >= 0;
 139     }
 140     public boolean allowEnums() {
 141         return compareTo(JDK1_5) >= 0;
 142     }
 143     public boolean allowForeach() {
 144         return compareTo(JDK1_5) >= 0;
 145     }
 146     public boolean allowStaticImport() {
 147         return compareTo(JDK1_5) >= 0;
 148     }
 149     public boolean allowBoxing() {
 150         return compareTo(JDK1_5) >= 0;
 151     }
 152     public boolean allowVarargs() {
 153         return compareTo(JDK1_5) >= 0;
 154     }
 155     public boolean allowAnnotations() {
 156         return compareTo(JDK1_5) >= 0;
 157     }
 158     // hex floating-point literals supported?
 159     public boolean allowHexFloats() {
 160         return compareTo(JDK1_5) >= 0;
 161     }
 162     public boolean allowAnonOuterThis() {
 163         return compareTo(JDK1_5) >= 0;
 164     }
 165     public boolean addBridges() {
 166         return compareTo(JDK1_5) >= 0;
 167     }
 168     public boolean enforceMandatoryWarnings() {
 169         return compareTo(JDK1_5) >= 0;
 170     }
 171     public boolean allowTryWithResources() {
 172         return compareTo(JDK1_7) >= 0;
 173     }
 174     public boolean allowTypeAnnotations() {
 175         return compareTo(JDK1_7) >= 0;
 176     }
 177     public boolean allowBinaryLiterals() {
 178         return compareTo(JDK1_7) >= 0;
 179     }
 180     public boolean allowUnderscoresInLiterals() {
 181         return compareTo(JDK1_7) >= 0;
 182     }
 183     public boolean allowStringsInSwitch() {
 184         return compareTo(JDK1_7) >= 0;
 185     }
 186     public boolean allowSimplifiedVarargs() {
 187         return compareTo(JDK1_7) >= 0;
 188     }
 189     public static SourceVersion toSourceVersion(Source source) {
 190         switch(source) {
 191         case JDK1_2:
 192             return RELEASE_2;
 193         case JDK1_3:
 194             return RELEASE_3;
 195         case JDK1_4:
 196             return RELEASE_4;
 197         case JDK1_5:
 198             return RELEASE_5;
 199         case JDK1_6:
 200             return RELEASE_6;
 201         case JDK1_7:
 202             return RELEASE_7;
 203         default:
 204             return null;
 205         }
 206     }
 207 }