|
27 | 27 | import java.io.IOException;
|
28 | 28 | import java.lang.reflect.AccessibleObject;
|
29 | 29 | import java.lang.reflect.Field;
|
30 |
| -import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
31 | 31 | import java.util.HashMap;
|
32 | 32 | import java.util.Map;
|
33 | 33 |
|
@@ -59,17 +59,21 @@ private EnumTypeAdapter(final Class<T> classOfT) {
|
59 | 59 | // Uses reflection to find enum constants to work around name mismatches for obfuscated
|
60 | 60 | // classes
|
61 | 61 | Field[] fields = classOfT.getDeclaredFields();
|
62 |
| - ArrayList<Field> constantFieldsList = new ArrayList<>(fields.length); |
| 62 | + int constantCount = 0; |
63 | 63 | for (Field f : fields) {
|
| 64 | + // Filter out non-constant fields, replacing elements as we go |
64 | 65 | if (f.isEnumConstant()) {
|
65 |
| - constantFieldsList.add(f); |
| 66 | + fields[constantCount++] = f; |
66 | 67 | }
|
67 | 68 | }
|
68 | 69 |
|
69 |
| - Field[] constantFields = constantFieldsList.toArray(new Field[0]); |
70 |
| - AccessibleObject.setAccessible(constantFields, true); |
| 70 | + // Trim the array to the new length. Every enum type can be expected to have at least |
| 71 | + // one declared field which is not an enum constant, namely the implicit $VALUES array |
| 72 | + fields = Arrays.copyOf(fields, constantCount); |
71 | 73 |
|
72 |
| - for (Field constantField : constantFields) { |
| 74 | + AccessibleObject.setAccessible(fields, true); |
| 75 | + |
| 76 | + for (Field constantField : fields) { |
73 | 77 | @SuppressWarnings("unchecked")
|
74 | 78 | T constant = (T) constantField.get(null);
|
75 | 79 | String name = constant.name();
|
|
0 commit comments