2018-08-16 09:56:31 +00:00
|
|
|
package com.boydti.fawe.util;
|
|
|
|
|
|
|
|
import sun.misc.Unsafe;
|
|
|
|
|
2019-12-21 20:21:25 +00:00
|
|
|
import java.lang.reflect.AccessibleObject;
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Field;
|
2018-08-16 09:56:31 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ReflectionUtils9 {
|
|
|
|
public static <T extends Enum<?>> T addEnum(Class<T> enumType, String enumName) {
|
|
|
|
|
|
|
|
// 0. Sanity checks
|
|
|
|
if (!Enum.class.isAssignableFrom(enumType)) {
|
|
|
|
throw new RuntimeException("class " + enumType + " is not an instance of Enum");
|
|
|
|
}
|
|
|
|
// 1. Lookup "$VALUES" holder in enum class and get previous enum instances
|
|
|
|
Field valuesField = null;
|
|
|
|
Field[] fields = enumType.getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if (field.getName().contains("$VALUES")) {
|
|
|
|
valuesField = field;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AccessibleObject.setAccessible(new Field[]{valuesField}, true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 2. Copy it
|
|
|
|
T[] previousValues = (T[]) valuesField.get(enumType);
|
2020-01-03 20:38:30 +00:00
|
|
|
List<T> values = new ArrayList<>(Arrays.asList(previousValues));
|
2018-08-16 09:56:31 +00:00
|
|
|
|
|
|
|
// 3. build new enum
|
|
|
|
T newValue = (T) makeEnum(enumType, // The target enum class
|
2019-06-12 13:45:41 +00:00
|
|
|
enumName, // THE NEW ENUM INSTANCE TO BE DYNAMICALLY ADDED
|
|
|
|
values.size()); // can be used to pass values to the enum constuctor
|
2018-08-16 09:56:31 +00:00
|
|
|
|
|
|
|
// 4. add new value
|
|
|
|
values.add(newValue);
|
|
|
|
|
|
|
|
// 5. Set new values field
|
|
|
|
try {
|
2019-12-21 20:21:25 +00:00
|
|
|
ReflectionUtils.setFailsafeFieldValue(valuesField, null,
|
2019-06-12 13:45:41 +00:00
|
|
|
values.toArray((T[]) Array.newInstance(enumType, 0)));
|
2018-08-16 09:56:31 +00:00
|
|
|
} catch (Throwable e) {
|
|
|
|
Field ordinalField = Enum.class.getDeclaredField("ordinal");
|
2019-12-21 20:21:25 +00:00
|
|
|
ReflectionUtils.setFailsafeFieldValue(ordinalField, newValue, 0);
|
2018-08-16 09:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 6. Clean enum cache
|
2019-12-21 20:21:25 +00:00
|
|
|
ReflectionUtils.cleanEnumCache(enumType);
|
2018-08-16 09:56:31 +00:00
|
|
|
return newValue;
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object makeEnum(Class<?> enumClass, String value, int ordinal) throws Exception {
|
|
|
|
Constructor<?> constructor = Unsafe.class.getDeclaredConstructors()[0];
|
|
|
|
constructor.setAccessible(true);
|
|
|
|
Unsafe unsafe = (Unsafe) constructor.newInstance();
|
|
|
|
Object instance = unsafe.allocateInstance(enumClass);
|
|
|
|
|
|
|
|
Field ordinalField = Enum.class.getDeclaredField("ordinal");
|
2019-12-21 20:21:25 +00:00
|
|
|
ReflectionUtils.setFailsafeFieldValue(ordinalField, instance, 0);
|
2018-08-16 09:56:31 +00:00
|
|
|
|
|
|
|
Field nameField = Enum.class.getDeclaredField("name");
|
2019-12-21 20:21:25 +00:00
|
|
|
ReflectionUtils.setFailsafeFieldValue(nameField, instance, value);
|
2018-08-16 09:56:31 +00:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|