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     private static final Context.Key<Source> sourceKey = new Context.Key<>();
  91 
  92     public static Source instance(Context context) {
  93         Source instance = context.get(sourceKey);
  94         if (instance == null) {
  95             Options options = Options.instance(context);
  96             String sourceString = options.get(SOURCE);
  97             if (sourceString != null) instance = lookup(sourceString);
  98             if (instance == null) instance = DEFAULT;
  99             context.put(sourceKey, instance);
 100         }
 101         return instance;
 102     }
 103 
 104     public final String name;
 105 
 106     private static final Map<String,Source> tab = new HashMap<>();
 107     static {
 108         for (Source s : values()) {
 109             tab.put(s.name, s);
 110         }
 111         tab.put("1.5", JDK5); // Make 5 an alias for 1.5
 112         tab.put("1.6", JDK6); // Make 6 an alias for 1.6
 113         tab.put("1.7", JDK7); // Make 7 an alias for 1.7
 114         tab.put("1.8", JDK8); // Make 8 an alias for 1.8
 115         tab.put("1.9", JDK9); // Make 9 an alias for 1.9
 116         tab.put("1.10", JDK10); // Make 10 an alias for 1.10
 117         // Decline to make 1.11 an alias for 11.
 118     }
 119 
 120     private Source(String name) {
 121         this.name = name;
 122     }
 123 
 124     public static final Source MIN = Source.JDK7;
 125 
 126     private static final Source MAX = values()[values().length - 1];
 127 
 128     public static final Source DEFAULT = MAX;
 129 
 130     public static Source lookup(String name) {
 131         return tab.get(name);
 132     }
 133 
 134     public boolean isSupported() {
 135         return this.compareTo(MIN) >= 0;
 136     }
 137 
 138     public Target requiredTarget() {
 139         if (this.compareTo(JDK12) >= 0) return Target.JDK1_12;
 140         if (this.compareTo(JDK11) >= 0) return Target.JDK1_11;
 141         if (this.compareTo(JDK10) >= 0) return Target.JDK1_10;
 142         if (this.compareTo(JDK9) >= 0) return Target.JDK1_9;
 143         if (this.compareTo(JDK8) >= 0) return Target.JDK1_8;
 144         if (this.compareTo(JDK7) >= 0) return Target.JDK1_7;
 145         if (this.compareTo(JDK6) >= 0) return Target.JDK1_6;
 146         if (this.compareTo(JDK5) >= 0) return Target.JDK1_5;
 147         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
 148         return Target.JDK1_1;
 149     }
 150 
 151     /**
 152      * Models a feature of the Java programming language. Each feature can be associated with a
 153      * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
 154      * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
 155      */
 156     public enum Feature {
 157 
 158         DIAMOND(JDK7, Fragments.FeatureDiamond, DiagKind.NORMAL),
 159         MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL),
 160         EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL),
 161         DEPRECATION_ON_IMPORT(MIN, JDK8),
 162         POLY(JDK8),
 163         LAMBDA(JDK8, Fragments.FeatureLambda, DiagKind.PLURAL),
 164         METHOD_REFERENCES(JDK8, Fragments.FeatureMethodReferences, DiagKind.PLURAL),
 165         DEFAULT_METHODS(JDK8, Fragments.FeatureDefaultMethods, DiagKind.PLURAL),
 166         STATIC_INTERFACE_METHODS(JDK8, Fragments.FeatureStaticIntfMethods, DiagKind.PLURAL),
 167         STATIC_INTERFACE_METHODS_INVOKE(JDK8, Fragments.FeatureStaticIntfMethodInvoke, DiagKind.PLURAL),
 168         STRICT_METHOD_CLASH_CHECK(JDK8),
 169         EFFECTIVELY_FINAL_IN_INNER_CLASSES(JDK8),
 170         TYPE_ANNOTATIONS(JDK8, Fragments.FeatureTypeAnnotations, DiagKind.PLURAL),
 171         ANNOTATIONS_AFTER_TYPE_PARAMS(JDK8, Fragments.FeatureAnnotationsAfterTypeParams, DiagKind.PLURAL),
 172         REPEATED_ANNOTATIONS(JDK8, Fragments.FeatureRepeatableAnnotations, DiagKind.PLURAL),
 173         INTERSECTION_TYPES_IN_CAST(JDK8, Fragments.FeatureIntersectionTypesInCast, DiagKind.PLURAL),
 174         GRAPH_INFERENCE(JDK8),
 175         FUNCTIONAL_INTERFACE_MOST_SPECIFIC(JDK8),
 176         POST_APPLICABILITY_VARARGS_ACCESS_CHECK(JDK8),
 177         MAP_CAPTURES_TO_BOUNDS(MIN, JDK7),
 178         PRIVATE_SAFE_VARARGS(JDK9),
 179         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
 180         UNDERSCORE_IDENTIFIER(MIN, JDK8),
 181         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
 182         LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
 183         VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
 184         IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8),
 185         SWITCH_MULTIPLE_CASE_LABELS(JDK12, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
 186         SWITCH_RULE(JDK12, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
 187         SWITCH_EXPRESSION(JDK12, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
 188         RAW_STRING_LITERALS(JDK12, Fragments.FeatureRawStringLiterals, DiagKind.PLURAL);
 189 
 190         enum DiagKind {
 191             NORMAL,
 192             PLURAL;
 193         }
 194 
 195         private final Source minLevel;
 196         private final Source maxLevel;
 197         private final Fragment optFragment;
 198         private final DiagKind optKind;
 199 
 200         Feature(Source minLevel) {
 201             this(minLevel, null, null);
 202         }
 203 
 204         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
 205             this(minLevel, MAX, optFragment, optKind);
 206         }
 207 
 208         Feature(Source minLevel, Source maxLevel) {
 209             this(minLevel, maxLevel, null, null);
 210         }
 211 
 212         Feature(Source minLevel, Source maxLevel, Fragment optFragment, DiagKind optKind) {
 213             this.minLevel = minLevel;
 214             this.maxLevel = maxLevel;
 215             this.optFragment = optFragment;
 216             this.optKind = optKind;
 217         }
 218 
 219         public boolean allowedInSource(Source source) {
 220             return source.compareTo(minLevel) >= 0 &&
 221                     source.compareTo(maxLevel) <= 0;
 222         }
 223 
 224         public boolean isPlural() {
 225             Assert.checkNonNull(optKind);
 226             return optKind == DiagKind.PLURAL;
 227         }
 228 
 229         public Fragment nameFragment() {
 230             Assert.checkNonNull(optFragment);
 231             return optFragment;
 232         }
 233 
 234         public Fragment fragment(String sourceName) {
 235             Assert.checkNonNull(optFragment);
 236             return optKind == DiagKind.NORMAL ?
 237                     Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
 238                     Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
 239         }
 240 
 241         public Error error(String sourceName) {
 242             Assert.checkNonNull(optFragment);
 243             return optKind == DiagKind.NORMAL ?
 244                     Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
 245                     Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
 246         }
 247     }
 248 
 249     public static SourceVersion toSourceVersion(Source source) {
 250         switch(source) {
 251         case JDK1_2:
 252             return RELEASE_2;
 253         case JDK1_3:
 254             return RELEASE_3;
 255         case JDK1_4:
 256             return RELEASE_4;
 257         case JDK5:
 258             return RELEASE_5;
 259         case JDK6:
 260             return RELEASE_6;
 261         case JDK7:
 262             return RELEASE_7;
 263         case JDK8:
 264             return RELEASE_8;
 265         case JDK9:
 266             return RELEASE_9;
 267         case JDK10:
 268             return RELEASE_10;
 269         case JDK11:
 270             return RELEASE_11;
 271         case JDK12:
 272             return RELEASE_12;
 273         default:
 274             return null;
 275         }
 276     }
 277 }