1 /*
   2  * Copyright (c) 2002, 2015, 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.util.*;
  35 import 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  */
  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 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 covers the to be determined language features that will be added in JDK 9. */
  74     JDK1_9("1.9");
  75 
  76     private static final Context.Key<Source> sourceKey = new Context.Key<>();
  77 
  78     public static Source instance(Context context) {
  79         Source instance = context.get(sourceKey);
  80         if (instance == null) {
  81             Options options = Options.instance(context);
  82             String sourceString = options.get(SOURCE);
  83             if (sourceString != null) instance = lookup(sourceString);
  84             if (instance == null) instance = DEFAULT;
  85             context.put(sourceKey, instance);
  86         }
  87         return instance;
  88     }
  89 
  90     public final String name;
  91 
  92     private static final Map<String,Source> tab = new HashMap<>();
  93     static {
  94         for (Source s : values()) {
  95             tab.put(s.name, s);
  96         }
  97         tab.put("5", JDK1_5); // Make 5 an alias for 1.5
  98         tab.put("6", JDK1_6); // Make 6 an alias for 1.6
  99         tab.put("7", JDK1_7); // Make 7 an alias for 1.7
 100         tab.put("8", JDK1_8); // Make 8 an alias for 1.8
 101         tab.put("9", JDK1_9); // Make 9 an alias for 1.9
 102     }
 103 
 104     private Source(String name) {
 105         this.name = name;
 106     }
 107 
 108     public static final Source MIN = Source.JDK1_6;
 109 
 110     private static final Source MAX = values()[values().length - 1];
 111 
 112     public static final Source DEFAULT = MAX;
 113 
 114     public static Source lookup(String name) {
 115         return tab.get(name);
 116     }
 117 
 118     public Target requiredTarget() {
 119         if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9;
 120         if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8;
 121         if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7;
 122         if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
 123         if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
 124         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
 125         return Target.JDK1_1;
 126     }
 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 allowModules() {
 141         return compareTo(JDK1_9) >= 0;
 142     }
 143     public boolean allowTryWithResources() {
 144         return compareTo(JDK1_7) >= 0;
 145     }
 146     public boolean allowEffectivelyFinalVariablesInTryWithResources() {
 147         return compareTo(JDK1_9) >= 0;
 148     }
 149     public boolean allowBinaryLiterals() {
 150         return compareTo(JDK1_7) >= 0;
 151     }
 152     public boolean allowUnderscoresInLiterals() {
 153         return compareTo(JDK1_7) >= 0;
 154     }
 155     public boolean allowStringsInSwitch() {
 156         return compareTo(JDK1_7) >= 0;
 157     }
 158     public boolean allowDeprecationOnImport() {
 159         return compareTo(JDK1_9) < 0;
 160     }
 161     public boolean allowSimplifiedVarargs() {
 162         return compareTo(JDK1_7) >= 0;
 163     }
 164     public boolean allowObjectToPrimitiveCast() {
 165         return compareTo(JDK1_7) >= 0;
 166     }
 167     public boolean enforceThisDotInit() {
 168         return compareTo(JDK1_7) >= 0;
 169     }
 170     public boolean allowPoly() {
 171         return compareTo(JDK1_8) >= 0;
 172     }
 173     public boolean allowLambda() {
 174         return compareTo(JDK1_8) >= 0;
 175     }
 176     public boolean allowMethodReferences() {
 177         return compareTo(JDK1_8) >= 0;
 178     }
 179     public boolean allowDefaultMethods() {
 180         return compareTo(JDK1_8) >= 0;
 181     }
 182     public boolean allowStaticInterfaceMethods() {
 183         return compareTo(JDK1_8) >= 0;
 184     }
 185     public boolean allowStrictMethodClashCheck() {
 186         return compareTo(JDK1_8) >= 0;
 187     }
 188     public boolean allowEffectivelyFinalInInnerClasses() {
 189         return compareTo(JDK1_8) >= 0;
 190     }
 191     public boolean allowTypeAnnotations() {
 192         return compareTo(JDK1_8) >= 0;
 193     }
 194     public boolean allowAnnotationsAfterTypeParams() {
 195         return compareTo(JDK1_8) >= 0;
 196     }
 197     public boolean allowRepeatedAnnotations() {
 198         return compareTo(JDK1_8) >= 0;
 199     }
 200     public boolean allowIntersectionTypesInCast() {
 201         return compareTo(JDK1_8) >= 0;
 202     }
 203     public boolean allowGraphInference() {
 204         return compareTo(JDK1_8) >= 0;
 205     }
 206     public boolean allowFunctionalInterfaceMostSpecific() {
 207         return compareTo(JDK1_8) >= 0;
 208     }
 209     public boolean allowPostApplicabilityVarargsAccessCheck() {
 210         return compareTo(JDK1_8) >= 0;
 211     }
 212     public boolean mapCapturesToBounds() {
 213         return compareTo(JDK1_8) < 0;
 214     }
 215     public boolean allowPrivateSafeVarargs() {
 216         return compareTo(JDK1_9) >= 0;
 217     }
 218     public boolean allowDiamondWithAnonymousClassCreation() {
 219         return compareTo(JDK1_9) >= 0;
 220     }
 221     public boolean allowUnderscoreIdentifier() {
 222         return compareTo(JDK1_8) <= 0;
 223     }
 224     public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; }
 225     public static SourceVersion toSourceVersion(Source source) {
 226         switch(source) {
 227         case JDK1_2:
 228             return RELEASE_2;
 229         case JDK1_3:
 230             return RELEASE_3;
 231         case JDK1_4:
 232             return RELEASE_4;
 233         case JDK1_5:
 234             return RELEASE_5;
 235         case JDK1_6:
 236             return RELEASE_6;
 237         case JDK1_7:
 238             return RELEASE_7;
 239         case JDK1_8:
 240             return RELEASE_8;
 241         case JDK1_9:
 242             return RELEASE_9;
 243         default:
 244             return null;
 245         }
 246     }
 247 }