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