1 /* 2 * Copyright (c) 2002, 2014, 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 covers the to be determined language features that will be added in JDK 8. */ 71 JDK1_8("1.8"); 72 73 private static final Context.Key<Source> sourceKey 74 = new Context.Key<Source>(); 75 instance(Context context)76 public static Source instance(Context context) { 77 Source instance = context.get(sourceKey); 78 if (instance == null) { 79 Options options = Options.instance(context); 80 String sourceString = options.get(SOURCE); 81 if (sourceString != null) instance = lookup(sourceString); 82 if (instance == null) instance = DEFAULT; 83 context.put(sourceKey, instance); 84 } 85 return instance; 86 } 87 88 public final String name; 89 90 private static final Map<String,Source> tab = new HashMap<String,Source>(); 91 static { 92 for (Source s : values()) { tab.put(s.name, s)93 tab.put(s.name, s); 94 } 95 tab.put("5", JDK1_5); // Make 5 an alias for 1.5 96 tab.put("6", JDK1_6); // Make 6 an alias for 1.6 97 tab.put("7", JDK1_7); // Make 7 an alias for 1.7 98 tab.put("8", JDK1_8); // Make 8 an alias for 1.8 99 } 100 Source(String name)101 private Source(String name) { 102 this.name = name; 103 } 104 105 public static final Source DEFAULT = JDK1_8; 106 lookup(String name)107 public static Source lookup(String name) { 108 return tab.get(name); 109 } 110 requiredTarget()111 public Target requiredTarget() { 112 if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8; 113 if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7; 114 if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6; 115 if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5; 116 if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; 117 return Target.JDK1_1; 118 } 119 120 /** Allow encoding errors, giving only warnings. */ allowEncodingErrors()121 public boolean allowEncodingErrors() { 122 return compareTo(JDK1_6) < 0; 123 } allowAsserts()124 public boolean allowAsserts() { 125 return compareTo(JDK1_4) >= 0; 126 } allowCovariantReturns()127 public boolean allowCovariantReturns() { 128 return compareTo(JDK1_5) >= 0; 129 } allowGenerics()130 public boolean allowGenerics() { 131 return compareTo(JDK1_5) >= 0; 132 } allowDiamond()133 public boolean allowDiamond() { 134 return compareTo(JDK1_7) >= 0; 135 } allowMulticatch()136 public boolean allowMulticatch() { 137 return compareTo(JDK1_7) >= 0; 138 } allowImprovedRethrowAnalysis()139 public boolean allowImprovedRethrowAnalysis() { 140 return compareTo(JDK1_7) >= 0; 141 } allowImprovedCatchAnalysis()142 public boolean allowImprovedCatchAnalysis() { 143 return compareTo(JDK1_7) >= 0; 144 } allowEnums()145 public boolean allowEnums() { 146 return compareTo(JDK1_5) >= 0; 147 } allowForeach()148 public boolean allowForeach() { 149 return compareTo(JDK1_5) >= 0; 150 } allowStaticImport()151 public boolean allowStaticImport() { 152 return compareTo(JDK1_5) >= 0; 153 } allowBoxing()154 public boolean allowBoxing() { 155 return compareTo(JDK1_5) >= 0; 156 } allowVarargs()157 public boolean allowVarargs() { 158 return compareTo(JDK1_5) >= 0; 159 } allowAnnotations()160 public boolean allowAnnotations() { 161 return compareTo(JDK1_5) >= 0; 162 } 163 // hex floating-point literals supported? allowHexFloats()164 public boolean allowHexFloats() { 165 return compareTo(JDK1_5) >= 0; 166 } allowAnonOuterThis()167 public boolean allowAnonOuterThis() { 168 return compareTo(JDK1_5) >= 0; 169 } addBridges()170 public boolean addBridges() { 171 return compareTo(JDK1_5) >= 0; 172 } enforceMandatoryWarnings()173 public boolean enforceMandatoryWarnings() { 174 return compareTo(JDK1_5) >= 0; 175 } allowTryWithResources()176 public boolean allowTryWithResources() { 177 return compareTo(JDK1_7) >= 0; 178 } allowBinaryLiterals()179 public boolean allowBinaryLiterals() { 180 return compareTo(JDK1_7) >= 0; 181 } allowUnderscoresInLiterals()182 public boolean allowUnderscoresInLiterals() { 183 return compareTo(JDK1_7) >= 0; 184 } allowStringsInSwitch()185 public boolean allowStringsInSwitch() { 186 return compareTo(JDK1_7) >= 0; 187 } allowSimplifiedVarargs()188 public boolean allowSimplifiedVarargs() { 189 return compareTo(JDK1_7) >= 0; 190 } allowObjectToPrimitiveCast()191 public boolean allowObjectToPrimitiveCast() { 192 return compareTo(JDK1_7) >= 0; 193 } enforceThisDotInit()194 public boolean enforceThisDotInit() { 195 return compareTo(JDK1_7) >= 0; 196 } allowPoly()197 public boolean allowPoly() { 198 return compareTo(JDK1_8) >= 0; 199 } allowLambda()200 public boolean allowLambda() { 201 return compareTo(JDK1_8) >= 0; 202 } allowMethodReferences()203 public boolean allowMethodReferences() { 204 return compareTo(JDK1_8) >= 0; 205 } allowDefaultMethods()206 public boolean allowDefaultMethods() { 207 return compareTo(JDK1_8) >= 0; 208 } allowStaticInterfaceMethods()209 public boolean allowStaticInterfaceMethods() { 210 return compareTo(JDK1_8) >= 0; 211 } allowStrictMethodClashCheck()212 public boolean allowStrictMethodClashCheck() { 213 return compareTo(JDK1_8) >= 0; 214 } allowEffectivelyFinalInInnerClasses()215 public boolean allowEffectivelyFinalInInnerClasses() { 216 return compareTo(JDK1_8) >= 0; 217 } allowTypeAnnotations()218 public boolean allowTypeAnnotations() { 219 return compareTo(JDK1_8) >= 0; 220 } allowAnnotationsAfterTypeParams()221 public boolean allowAnnotationsAfterTypeParams() { 222 return compareTo(JDK1_8) >= 0; 223 } allowRepeatedAnnotations()224 public boolean allowRepeatedAnnotations() { 225 return compareTo(JDK1_8) >= 0; 226 } allowIntersectionTypesInCast()227 public boolean allowIntersectionTypesInCast() { 228 return compareTo(JDK1_8) >= 0; 229 } allowGraphInference()230 public boolean allowGraphInference() { 231 return compareTo(JDK1_8) >= 0; 232 } allowFunctionalInterfaceMostSpecific()233 public boolean allowFunctionalInterfaceMostSpecific() { 234 return compareTo(JDK1_8) >= 0; 235 } allowPostApplicabilityVarargsAccessCheck()236 public boolean allowPostApplicabilityVarargsAccessCheck() { 237 return compareTo(JDK1_8) >= 0; 238 } toSourceVersion(Source source)239 public static SourceVersion toSourceVersion(Source source) { 240 switch(source) { 241 case JDK1_2: 242 return RELEASE_2; 243 case JDK1_3: 244 return RELEASE_3; 245 case JDK1_4: 246 return RELEASE_4; 247 case JDK1_5: 248 return RELEASE_5; 249 case JDK1_6: 250 return RELEASE_6; 251 case JDK1_7: 252 return RELEASE_7; 253 case JDK1_8: 254 return RELEASE_8; 255 default: 256 return null; 257 } 258 } 259 } 260