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 covers the to be determined language features that will be added in JDK 9. */ 74 JDK1_9("1.9"); 75 76 private static final Context.Key<Source> sourceKey = new Context.Key<>(); 77 78 public static Source instance(Context context) { 79 Source instance = context.get(sourceKey); 80 if (instance == null) { 81 Options options = Options.instance(context); 82 String sourceString = options.get(SOURCE); 83 if (sourceString != null) instance = lookup(sourceString); 84 if (instance == null) instance = DEFAULT; 85 context.put(sourceKey, instance); 86 } 87 return instance; 88 } 89 90 public final String name; 91 92 private static final Map<String,Source> tab = new HashMap<>(); 93 static { 94 for (Source s : values()) { 95 tab.put(s.name, s); 96 } 97 tab.put("5", JDK1_5); // Make 5 an alias for 1.5 98 tab.put("6", JDK1_6); // Make 6 an alias for 1.6 99 tab.put("7", JDK1_7); // Make 7 an alias for 1.7 100 tab.put("8", JDK1_8); // Make 8 an alias for 1.8 101 tab.put("9", JDK1_9); // Make 9 an alias for 1.9 102 } 103 104 private Source(String name) { 105 this.name = name; 106 } 107 108 public static final Source MIN = Source.JDK1_6; 109 110 private static final Source MAX = values()[values().length - 1]; 111 112 public static final Source DEFAULT = MAX; 113 114 public static Source lookup(String name) { 115 return tab.get(name); 116 } 117 118 public Target requiredTarget() { 119 if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9; 120 if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8; 121 if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7; 122 if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6; 123 if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5; 124 if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; 125 return Target.JDK1_1; 126 } 127 128 public boolean allowDiamond() { 129 return compareTo(JDK1_7) >= 0; 130 } 131 public boolean allowMulticatch() { 132 return compareTo(JDK1_7) >= 0; 133 } 134 public boolean allowImprovedRethrowAnalysis() { 135 return compareTo(JDK1_7) >= 0; 136 } 137 public boolean allowImprovedCatchAnalysis() { 138 return compareTo(JDK1_7) >= 0; 223 } 224 public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; } 225 public static SourceVersion toSourceVersion(Source source) { 226 switch(source) { 227 case JDK1_2: 228 return RELEASE_2; 229 case JDK1_3: 230 return RELEASE_3; 231 case JDK1_4: 232 return RELEASE_4; 233 case JDK1_5: 234 return RELEASE_5; 235 case JDK1_6: 236 return RELEASE_6; 237 case JDK1_7: 238 return RELEASE_7; 239 case JDK1_8: 240 return RELEASE_8; 241 case JDK1_9: 242 return RELEASE_9; 243 default: 244 return null; 245 } 246 } 247 } | 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 private static final Context.Key<Source> sourceKey = new Context.Key<>(); 80 81 public static Source instance(Context context) { 82 Source instance = context.get(sourceKey); 83 if (instance == null) { 84 Options options = Options.instance(context); 85 String sourceString = options.get(SOURCE); 86 if (sourceString != null) instance = lookup(sourceString); 87 if (instance == null) instance = DEFAULT; 88 context.put(sourceKey, instance); 89 } 90 return instance; 91 } 92 93 public final String name; 94 95 private static final Map<String,Source> tab = new HashMap<>(); 96 static { 97 for (Source s : values()) { 98 tab.put(s.name, s); 99 } 100 tab.put("5", JDK1_5); // Make 5 an alias for 1.5 101 tab.put("6", JDK1_6); // Make 6 an alias for 1.6 102 tab.put("7", JDK1_7); // Make 7 an alias for 1.7 103 tab.put("8", JDK1_8); // Make 8 an alias for 1.8 104 tab.put("9", JDK1_9); // Make 9 an alias for 1.9 105 tab.put("10", JDK1_10); // Make 10 an alias for 1.10 106 } 107 108 private Source(String name) { 109 this.name = name; 110 } 111 112 public static final Source MIN = Source.JDK1_6; 113 114 private static final Source MAX = values()[values().length - 1]; 115 116 public static final Source DEFAULT = MAX; 117 118 public static Source lookup(String name) { 119 return tab.get(name); 120 } 121 122 public Target requiredTarget() { 123 if (this.compareTo(JDK1_10) >= 0) return Target.JDK1_10; 124 if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9; 125 if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8; 126 if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7; 127 if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6; 128 if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5; 129 if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; 130 return Target.JDK1_1; 131 } 132 133 public boolean allowDiamond() { 134 return compareTo(JDK1_7) >= 0; 135 } 136 public boolean allowMulticatch() { 137 return compareTo(JDK1_7) >= 0; 138 } 139 public boolean allowImprovedRethrowAnalysis() { 140 return compareTo(JDK1_7) >= 0; 141 } 142 public boolean allowImprovedCatchAnalysis() { 143 return compareTo(JDK1_7) >= 0; 228 } 229 public boolean allowPrivateInterfaceMethods() { return compareTo(JDK1_9) >= 0; } 230 public static SourceVersion toSourceVersion(Source source) { 231 switch(source) { 232 case JDK1_2: 233 return RELEASE_2; 234 case JDK1_3: 235 return RELEASE_3; 236 case JDK1_4: 237 return RELEASE_4; 238 case JDK1_5: 239 return RELEASE_5; 240 case JDK1_6: 241 return RELEASE_6; 242 case JDK1_7: 243 return RELEASE_7; 244 case JDK1_8: 245 return RELEASE_8; 246 case JDK1_9: 247 return RELEASE_9; 248 case JDK1_10: 249 return RELEASE_10; 250 default: 251 return null; 252 } 253 } 254 } |