1 /*
   2  * Copyright (c) 2002, 2018, 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.resources.CompilerProperties.Errors;
  35 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  36 import com.sun.tools.javac.util.*;
  37 import com.sun.tools.javac.util.JCDiagnostic.Error;
  38 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
  39 
  40 import static com.sun.tools.javac.main.Option.*;
  41 
  42 /** The source language version accepted.
  43  *
  44  *  <p><b>This is NOT part of any supported API.
  45  *  If you write code that depends on this, you do so at your own risk.
  46  *  This code and its internal interfaces are subject to change or
  47  *  deletion without notice.</b>
  48  */
  49 public enum Source {
  50     /** 1.0 had no inner classes, and so could not pass the JCK. */
  51     // public static final Source JDK1_0 =              new Source("1.0");
  52 
  53     /** 1.1 did not have strictfp, and so could not pass the JCK. */
  54     // public static final Source JDK1_1 =              new Source("1.1");
  55 
  56     /** 1.2 introduced strictfp. */
  57     JDK1_2("1.2"),
  58 
  59     /** 1.3 is the same language as 1.2. */
  60     JDK1_3("1.3"),
  61 
  62     /** 1.4 introduced assert. */
  63     JDK1_4("1.4"),
  64 
  65     /** 1.5 introduced generics, attributes, foreach, boxing, static import,
  66      *  covariant return, enums, varargs, et al. */
  67     JDK5("5"),
  68 
  69     /** 1.6 reports encoding problems as errors instead of warnings. */
  70     JDK6("6"),
  71 
  72     /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */
  73     JDK7("7"),
  74 
  75     /** 1.8 lambda expressions and default methods. */
  76     JDK8("8"),
  77 
  78     /** 1.9 modularity. */
  79     JDK9("9"),
  80 
  81     /** 1.10 local-variable type inference (var). */
  82     JDK10("10"),
  83 
  84     /** 1.11 local-variable syntax for lambda parameters */
  85     JDK11("11"),
  86 
  87     /** 12 covers the to be determined language features that will be added in JDK 12. */
  88     JDK12("12"),
  89 
  90     /** 13 covers the to be determined language features that will be added in JDK 13. */
  91     JDK13("13");
  92 
  93     private static final Context.Key<Source> sourceKey = new Context.Key<>();
  94 
  95     public static Source instance(Context context) {
  96         Source instance = context.get(sourceKey);
  97         if (instance == null) {
  98             Options options = Options.instance(context);
  99             String sourceString = options.get(SOURCE);
 100             if (sourceString != null) instance = lookup(sourceString);
 101             if (instance == null) instance = DEFAULT;
 102             context.put(sourceKey, instance);
 103         }
 104         return instance;
 105     }
 106 
 107     public final String name;
 108 
 109     private static final Map<String,Source> tab = new HashMap<>();
 110     static {
 111         for (Source s : values()) {
 112             tab.put(s.name, s);
 113         }
 114         tab.put("1.5", JDK5); // Make 5 an alias for 1.5
 115         tab.put("1.6", JDK6); // Make 6 an alias for 1.6
 116         tab.put("1.7", JDK7); // Make 7 an alias for 1.7
 117         tab.put("1.8", JDK8); // Make 8 an alias for 1.8
 118         tab.put("1.9", JDK9); // Make 9 an alias for 1.9
 119         tab.put("1.10", JDK10); // Make 10 an alias for 1.10
 120         // Decline to make 1.11 an alias for 11.
 121     }
 122 
 123     private Source(String name) {
 124         this.name = name;
 125     }
 126 
 127     public static final Source MIN = Source.JDK7;
 128 
 129     private static final Source MAX = values()[values().length - 1];
 130 
 131     public static final Source DEFAULT = MAX;
 132 
 133     public static Source lookup(String name) {
 134         return tab.get(name);
 135     }
 136 
 137     public boolean isSupported() {
 138         return this.compareTo(MIN) >= 0;
 139     }
 140 
 141     public Target requiredTarget() {
 142         if (this.compareTo(JDK13) >= 0) return Target.JDK1_13;
 143         if (this.compareTo(JDK12) >= 0) return Target.JDK1_12;
 144         if (this.compareTo(JDK11) >= 0) return Target.JDK1_11;
 145         if (this.compareTo(JDK10) >= 0) return Target.JDK1_10;
 146         if (this.compareTo(JDK9) >= 0) return Target.JDK1_9;
 147         if (this.compareTo(JDK8) >= 0) return Target.JDK1_8;
 148         if (this.compareTo(JDK7) >= 0) return Target.JDK1_7;
 149         if (this.compareTo(JDK6) >= 0) return Target.JDK1_6;
 150         if (this.compareTo(JDK5) >= 0) return Target.JDK1_5;
 151         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
 152         return Target.JDK1_1;
 153     }
 154 
 155     /**
 156      * Models a feature of the Java programming language. Each feature can be associated with a
 157      * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
 158      * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
 159      */
 160     public enum Feature {
 161 
 162         DIAMOND(JDK7, Fragments.FeatureDiamond, DiagKind.NORMAL),
 163         MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL),
 164         EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL),
 165         DEPRECATION_ON_IMPORT(MIN, JDK8),
 166         POLY(JDK8),
 167         LAMBDA(JDK8, Fragments.FeatureLambda, DiagKind.PLURAL),
 168         METHOD_REFERENCES(JDK8, Fragments.FeatureMethodReferences, DiagKind.PLURAL),
 169         DEFAULT_METHODS(JDK8, Fragments.FeatureDefaultMethods, DiagKind.PLURAL),
 170         STATIC_INTERFACE_METHODS(JDK8, Fragments.FeatureStaticIntfMethods, DiagKind.PLURAL),
 171         STATIC_INTERFACE_METHODS_INVOKE(JDK8, Fragments.FeatureStaticIntfMethodInvoke, DiagKind.PLURAL),
 172         STRICT_METHOD_CLASH_CHECK(JDK8),
 173         EFFECTIVELY_FINAL_IN_INNER_CLASSES(JDK8),
 174         TYPE_ANNOTATIONS(JDK8, Fragments.FeatureTypeAnnotations, DiagKind.PLURAL),
 175         ANNOTATIONS_AFTER_TYPE_PARAMS(JDK8, Fragments.FeatureAnnotationsAfterTypeParams, DiagKind.PLURAL),
 176         REPEATED_ANNOTATIONS(JDK8, Fragments.FeatureRepeatableAnnotations, DiagKind.PLURAL),
 177         INTERSECTION_TYPES_IN_CAST(JDK8, Fragments.FeatureIntersectionTypesInCast, DiagKind.PLURAL),
 178         GRAPH_INFERENCE(JDK8),
 179         FUNCTIONAL_INTERFACE_MOST_SPECIFIC(JDK8),
 180         POST_APPLICABILITY_VARARGS_ACCESS_CHECK(JDK8),
 181         MAP_CAPTURES_TO_BOUNDS(MIN, JDK7),
 182         PRIVATE_SAFE_VARARGS(JDK9),
 183         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
 184         UNDERSCORE_IDENTIFIER(MIN, JDK8),
 185         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
 186         LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
 187         VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
 188         IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8),
 189         SWITCH_MULTIPLE_CASE_LABELS(JDK13, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
 190         SWITCH_RULE(JDK13, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
 191         SWITCH_EXPRESSION(JDK13, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
 192         CONDY_FOR_LAMBDA(JDK13),
 193         CONSTABLES(JDK13);
 194 
 195         enum DiagKind {
 196             NORMAL,
 197             PLURAL;
 198         }
 199 
 200         private final Source minLevel;
 201         private final Source maxLevel;
 202         private final Fragment optFragment;
 203         private final DiagKind optKind;
 204 
 205         Feature(Source minLevel) {
 206             this(minLevel, null, null);
 207         }
 208 
 209         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
 210             this(minLevel, MAX, optFragment, optKind);
 211         }
 212 
 213         Feature(Source minLevel, Source maxLevel) {
 214             this(minLevel, maxLevel, null, null);
 215         }
 216 
 217         Feature(Source minLevel, Source maxLevel, Fragment optFragment, DiagKind optKind) {
 218             this.minLevel = minLevel;
 219             this.maxLevel = maxLevel;
 220             this.optFragment = optFragment;
 221             this.optKind = optKind;
 222         }
 223 
 224         public boolean allowedInSource(Source source) {
 225             return source.compareTo(minLevel) >= 0 &&
 226                     source.compareTo(maxLevel) <= 0;
 227         }
 228 
 229         public boolean isPlural() {
 230             Assert.checkNonNull(optKind);
 231             return optKind == DiagKind.PLURAL;
 232         }
 233 
 234         public Fragment nameFragment() {
 235             Assert.checkNonNull(optFragment);
 236             return optFragment;
 237         }
 238 
 239         public Fragment fragment(String sourceName) {
 240             Assert.checkNonNull(optFragment);
 241             return optKind == DiagKind.NORMAL ?
 242                     Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
 243                     Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
 244         }
 245 
 246         public Error error(String sourceName) {
 247             Assert.checkNonNull(optFragment);
 248             return optKind == DiagKind.NORMAL ?
 249                     Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
 250                     Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
 251         }
 252     }
 253 
 254     public static SourceVersion toSourceVersion(Source source) {
 255         switch(source) {
 256         case JDK1_2:
 257             return RELEASE_2;
 258         case JDK1_3:
 259             return RELEASE_3;
 260         case JDK1_4:
 261             return RELEASE_4;
 262         case JDK5:
 263             return RELEASE_5;
 264         case JDK6:
 265             return RELEASE_6;
 266         case JDK7:
 267             return RELEASE_7;
 268         case JDK8:
 269             return RELEASE_8;
 270         case JDK9:
 271             return RELEASE_9;
 272         case JDK10:
 273             return RELEASE_10;
 274         case JDK11:
 275             return RELEASE_11;
 276         case JDK12:
 277             return RELEASE_12;
 278         case JDK13:
 279             return RELEASE_13;
 280         default:
 281             return null;
 282         }
 283     }
 284 }