src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java
changeset 48054 702043a4cdeb
parent 47268 48ec75306997
child 48343 d4329843abf4
child 48776 107413b070b9
equal deleted inserted replaced
48053:6dcbdc9f99fc 48054:702043a4cdeb
    29 
    29 
    30 import javax.lang.model.SourceVersion;
    30 import javax.lang.model.SourceVersion;
    31 import static javax.lang.model.SourceVersion.*;
    31 import static javax.lang.model.SourceVersion.*;
    32 
    32 
    33 import com.sun.tools.javac.jvm.Target;
    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;
    34 import com.sun.tools.javac.util.*;
    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 
    35 import static com.sun.tools.javac.main.Option.*;
    40 import static com.sun.tools.javac.main.Option.*;
    36 
    41 
    37 /** The source language version accepted.
    42 /** The source language version accepted.
    38  *
    43  *
    39  *  <p><b>This is NOT part of any supported API.
    44  *  <p><b>This is NOT part of any supported API.
    57     /** 1.4 introduced assert. */
    62     /** 1.4 introduced assert. */
    58     JDK1_4("1.4"),
    63     JDK1_4("1.4"),
    59 
    64 
    60     /** 1.5 introduced generics, attributes, foreach, boxing, static import,
    65     /** 1.5 introduced generics, attributes, foreach, boxing, static import,
    61      *  covariant return, enums, varargs, et al. */
    66      *  covariant return, enums, varargs, et al. */
    62     JDK1_5("1.5"),
    67     JDK5("5"),
    63 
    68 
    64     /** 1.6 reports encoding problems as errors instead of warnings. */
    69     /** 1.6 reports encoding problems as errors instead of warnings. */
    65     JDK1_6("1.6"),
    70     JDK6("6"),
    66 
    71 
    67     /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */
    72     /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */
    68     JDK1_7("1.7"),
    73     JDK7("7"),
    69 
    74 
    70     /** 1.8 lambda expressions and default methods. */
    75     /** 1.8 lambda expressions and default methods. */
    71     JDK1_8("1.8"),
    76     JDK8("8"),
    72 
    77 
    73     /** 1.9 modularity. */
    78     /** 1.9 modularity. */
    74     JDK1_9("1.9"),
    79     JDK9("9"),
    75 
    80 
    76     /** 1.10 covers the to be determined language features that will be added in JDK 10. */
    81     /** 1.10 covers the to be determined language features that will be added in JDK 10. */
    77     JDK1_10("1.10");
    82     JDK10("10");
    78 
    83 
    79     private static final Context.Key<Source> sourceKey = new Context.Key<>();
    84     private static final Context.Key<Source> sourceKey = new Context.Key<>();
    80 
    85 
    81     public static Source instance(Context context) {
    86     public static Source instance(Context context) {
    82         Source instance = context.get(sourceKey);
    87         Source instance = context.get(sourceKey);
    95     private static final Map<String,Source> tab = new HashMap<>();
   100     private static final Map<String,Source> tab = new HashMap<>();
    96     static {
   101     static {
    97         for (Source s : values()) {
   102         for (Source s : values()) {
    98             tab.put(s.name, s);
   103             tab.put(s.name, s);
    99         }
   104         }
   100         tab.put("5", JDK1_5); // Make 5 an alias for 1.5
   105         tab.put("1.5", JDK5); // Make 5 an alias for 1.5
   101         tab.put("6", JDK1_6); // Make 6 an alias for 1.6
   106         tab.put("1.6", JDK6); // Make 6 an alias for 1.6
   102         tab.put("7", JDK1_7); // Make 7 an alias for 1.7
   107         tab.put("1.7", JDK7); // Make 7 an alias for 1.7
   103         tab.put("8", JDK1_8); // Make 8 an alias for 1.8
   108         tab.put("1.8", JDK8); // Make 8 an alias for 1.8
   104         tab.put("9", JDK1_9); // Make 9 an alias for 1.9
   109         tab.put("1.9", JDK9); // Make 9 an alias for 1.9
   105         tab.put("10", JDK1_10); // Make 10 an alias for 1.10
   110         tab.put("1.10", JDK10); // Make 10 an alias for 1.10
   106     }
   111     }
   107 
   112 
   108     private Source(String name) {
   113     private Source(String name) {
   109         this.name = name;
   114         this.name = name;
   110     }
   115     }
   111 
   116 
   112     public static final Source MIN = Source.JDK1_6;
   117     public static final Source MIN = Source.JDK6;
   113 
   118 
   114     private static final Source MAX = values()[values().length - 1];
   119     private static final Source MAX = values()[values().length - 1];
   115 
   120 
   116     public static final Source DEFAULT = MAX;
   121     public static final Source DEFAULT = MAX;
   117 
   122 
   118     public static Source lookup(String name) {
   123     public static Source lookup(String name) {
   119         return tab.get(name);
   124         return tab.get(name);
   120     }
   125     }
   121 
   126 
   122     public Target requiredTarget() {
   127     public Target requiredTarget() {
   123         if (this.compareTo(JDK1_10) >= 0) return Target.JDK1_10;
   128         if (this.compareTo(JDK10) >= 0) return Target.JDK1_10;
   124         if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9;
   129         if (this.compareTo(JDK9) >= 0) return Target.JDK1_9;
   125         if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8;
   130         if (this.compareTo(JDK8) >= 0) return Target.JDK1_8;
   126         if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7;
   131         if (this.compareTo(JDK7) >= 0) return Target.JDK1_7;
   127         if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6;
   132         if (this.compareTo(JDK6) >= 0) return Target.JDK1_6;
   128         if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5;
   133         if (this.compareTo(JDK5) >= 0) return Target.JDK1_5;
   129         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
   134         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
   130         return Target.JDK1_1;
   135         return Target.JDK1_1;
   131     }
   136     }
   132 
   137 
   133     public boolean allowDiamond() {
   138     /**
   134         return compareTo(JDK1_7) >= 0;
   139      * Models a feature of the Java programming language. Each feature can be associated with a
   135     }
   140      * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
   136     public boolean allowMulticatch() {
   141      * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
   137         return compareTo(JDK1_7) >= 0;
   142      */
   138     }
   143     public enum Feature {
   139     public boolean allowImprovedRethrowAnalysis() {
   144 
   140         return compareTo(JDK1_7) >= 0;
   145         DIAMOND(JDK7, Fragments.FeatureDiamond, DiagKind.NORMAL),
   141     }
   146         MULTICATCH(JDK7, Fragments.FeatureMulticatch, DiagKind.PLURAL),
   142     public boolean allowImprovedCatchAnalysis() {
   147         IMPROVED_RETHROW_ANALYSIS(JDK7),
   143         return compareTo(JDK1_7) >= 0;
   148         IMPROVED_CATCH_ANALYSIS(JDK7),
   144     }
   149         MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL),
   145     public boolean allowModules() {
   150         TRY_WITH_RESOURCES(JDK7, Fragments.FeatureTryWithResources, DiagKind.NORMAL),
   146         return compareTo(JDK1_9) >= 0;
   151         EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL),
   147     }
   152         BINARY_LITERALS(JDK7, Fragments.FeatureBinaryLit, DiagKind.PLURAL),
   148     public boolean allowTryWithResources() {
   153         UNDERSCORES_IN_LITERALS(JDK7, Fragments.FeatureUnderscoreLit, DiagKind.PLURAL),
   149         return compareTo(JDK1_7) >= 0;
   154         STRINGS_IN_SWITCH(JDK7, Fragments.FeatureStringSwitch, DiagKind.PLURAL),
   150     }
   155         DEPRECATION_ON_IMPORT(MIN, JDK9),
   151     public boolean allowEffectivelyFinalVariablesInTryWithResources() {
   156         SIMPLIFIED_VARARGS(JDK7),
   152         return compareTo(JDK1_9) >= 0;
   157         OBJECT_TO_PRIMITIVE_CAST(JDK7),
   153     }
   158         ENFORCE_THIS_DOT_INIT(JDK7),
   154     public boolean allowBinaryLiterals() {
   159         POLY(JDK8),
   155         return compareTo(JDK1_7) >= 0;
   160         LAMBDA(JDK8, Fragments.FeatureLambda, DiagKind.PLURAL),
   156     }
   161         METHOD_REFERENCES(JDK8, Fragments.FeatureMethodReferences, DiagKind.PLURAL),
   157     public boolean allowUnderscoresInLiterals() {
   162         DEFAULT_METHODS(JDK8, Fragments.FeatureDefaultMethods, DiagKind.PLURAL),
   158         return compareTo(JDK1_7) >= 0;
   163         STATIC_INTERFACE_METHODS(JDK8, Fragments.FeatureStaticIntfMethods, DiagKind.PLURAL),
   159     }
   164         STATIC_INTERFACE_METHODS_INVOKE(JDK8, Fragments.FeatureStaticIntfMethodInvoke, DiagKind.PLURAL),
   160     public boolean allowStringsInSwitch() {
   165         STRICT_METHOD_CLASH_CHECK(JDK8),
   161         return compareTo(JDK1_7) >= 0;
   166         EFFECTIVELY_FINAL_IN_INNER_CLASSES(JDK8),
   162     }
   167         TYPE_ANNOTATIONS(JDK8, Fragments.FeatureTypeAnnotations, DiagKind.PLURAL),
   163     public boolean allowDeprecationOnImport() {
   168         ANNOTATIONS_AFTER_TYPE_PARAMS(JDK8, Fragments.FeatureAnnotationsAfterTypeParams, DiagKind.PLURAL),
   164         return compareTo(JDK1_9) < 0;
   169         REPEATED_ANNOTATIONS(JDK8, Fragments.FeatureRepeatableAnnotations, DiagKind.PLURAL),
   165     }
   170         INTERSECTION_TYPES_IN_CAST(JDK8, Fragments.FeatureIntersectionTypesInCast, DiagKind.PLURAL),
   166     public boolean allowSimplifiedVarargs() {
   171         GRAPH_INFERENCE(JDK8),
   167         return compareTo(JDK1_7) >= 0;
   172         FUNCTIONAL_INTERFACE_MOST_SPECIFIC(JDK8),
   168     }
   173         POST_APPLICABILITY_VARARGS_ACCESS_CHECK(JDK8),
   169     public boolean allowObjectToPrimitiveCast() {
   174         MAP_CAPTURES_TO_BOUNDS(MIN, JDK7),
   170         return compareTo(JDK1_7) >= 0;
   175         PRIVATE_SAFE_VARARGS(JDK9),
   171     }
   176         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
   172     public boolean enforceThisDotInit() {
   177         UNDERSCORE_IDENTIFIER(MIN, JDK8),
   173         return compareTo(JDK1_7) >= 0;
   178         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
   174     }
   179         LOCAL_VARIABLE_TYPE_INFERENCE(JDK10);
   175     public boolean allowPoly() {
   180 
   176         return compareTo(JDK1_8) >= 0;
   181         enum DiagKind {
   177     }
   182             NORMAL,
   178     public boolean allowLambda() {
   183             PLURAL;
   179         return compareTo(JDK1_8) >= 0;
   184         }
   180     }
   185 
   181     public boolean allowMethodReferences() {
   186         private final Source minLevel;
   182         return compareTo(JDK1_8) >= 0;
   187         private final Source maxLevel;
   183     }
   188         private final Fragment optFragment;
   184     public boolean allowDefaultMethods() {
   189         private final DiagKind optKind;
   185         return compareTo(JDK1_8) >= 0;
   190 
   186     }
   191         Feature(Source minLevel) {
   187     public boolean allowStaticInterfaceMethods() {
   192             this(minLevel, null, null);
   188         return compareTo(JDK1_8) >= 0;
   193         }
   189     }
   194 
   190     public boolean allowStrictMethodClashCheck() {
   195         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
   191         return compareTo(JDK1_8) >= 0;
   196             this(minLevel, MAX, optFragment, optKind);
   192     }
   197         }
   193     public boolean allowEffectivelyFinalInInnerClasses() {
   198 
   194         return compareTo(JDK1_8) >= 0;
   199         Feature(Source minLevel, Source maxLevel) {
   195     }
   200             this(minLevel, maxLevel, null, null);
   196     public boolean allowTypeAnnotations() {
   201         }
   197         return compareTo(JDK1_8) >= 0;
   202 
   198     }
   203         Feature(Source minLevel, Source maxLevel, Fragment optFragment, DiagKind optKind) {
   199     public boolean allowAnnotationsAfterTypeParams() {
   204             this.minLevel = minLevel;
   200         return compareTo(JDK1_8) >= 0;
   205             this.maxLevel = maxLevel;
   201     }
   206             this.optFragment = optFragment;
   202     public boolean allowRepeatedAnnotations() {
   207             this.optKind = optKind;
   203         return compareTo(JDK1_8) >= 0;
   208         }
   204     }
   209 
   205     public boolean allowIntersectionTypesInCast() {
   210         public boolean allowedInSource(Source source) {
   206         return compareTo(JDK1_8) >= 0;
   211             return source.compareTo(minLevel) >= 0 &&
   207     }
   212                     source.compareTo(maxLevel) <= 0;
   208     public boolean allowGraphInference() {
   213         }
   209         return compareTo(JDK1_8) >= 0;
   214 
   210     }
   215         public Fragment fragment(String sourceName) {
   211     public boolean allowFunctionalInterfaceMostSpecific() {
   216             Assert.checkNonNull(optFragment);
   212         return compareTo(JDK1_8) >= 0;
   217             return optKind == DiagKind.NORMAL ?
   213     }
   218                     Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
   214     public boolean allowPostApplicabilityVarargsAccessCheck() {
   219                     Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
   215         return compareTo(JDK1_8) >= 0;
   220         }
   216     }
   221 
   217     public boolean mapCapturesToBounds() {
   222         public Error error(String sourceName) {
   218         return compareTo(JDK1_8) < 0;
   223             Assert.checkNonNull(optFragment);
   219     }
   224             return optKind == DiagKind.NORMAL ?
   220     public boolean allowPrivateSafeVarargs() {
   225                     Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) :
   221         return compareTo(JDK1_9) >= 0;
   226                     Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name);
   222     }
   227         }
   223     public boolean allowDiamondWithAnonymousClassCreation() {
   228     }
   224         return compareTo(JDK1_9) >= 0;
   229 
   225     }
       
   226     public boolean allowUnderscoreIdentifier() {
       
   227         return compareTo(JDK1_8) <= 0;
       
   228     }
       
   229     public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; }
       
   230     public boolean allowLocalVariableTypeInference() { return compareTo(JDK1_10) >= 0; }
       
   231     public static SourceVersion toSourceVersion(Source source) {
   230     public static SourceVersion toSourceVersion(Source source) {
   232         switch(source) {
   231         switch(source) {
   233         case JDK1_2:
   232         case JDK1_2:
   234             return RELEASE_2;
   233             return RELEASE_2;
   235         case JDK1_3:
   234         case JDK1_3:
   236             return RELEASE_3;
   235             return RELEASE_3;
   237         case JDK1_4:
   236         case JDK1_4:
   238             return RELEASE_4;
   237             return RELEASE_4;
   239         case JDK1_5:
   238         case JDK5:
   240             return RELEASE_5;
   239             return RELEASE_5;
   241         case JDK1_6:
   240         case JDK6:
   242             return RELEASE_6;
   241             return RELEASE_6;
   243         case JDK1_7:
   242         case JDK7:
   244             return RELEASE_7;
   243             return RELEASE_7;
   245         case JDK1_8:
   244         case JDK8:
   246             return RELEASE_8;
   245             return RELEASE_8;
   247         case JDK1_9:
   246         case JDK9:
   248             return RELEASE_9;
   247             return RELEASE_9;
   249         case JDK1_10:
   248         case JDK10:
   250             return RELEASE_10;
   249             return RELEASE_10;
   251         default:
   250         default:
   252             return null;
   251             return null;
   253         }
   252         }
   254     }
   253     }