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); }