< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2019, 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


  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, no language features; switch expression were in preview */
  88     JDK12("12"),
  89 
  90     /**
  91      * 13, no language features; text blocks and revised switch
  92      * expressions in preview
  93      */
  94     JDK13("13"),
  95 
  96     /**
  97      * 14 covers the to be determined language features that will be
  98      * added in JDK 14.
  99      */
 100     JDK14("14");
 101 
 102     private static final Context.Key<Source> sourceKey = new Context.Key<>();
 103 
 104     public static Source instance(Context context) {
 105         Source instance = context.get(sourceKey);
 106         if (instance == null) {
 107             Options options = Options.instance(context);
 108             String sourceString = options.get(SOURCE);
 109             if (sourceString != null) instance = lookup(sourceString);
 110             if (instance == null) instance = DEFAULT;
 111             context.put(sourceKey, instance);
 112         }
 113         return instance;
 114     }
 115 
 116     public final String name;
 117 
 118     private static final Map<String,Source> tab = new HashMap<>();
 119     static {
 120         for (Source s : values()) {


 131 
 132     private Source(String name) {
 133         this.name = name;
 134     }
 135 
 136     public static final Source MIN = Source.JDK7;
 137 
 138     private static final Source MAX = values()[values().length - 1];
 139 
 140     public static final Source DEFAULT = MAX;
 141 
 142     public static Source lookup(String name) {
 143         return tab.get(name);
 144     }
 145 
 146     public boolean isSupported() {
 147         return this.compareTo(MIN) >= 0;
 148     }
 149 
 150     public Target requiredTarget() {
 151         if (this.compareTo(JDK14) >= 0) return Target.JDK1_14;
 152         if (this.compareTo(JDK13) >= 0) return Target.JDK1_13;
 153         if (this.compareTo(JDK12) >= 0) return Target.JDK1_12;
 154         if (this.compareTo(JDK11) >= 0) return Target.JDK1_11;
 155         if (this.compareTo(JDK10) >= 0) return Target.JDK1_10;
 156         if (this.compareTo(JDK9) >= 0) return Target.JDK1_9;
 157         if (this.compareTo(JDK8) >= 0) return Target.JDK1_8;
 158         if (this.compareTo(JDK7) >= 0) return Target.JDK1_7;
 159         if (this.compareTo(JDK6) >= 0) return Target.JDK1_6;
 160         if (this.compareTo(JDK5) >= 0) return Target.JDK1_5;
 161         if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4;
 162         return Target.JDK1_1;
 163     }
 164 
 165     /**
 166      * Models a feature of the Java programming language. Each feature can be associated with a
 167      * minimum source level, a maximum source level and a diagnostic fragment describing the feature,
 168      * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}.
 169      */
 170     public enum Feature {
 171 


 179         DEFAULT_METHODS(JDK8, Fragments.FeatureDefaultMethods, DiagKind.PLURAL),
 180         STATIC_INTERFACE_METHODS(JDK8, Fragments.FeatureStaticIntfMethods, DiagKind.PLURAL),
 181         STATIC_INTERFACE_METHODS_INVOKE(JDK8, Fragments.FeatureStaticIntfMethodInvoke, DiagKind.PLURAL),
 182         STRICT_METHOD_CLASH_CHECK(JDK8),
 183         EFFECTIVELY_FINAL_IN_INNER_CLASSES(JDK8),
 184         TYPE_ANNOTATIONS(JDK8, Fragments.FeatureTypeAnnotations, DiagKind.PLURAL),
 185         ANNOTATIONS_AFTER_TYPE_PARAMS(JDK8, Fragments.FeatureAnnotationsAfterTypeParams, DiagKind.PLURAL),
 186         REPEATED_ANNOTATIONS(JDK8, Fragments.FeatureRepeatableAnnotations, DiagKind.PLURAL),
 187         INTERSECTION_TYPES_IN_CAST(JDK8, Fragments.FeatureIntersectionTypesInCast, DiagKind.PLURAL),
 188         GRAPH_INFERENCE(JDK8),
 189         FUNCTIONAL_INTERFACE_MOST_SPECIFIC(JDK8),
 190         POST_APPLICABILITY_VARARGS_ACCESS_CHECK(JDK8),
 191         MAP_CAPTURES_TO_BOUNDS(MIN, JDK7),
 192         PRIVATE_SAFE_VARARGS(JDK9),
 193         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
 194         UNDERSCORE_IDENTIFIER(MIN, JDK8),
 195         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
 196         LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
 197         VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
 198         IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8),
 199         SWITCH_MULTIPLE_CASE_LABELS(JDK14, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
 200         SWITCH_RULE(JDK14, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
 201         SWITCH_EXPRESSION(JDK14, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
 202         TEXT_BLOCKS(JDK14, Fragments.FeatureTextBlocks, DiagKind.PLURAL);
 203 
 204         enum DiagKind {
 205             NORMAL,
 206             PLURAL;
 207         }
 208 
 209         private final Source minLevel;
 210         private final Source maxLevel;
 211         private final Fragment optFragment;
 212         private final DiagKind optKind;
 213 
 214         Feature(Source minLevel) {
 215             this(minLevel, null, null);
 216         }
 217 
 218         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
 219             this(minLevel, MAX, optFragment, optKind);
 220         }
 221 
 222         Feature(Source minLevel, Source maxLevel) {


 269         case JDK1_4:
 270             return RELEASE_4;
 271         case JDK5:
 272             return RELEASE_5;
 273         case JDK6:
 274             return RELEASE_6;
 275         case JDK7:
 276             return RELEASE_7;
 277         case JDK8:
 278             return RELEASE_8;
 279         case JDK9:
 280             return RELEASE_9;
 281         case JDK10:
 282             return RELEASE_10;
 283         case JDK11:
 284             return RELEASE_11;
 285         case JDK12:
 286             return RELEASE_12;
 287         case JDK13:
 288             return RELEASE_13;
 289         case JDK14:
 290             return RELEASE_14;
 291         default:
 292             return null;
 293         }
 294     }
 295 }
   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


  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()) {


 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 


 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 
 193         enum DiagKind {
 194             NORMAL,
 195             PLURAL;
 196         }
 197 
 198         private final Source minLevel;
 199         private final Source maxLevel;
 200         private final Fragment optFragment;
 201         private final DiagKind optKind;
 202 
 203         Feature(Source minLevel) {
 204             this(minLevel, null, null);
 205         }
 206 
 207         Feature(Source minLevel, Fragment optFragment, DiagKind optKind) {
 208             this(minLevel, MAX, optFragment, optKind);
 209         }
 210 
 211         Feature(Source minLevel, Source maxLevel) {


 258         case JDK1_4:
 259             return RELEASE_4;
 260         case JDK5:
 261             return RELEASE_5;
 262         case JDK6:
 263             return RELEASE_6;
 264         case JDK7:
 265             return RELEASE_7;
 266         case JDK8:
 267             return RELEASE_8;
 268         case JDK9:
 269             return RELEASE_9;
 270         case JDK10:
 271             return RELEASE_10;
 272         case JDK11:
 273             return RELEASE_11;
 274         case JDK12:
 275             return RELEASE_12;
 276         case JDK13:
 277             return RELEASE_13;


 278         default:
 279             return null;
 280         }
 281     }
 282 }
< prev index next >