Vag*_*aus 6 android constructor objectinstantiation
Android有没有办法在不调用任何构造函数的情况下实例化对象?
在Java中,Sun有sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization(),在.Net中我们有System.Runtime.Serialization.FormatterServices.GetUninitializedObject()但我在Android平台上找不到类似的东西.
在查看Android源代码后,我们找到了一种通过使用ObjectInputStream#newInstance()静态方法来实现此目的的方法.
private Object newInstanceSkippingConstructor(final Class clazz) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Method newInstance = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
newInstance.setAccessible(true);
return newInstance.invoke(null, clazz, Object.class);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1150 次 |
| 最近记录: |