예제 #1
0
 public static <T> T create(Class<T> clazz, Class<? super T> parent) {
   try {
     ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
     Constructor objDef = parent.getDeclaredConstructor();
     Constructor intConstr = rf.newConstructorForSerialization(clazz, objDef);
     return clazz.cast(intConstr.newInstance());
   } catch (RuntimeException e) {
     throw e;
   } catch (Exception e) {
     throw new IllegalStateException("Cannot create object", e);
   }
 }
  public SunReflectionFactoryInstantiator(final Class type) {

    final ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
    Constructor javaLangObjectConstructor;

    try {
      javaLangObjectConstructor = Object.class.getConstructor((Class[]) null);
    } catch (final NoSuchMethodException e) {
      throw new Error("Cannot find constructor for java.lang.Object!");
    }
    this.mungedConstructor =
        reflectionFactory.newConstructorForSerialization(type, javaLangObjectConstructor);
    this.mungedConstructor.setAccessible(true);
  }
예제 #3
0
  public static void setStaticFinalField(Field field, Object value)
      throws NoSuchFieldException, IllegalAccessException {
    field.setAccessible(true);
    final Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);

    int modifiers = modifiersField.getInt(field);
    modifiers &= ~Modifier.FINAL;
    modifiersField.setInt(field, modifiers);

    final FieldAccessor fieldAccessor = reflectionFactory.newFieldAccessor(field, false);
    fieldAccessor.set(null, value);
  }
예제 #4
0
public class ReflectionUtil {
  private static final ReflectionFactory reflectionFactory =
      ReflectionFactory.getReflectionFactory();

  public static void setStaticFinalField(Field field, Object value)
      throws NoSuchFieldException, IllegalAccessException {
    field.setAccessible(true);
    final Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);

    int modifiers = modifiersField.getInt(field);
    modifiers &= ~Modifier.FINAL;
    modifiersField.setInt(field, modifiers);

    final FieldAccessor fieldAccessor = reflectionFactory.newFieldAccessor(field, false);
    fieldAccessor.set(null, value);
  }

  public static <T> T getPrivateField(Object target, String fieldName, Class<T> fieldType)
      throws Exception {
    Class<?> type = target.getClass();
    Field field = getField(fieldName, type);
    if (!fieldType.isAssignableFrom(field.getType())) {
      throw new IllegalArgumentException(
          "Field type does not match " + field.getType() + " is no subclass of " + "" + fieldType);
    }
    field.setAccessible(true);
    return fieldType.cast(field.get(target));
  }

  private static Field getField(String fieldName, Class<? extends Object> type)
      throws NoSuchFieldException {
    if (type == null) {
      throw new NoSuchFieldException(fieldName);
    }
    try {
      Field field = type.getDeclaredField(fieldName);
      if (field != null) {
        return field;
      }
    } catch (NoSuchFieldError e) {
    } catch (NoSuchFieldException e) {
    }
    return getField(fieldName, type.getSuperclass());
  }
}
 /**
  * Constructs an instance even if the corresponding class doesn't have a default constructor.
  *
  * @param cls A class to instantiate.
  * @return A newly allocated instance.
  * @throws InvocationTargetException Something went wrong while instantiating.
  * @throws IllegalAccessException Something went wrong while instantiating.
  * @throws InstantiationException Something went wrong while instantiating.
  */
 static Object forceNewInstance(Class cls)
     throws InvocationTargetException, IllegalAccessException, InstantiationException {
   return ReflectionFactory.getReflectionFactory()
       .newConstructorForSerialization(cls, OBJECT_CONSTRUCTOR)
       .newInstance();
 }
예제 #6
0
 /*
  * Bootstrapping protocol between java.lang and java.lang.reflect
  *  packages
  */
 static {
   sun.reflect.ReflectionFactory factory =
       (sun.reflect.ReflectionFactory)
           AccessController.doPrivileged(new ReflectionFactory.GetReflectionFactoryAction());
   factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
 }