Ejemplo n.º 1
0
 public static void setFinalStaticField(
     Class classWhichContainsField, String fieldName, Object newValue) {
   try {
     Field field = classWhichContainsField.getDeclaredField(fieldName);
     setFinalStaticField(field, newValue);
   } catch (NoSuchFieldException e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 2
0
 public static Object newInstanceOf(String className) {
   try {
     Class<?> clazz = Class.forName(className);
     if (clazz != null) {
       return newInstanceOf(clazz);
     }
   } catch (ClassNotFoundException e) {
   }
   return null;
 }
Ejemplo n.º 3
0
    public static void setFinalStaticField(
        Class classWhichContainsField, String fieldName, Object newValue) {
      try {
        Field field = classWhichContainsField.getField(fieldName);
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
      } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
      }
    }