1 /* 2 * Copyright (c) 2002, 2017, 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.util.*; 35 import static com.sun.tools.javac.main.Option.*; 36 37 /** The source language version accepted. 38 * 39 * <p><b>This is NOT part of any supported API. 40 * If you write code that depends on this, you do so at your own risk. 41 * This code and its internal interfaces are subject to change or 42 * deletion without notice.</b> 43 */ 44 public enum Source { 45 /** 1.0 had no inner classes, and so could not pass the JCK. */ 46 // public static final Source JDK1_0 = new Source("1.0"); 47 48 /** 1.1 did not have strictfp, and so could not pass the JCK. */ 49 // public static final Source JDK1_1 = new Source("1.1"); 50 51 /** 1.2 introduced strictfp. */ 52 JDK1_2("1.2"), 53 54 /** 1.3 is the same language as 1.2. */ 55 JDK1_3("1.3"), 56 57 /** 1.4 introduced assert. */ 58 JDK1_4("1.4"), 59 60 /** 1.5 introduced generics, attributes, foreach, boxing, static import, 61 * covariant return, enums, varargs, et al. */ 62 JDK1_5("1.5"), 63 64 /** 1.6 reports encoding problems as errors instead of warnings. */ 65 JDK1_6("1.6"), 66 67 /** 1.7 introduced try-with-resources, multi-catch, string switch, etc. */ 68 JDK1_7("1.7"), 69 70 /** 1.8 lambda expressions and default methods. */ 71 JDK1_8("1.8"), 72 73 /** 1.9 modularity. */ 74 JDK1_9("1.9"), 75 76 /** 1.10 covers the to be determined language features that will be added in JDK 10. */ 77 JDK1_10("1.10"), 78 79 JDK_11("11"); 80 81 82 private static final Context.Key<Source> sourceKey = new Context.Key<>(); 83 84 public static Source instance(Context context) { 85 Source instance = context.get(sourceKey); 86 if (instance == null) { 87 Options options = Options.instance(context); 88 String sourceString = options.get(SOURCE); 89 if (sourceString != null) instance = lookup(sourceString); 90 if (instance == null) instance = DEFAULT; 91 context.put(sourceKey, instance); 92 } 93 return instance; 94 } 95 96 public final String name; 97 98 private static final Map<String,Source> tab = new HashMap<>(); 99 static { 100 for (Source s : values()) { 101 tab.put(s.name, s); 102 } 103 tab.put("5", JDK1_5); // Make 5 an alias for 1.5 104 tab.put("6", JDK1_6); // Make 6 an alias for 1.6 105 tab.put("7", JDK1_7); // Make 7 an alias for 1.7 106 tab.put("8", JDK1_8); // Make 8 an alias for 1.8 107 tab.put("9", JDK1_9); // Make 9 an alias for 1.9 108 tab.put("10", JDK1_10); // Make 10 an alias for 1.10 109 tab.put("11", JDK_11); 110 } 111 112 private Source(String name) { 113 this.name = name; 114 } 115 116 public static final Source MIN = Source.JDK1_6; 117 118 private static final Source MAX = values()[values().length - 1]; 119 120 public static final Source DEFAULT = JDK1_10; // MAX; - FIXME later 121 122 public static Source lookup(String name) { 123 return tab.get(name); 124 } 125 126 public Target requiredTarget() { 127 if (this.compareTo(JDK_11) >= 0) return Target.JDK_11; 128 if (this.compareTo(JDK1_10) >= 0) return Target.JDK1_10; 129 if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9; 130 if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8; 131 if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7; 132 if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6; 133 if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5; 134 if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; 135 return Target.JDK1_1; 136 } 137 138 public boolean allowDiamond() { 139 return compareTo(JDK1_7) >= 0; 140 } 141 public boolean allowMulticatch() { 142 return compareTo(JDK1_7) >= 0; 143 } 144 public boolean allowImprovedRethrowAnalysis() { 145 return compareTo(JDK1_7) >= 0; 146 } 147 public boolean allowImprovedCatchAnalysis() { 148 return compareTo(JDK1_7) >= 0; 149 } 150 public boolean allowModules() { 151 return compareTo(JDK1_9) >= 0; 152 } 153 public boolean allowTryWithResources() { 154 return compareTo(JDK1_7) >= 0; 155 } 156 public boolean allowEffectivelyFinalVariablesInTryWithResources() { 157 return compareTo(JDK1_9) >= 0; 158 } 159 public boolean allowBinaryLiterals() { 160 return compareTo(JDK1_7) >= 0; 161 } 162 public boolean allowUnderscoresInLiterals() { 163 return compareTo(JDK1_7) >= 0; 164 } 165 public boolean allowStringsInSwitch() { 166 return compareTo(JDK1_7) >= 0; 167 } 168 public boolean allowDeprecationOnImport() { 169 return compareTo(JDK1_9) < 0; 170 } 171 public boolean allowSimplifiedVarargs() { 172 return compareTo(JDK1_7) >= 0; 173 } 174 public boolean allowObjectToPrimitiveCast() { 175 return compareTo(JDK1_7) >= 0; 176 } 177 public boolean enforceThisDotInit() { 178 return compareTo(JDK1_7) >= 0; 179 } 180 public boolean allowPoly() { 181 return compareTo(JDK1_8) >= 0; 182 } 183 public boolean allowLambda() { 184 return compareTo(JDK1_8) >= 0; 185 } 186 public boolean allowMethodReferences() { 187 return compareTo(JDK1_8) >= 0; 188 } 189 public boolean allowDefaultMethods() { 190 return compareTo(JDK1_8) >= 0; 191 } 192 public boolean allowStaticInterfaceMethods() { 193 return compareTo(JDK1_8) >= 0; 194 } 195 public boolean allowStrictMethodClashCheck() { 196 return compareTo(JDK1_8) >= 0; 197 } 198 public boolean allowEffectivelyFinalInInnerClasses() { 199 return compareTo(JDK1_8) >= 0; 200 } 201 public boolean allowTypeAnnotations() { 202 return compareTo(JDK1_8) >= 0; 203 } 204 public boolean allowAnnotationsAfterTypeParams() { 205 return compareTo(JDK1_8) >= 0; 206 } 207 public boolean allowRepeatedAnnotations() { 208 return compareTo(JDK1_8) >= 0; 209 } 210 public boolean allowIntersectionTypesInCast() { 211 return compareTo(JDK1_8) >= 0; 212 } 213 public boolean allowGraphInference() { 214 return compareTo(JDK1_8) >= 0; 215 } 216 public boolean allowFunctionalInterfaceMostSpecific() { 217 return compareTo(JDK1_8) >= 0; 218 } 219 public boolean allowPostApplicabilityVarargsAccessCheck() { 220 return compareTo(JDK1_8) >= 0; 221 } 222 public boolean mapCapturesToBounds() { 223 return compareTo(JDK1_8) < 0; 224 } 225 public boolean allowPrivateSafeVarargs() { 226 return compareTo(JDK1_9) >= 0; 227 } 228 public boolean allowDiamondWithAnonymousClassCreation() { 229 return compareTo(JDK1_9) >= 0; 230 } 231 public boolean allowUnderscoreIdentifier() { 232 return compareTo(JDK1_8) <= 0; 233 } 234 public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; } 235 public static SourceVersion toSourceVersion(Source source) { 236 switch(source) { 237 case JDK1_2: 238 return RELEASE_2; 239 case JDK1_3: 240 return RELEASE_3; 241 case JDK1_4: 242 return RELEASE_4; 243 case JDK1_5: 244 return RELEASE_5; 245 case JDK1_6: 246 return RELEASE_6; 247 case JDK1_7: 248 return RELEASE_7; 249 case JDK1_8: 250 return RELEASE_8; 251 case JDK1_9: 252 return RELEASE_9; 253 case JDK1_10: 254 return RELEASE_10; 255 case JDK_11: 256 return RELEASE_11; 257 default: 258 return null; 259 } 260 } 261 }