57
57
@ RequiredArgsConstructor (access = AccessLevel .PRIVATE )
58
58
public class ParsedConfiguration {
59
59
private static final Field propField ;
60
+ private static final Map <Class <?>, FieldRefConstructor > constructors = new HashMap <>();
60
61
61
62
static {
62
63
try {
@@ -65,6 +66,18 @@ public class ParsedConfiguration {
65
66
throw new RuntimeException (e );
66
67
}
67
68
ReflectionUtil .jailBreak (propField );
69
+ constructors .put (Boolean .class , BooleanConfigField ::new );
70
+ constructors .put (boolean .class , BooleanConfigField ::new );
71
+ constructors .put (Integer .class , IntConfigField ::new );
72
+ constructors .put (int .class , IntConfigField ::new );
73
+ constructors .put (Float .class , FloatConfigField ::new );
74
+ constructors .put (float .class , FloatConfigField ::new );
75
+ constructors .put (Double .class , DoubleConfigField ::new );
76
+ constructors .put (double .class , DoubleConfigField ::new );
77
+ constructors .put (boolean [].class , BooleanListConfigField ::new );
78
+ constructors .put (int [].class , IntListConfigField ::new );
79
+ constructors .put (double [].class , DoubleListConfigField ::new );
80
+ constructors .put (String [].class , StringListConfigField ::new );
68
81
}
69
82
70
83
public final Class <?> configClass ;
@@ -170,27 +183,10 @@ public void reloadFields() throws ConfigException, IllegalAccessException {
170
183
maxFieldNameLength = Math .max (maxFieldNameLength , field .getName ().length ());
171
184
val fieldClass = field .getType ();
172
185
val name = field .getName ();
173
- if (fieldClass .equals (Boolean .class ) || fieldClass .equals (boolean .class )) {
174
- fields .put (name , new BooleanConfigField (field , rawConfig , category ));
175
- } else if (fieldClass .equals (Integer .class ) || fieldClass .equals (int .class )) {
176
- fields .put (name , new IntConfigField (field , rawConfig , category ));
177
- } else if (fieldClass .equals (Float .class ) || fieldClass .equals (float .class )) {
178
- //noinspection deprecation
179
- fields .put (name , new FloatConfigField (field , rawConfig , category ));
180
- } else if (fieldClass .equals (Double .class ) || fieldClass .equals (double .class )) {
181
- fields .put (name , new DoubleConfigField (field , rawConfig , category ));
182
- } else if (fieldClass .equals (String .class )) {
183
- fields .put (name , new StringConfigField (field , rawConfig , category ));
186
+ if (constructors .containsKey (fieldClass )) {
187
+ fields .put (name , constructors .get (fieldClass ).construct (field , rawConfig , category ));
184
188
} else if (fieldClass .isEnum ()) {
185
189
fields .put (name , new EnumConfigField <>(field , rawConfig , category ));
186
- } else if (fieldClass .isArray () && fieldClass .getComponentType ().equals (boolean .class )) {
187
- fields .put (name , new BooleanListConfigField (field , rawConfig , category ));
188
- } else if (fieldClass .isArray () && fieldClass .getComponentType ().equals (int .class )) {
189
- fields .put (name , new IntListConfigField (field , rawConfig , category ));
190
- } else if (fieldClass .isArray () && fieldClass .getComponentType ().equals (double .class )) {
191
- fields .put (name , new DoubleListConfigField (field , rawConfig , category ));
192
- } else if (fieldClass .isArray () && fieldClass .getComponentType ().equals (String .class )) {
193
- fields .put (name , new StringListConfigField (field , rawConfig , category ));
194
190
} else {
195
191
throw new ConfigException ("Illegal config field: " + field .getName () + " in " + configClass .getName () +
196
192
": Unsupported type " + fieldClass .getName () +
@@ -226,4 +222,8 @@ public boolean validate(BiConsumer<Class<?>, Field> invalidFieldHandler, boolean
226
222
}
227
223
return valid ;
228
224
}
225
+
226
+ private interface FieldRefConstructor {
227
+ AConfigField <?> construct (Field field , Configuration configuration , String category ) throws ConfigException ;
228
+ }
229
229
}
0 commit comments