GenuineSounds

EnumInjection

Jun 11th, 2012
193
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. package ltd.genuine.enums;
  2.  
  3. import java.lang.reflect.AccessibleObject;
  4. import java.lang.reflect.Array;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Modifier;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10.  
  11. import net.minecraft.src.EnumRarity;
  12.  
  13. // These are restricted... I wonder why?!
  14. import sun.reflect.ConstructorAccessor;
  15. import sun.reflect.FieldAccessor;
  16. import sun.reflect.ReflectionFactory;
  17.  
  18. @SuppressWarnings("restriction")
  19. public class EnumInjection {
  20.  
  21.     private static ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
  22.  
  23.     private static void setFailsafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException, IllegalAccessException {
  24.         field.setAccessible(true);
  25.         Field modifiersField = Field.class.getDeclaredField("modifiers");
  26.         modifiersField.setAccessible(true);
  27.         int modifiers = modifiersField.getInt(field);
  28.         modifiers &= ~Modifier.FINAL;
  29.         modifiersField.setInt(field, modifiers);
  30.         FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false);
  31.         fa.set(target, value);
  32.     }
  33.  
  34.     private static void blankField(Class<?> enumClass, String fieldName) throws NoSuchFieldException, IllegalAccessException {
  35.         for (Field field : Class.class.getDeclaredFields()) {
  36.             if (field.getName().contains(fieldName)) {
  37.                 AccessibleObject.setAccessible(new Field[] { field }, true);
  38.                 setFailsafeFieldValue(field, enumClass, null);
  39.                 break;
  40.             }
  41.         }
  42.     }
  43.  
  44.     private static void cleanEnumCache(Class<?> enumClass) throws NoSuchFieldException, IllegalAccessException {
  45.         blankField(enumClass, "enumConstantDirectory"); // Sun (Oracle?!?) JDK 1.5/6
  46.         blankField(enumClass, "enumConstants"); // IBM JDK
  47.     }
  48.  
  49.     private static ConstructorAccessor getConstructorAccessor(Class<?> enumClass, Class<?>[] additionalParameterTypes) throws NoSuchMethodException {
  50.         Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2];
  51.         parameterTypes[0] = String.class;
  52.         parameterTypes[1] = int.class;
  53.         System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length);
  54.         return reflectionFactory.newConstructorAccessor(enumClass.getDeclaredConstructor(parameterTypes));
  55.     }
  56.  
  57.     private static Object makeEnum(Class<?> enumClass, String value, int ordinal, Class<?>[] additionalTypes, Object[] additionalValues) throws Exception {
  58.         Object[] parms = new Object[additionalValues.length + 2];
  59.         parms[0] = value;
  60.         parms[1] = Integer.valueOf(ordinal);
  61.         System.arraycopy(additionalValues, 0, parms, 2, additionalValues.length);
  62.         return enumClass.cast(getConstructorAccessor(enumClass, additionalTypes).newInstance(parms));
  63.     }
  64.  
  65.     /**
  66.      * Add an enum instance to the enum class given as argument
  67.      *
  68.      * @param <T>
  69.      *            the type of the enum (implicit)
  70.      * @param enumType
  71.      *            the class of the enum to be modified
  72.      * @param enumName
  73.      *            the name of the new enum instance to be added to the class.
  74.      */
  75.     @SuppressWarnings("unchecked")
  76.     public static <T extends Enum<?>> T addEnum(Class<T> enumType, String enumName, Class[] initClasses, Object... initValues) {
  77.         if (!Enum.class.isAssignableFrom(enumType)) {
  78.             throw new RuntimeException("You're doing it wrong!");
  79.         }
  80.         Field valuesField = null;
  81.         Field[] fields = enumType.getDeclaredFields();
  82.         for (Field field : fields) {
  83.             if (field.getName().contains("$VALUES")) {
  84.                 valuesField = field;
  85.                 break;
  86.             }
  87.         }
  88.         AccessibleObject.setAccessible(new Field[] { valuesField }, true);
  89.         try {
  90.             T[] previousValues = (T[]) valuesField.get(enumType);
  91.             List<T> values = new ArrayList<T>(Arrays.asList(previousValues));
  92.             T newValue = (T) makeEnum(enumType, enumName, values.size(), initClasses, initValues);
  93.            
  94.             // Rarity specific fixes. I might make this more versatile.
  95.             if (newValue instanceof EnumRarity) {
  96.                 boolean flag = false;
  97.                 for (int i = 0; i < values.size(); i++) {
  98.                     EnumRarity rarity = (EnumRarity) values.get(i);
  99.                     if (rarity.rarityName.equalsIgnoreCase(initValues[1].toString())) {
  100.                         for (Field replace : EnumRarity.class.getDeclaredFields()) {
  101.                             replace.setAccessible(true);
  102.                             if (replace.get(rarity) instanceof Integer) {
  103.                                 setFailsafeFieldValue(replace, rarity, initValues[0]);
  104.                                 flag = true;
  105.                             }
  106.                         }
  107.                     }
  108.                 }
  109.                 if (!flag) {
  110.                     values.add(newValue);
  111.                     setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
  112.                 }
  113.             } else {
  114.                 values.add(newValue);
  115.                 setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
  116.             }
  117.             cleanEnumCache(enumType);
  118.            
  119.             // DEBUG: Output the new array, you'll only notice new enums.
  120.             System.out.println(values);
  121.             return newValue;
  122.         }
  123.         catch (Exception e) {
  124.             e.printStackTrace();
  125.         }
  126.         return null;
  127.     }
  128. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment