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