1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import sun.reflect.ConstructorAccessor; import sun.reflect.FieldAccessor; import sun.reflect.ReflectionFactory;
public class EnumUtils {
private static ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
private static void setFailsafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false); fa.set(target, value); }
private static void blankField(Class<?> enumClass, String fieldName) throws NoSuchFieldException, IllegalAccessException { for (Field field : Class.class.getDeclaredFields()) { if (field.getName().contains(fieldName)) { AccessibleObject.setAccessible(new Field[] { field }, true); setFailsafeFieldValue(field, enumClass, null); break; } } }
private static void cleanEnumCache(Class<?> enumClass) throws NoSuchFieldException, IllegalAccessException { blankField(enumClass, "enumConstantDirectory"); blankField(enumClass, "enumConstants"); }
private static ConstructorAccessor getConstructorAccessor(Class<?> enumClass, Class<?>[] additionalParameterTypes) throws NoSuchMethodException { Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2]; parameterTypes[0] = String.class; parameterTypes[1] = int.class; System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length); return reflectionFactory.newConstructorAccessor(enumClass.getDeclaredConstructor(parameterTypes)); }
private static Object makeEnum(Class<?> enumClass, String value, int ordinal, Class<?>[] additionalTypes, Object[] additionalValues) throws Exception { Object[] params = new Object[additionalValues.length + 2]; params[0] = value; params[1] = Integer.valueOf(ordinal); System.arraycopy(additionalValues, 0, params, 2, additionalValues.length); return enumClass.cast(getConstructorAccessor(enumClass, additionalTypes).newInstance(params)); }
@SuppressWarnings("unchecked") public static <T extends Enum<?>> void addEnum(Class<T> enumType, String enumName, Class<?>[] additionalTypes, Object[] additionalValues) { if (!Enum.class.isAssignableFrom(enumType)) { throw new RuntimeException("class " + enumType + " is not an instance of Enum"); } 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 { T[] previousValues = (T[]) valuesField.get(enumType); List<T> values = new ArrayList<T>(Arrays.asList(previousValues)); T newValue = (T) makeEnum(enumType, enumName, values.size(), additionalTypes, additionalValues); values.add(newValue); setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0))); cleanEnumCache(enumType); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }
|