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 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()) { 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 101 private Source(String name) { 102 this.name = name; 103 } 104 105 public static final Source DEFAULT = JDK1_8; 106 107 public static Source lookup(String name) { 108 return tab.get(name); 109 } 110 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. */ 121 public boolean allowEncodingErrors() { 122 return compareTo(JDK1_6) < 0; 123 } 124 public boolean allowAsserts() { 125 return compareTo(JDK1_4) >= 0; 126 } 127 public boolean allowCovariantReturns() { 128 return compareTo(JDK1_5) >= 0; 129 } 130 public boolean allowGenerics() { 131 return compareTo(JDK1_5) >= 0; 226 } 227 public boolean allowStructuralMostSpecific() { 228 return compareTo(JDK1_8) >= 0; 229 } 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 default: 247 return null; 248 } 249 } 250 } | 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 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 77 = new Context.Key<Source>(); 78 79 public static Source instance(Context context) { 80 Source instance = context.get(sourceKey); 81 if (instance == null) { 82 Options options = Options.instance(context); 83 String sourceString = options.get(SOURCE); 84 if (sourceString != null) instance = lookup(sourceString); 85 if (instance == null) instance = DEFAULT; 86 context.put(sourceKey, instance); 87 } 88 return instance; 89 } 90 91 public final String name; 92 93 private static final Map<String,Source> tab = new HashMap<>(); 94 static { 95 for (Source s : values()) { 96 tab.put(s.name, s); 97 } 98 tab.put("5", JDK1_5); // Make 5 an alias for 1.5 99 tab.put("6", JDK1_6); // Make 6 an alias for 1.6 100 tab.put("7", JDK1_7); // Make 7 an alias for 1.7 101 tab.put("8", JDK1_8); // Make 8 an alias for 1.8 102 tab.put("9", JDK1_9); // Make 9 an alias for 1.9 103 } 104 105 private Source(String name) { 106 this.name = name; 107 } 108 109 public static final Source DEFAULT = JDK1_9; 110 111 public static Source lookup(String name) { 112 return tab.get(name); 113 } 114 115 public Target requiredTarget() { 116 if (this.compareTo(JDK1_9) >= 0) return Target.JDK1_9; 117 if (this.compareTo(JDK1_8) >= 0) return Target.JDK1_8; 118 if (this.compareTo(JDK1_7) >= 0) return Target.JDK1_7; 119 if (this.compareTo(JDK1_6) >= 0) return Target.JDK1_6; 120 if (this.compareTo(JDK1_5) >= 0) return Target.JDK1_5; 121 if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; 122 return Target.JDK1_1; 123 } 124 125 /** Allow encoding errors, giving only warnings. */ 126 public boolean allowEncodingErrors() { 127 return compareTo(JDK1_6) < 0; 128 } 129 public boolean allowAsserts() { 130 return compareTo(JDK1_4) >= 0; 131 } 132 public boolean allowCovariantReturns() { 133 return compareTo(JDK1_5) >= 0; 134 } 135 public boolean allowGenerics() { 136 return compareTo(JDK1_5) >= 0; 231 } 232 public boolean allowStructuralMostSpecific() { 233 return compareTo(JDK1_8) >= 0; 234 } 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_8; // Adjust once RELEASE_9 exists 253 default: 254 return null; 255 } 256 } 257 } |