Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ltd.genuine.enums;
- 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 net.minecraft.src.EnumRarity;
- // These are restricted... I wonder why?!
- import sun.reflect.ConstructorAccessor;
- import sun.reflect.FieldAccessor;
- import sun.reflect.ReflectionFactory;
- @SuppressWarnings("restriction")
- public class EnumInjection {
- 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"); // Sun (Oracle?!?) JDK 1.5/6
- blankField(enumClass, "enumConstants"); // IBM JDK
- }
- 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[] parms = new Object[additionalValues.length + 2];
- parms[0] = value;
- parms[1] = Integer.valueOf(ordinal);
- System.arraycopy(additionalValues, 0, parms, 2, additionalValues.length);
- return enumClass.cast(getConstructorAccessor(enumClass, additionalTypes).newInstance(parms));
- }
- /**
- * Add an enum instance to the enum class given as argument
- *
- * @param <T>
- * the type of the enum (implicit)
- * @param enumType
- * the class of the enum to be modified
- * @param enumName
- * the name of the new enum instance to be added to the class.
- */
- @SuppressWarnings("unchecked")
- public static <T extends Enum<?>> T addEnum(Class<T> enumType, String enumName, Class[] initClasses, Object... initValues) {
- if (!Enum.class.isAssignableFrom(enumType)) {
- throw new RuntimeException("You're doing it wrong!");
- }
- 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(), initClasses, initValues);
- // Rarity specific fixes. I might make this more versatile.
- if (newValue instanceof EnumRarity) {
- boolean flag = false;
- for (int i = 0; i < values.size(); i++) {
- EnumRarity rarity = (EnumRarity) values.get(i);
- if (rarity.rarityName.equalsIgnoreCase(initValues[1].toString())) {
- for (Field replace : EnumRarity.class.getDeclaredFields()) {
- replace.setAccessible(true);
- if (replace.get(rarity) instanceof Integer) {
- setFailsafeFieldValue(replace, rarity, initValues[0]);
- flag = true;
- }
- }
- }
- }
- if (!flag) {
- values.add(newValue);
- setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
- }
- } else {
- values.add(newValue);
- setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
- }
- cleanEnumCache(enumType);
- // DEBUG: Output the new array, you'll only notice new enums.
- System.out.println(values);
- return newValue;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement