public static Object createPrivateInnerClass(String className, Object... parameter) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class clazz = Class.forName(className);
Constructor constructor = clazz.getDeclaredConstructors()[0];
constructor.setAccessible(true);
Object object = constructor.newInstance(parameter);
return object;
}
/**
* 获取私有成员变量的值
* @param filedName
* @return
*/
public static Object getStaticField(String className, String filedName) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
Class clazz = Class.forName(className);
return getStaticField(clazz, filedName);
}
public static Object getStaticField(Class clazz, String filedName) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(filedName);
field.setAccessible(true);
return field.get(null);
}
public static Object getStaticFieldAnyway(Class clazz, String filedName) throws IllegalAccessException {
Field field = null;
try {
field = clazz.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
field = getPrivateFieldAnyway(clazz.getSuperclass(), filedName);
}
field.setAccessible(true);
return field.get(null);
}
public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(filedName);
field.setAccessible(true);
return field.get(instance);
}
public static Object getSuperPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getSuperclass().getDeclaredField(filedName);
field.setAccessible(true);
return field.get(instance);
}
public static Object getPrivateFieldAnyway(Object instance, String filedName) throws IllegalAccessException {
Field field = null;
try {
field = instance.getClass().getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
field = getPrivateFieldAnyway(instance.getClass().getSuperclass(), filedName);
}
field.setAccessible(true);
return field.get(instance);
}
public static Field getPrivateFieldAnyway(Class clazz, String filedName) throws IllegalAccessException {
Field field = null;
try {
field = clazz.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
field = getPrivateFieldAnyway(clazz.getSuperclass(), filedName);
}
return field;
}
/**
* 设置私有成员的值
* @param fieldName
* @param value
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static void setStaticField(Class clazz, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(null, value);
}
public static void setStaticFieldAnyway(Class clazz, String fieldName, Object value) throws IllegalAccessException {
Field field = null;
try {
field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = getPrivateFieldAnyway(clazz.getSuperclass(), fieldName);
}
field.setAccessible(true);
field.set(null, value);
}
public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
}
public static void setPrivateFieldAnyway(Object instance, String fieldName, Object value) throws IllegalAccessException {
Field field = null;
try {
field = instance.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = getPrivateFieldAnyway(instance.getClass().getSuperclass(), fieldName);
}
field.setAccessible(true);
field.set(instance, value);
}
public static void setSuperPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
}
public static <O> O newInstance(Class<?> cls){
Object obj;
try {
obj = cls.newInstance();
} catch (Exception e) {
HmiPlugins.getInstance().getErrorHandler().handleError(e);
throw new ClassCastException("getProxy class donot newInstance ");
}
return (O) obj;
}
/**
*
*
* 如public UserDao extends HibernateDao<User,Long>
*
* @param clazz clazz The class to introspect
* @param index the Index of the generic ddeclaration,start from 0.
* @return the index generic declaration, or Object.class if cannot be determined
*/
public static Class<?> getSuperClassGenricType(final Class<?> clazz, final int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
LogUtil.d(clazz.getSimpleName() , "'s superclass not ParameterizedType");
return Object.class;
}
Type[] types = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= types.length || index < 0) {
LogUtil.d("Index: " , index , ", Size of " , clazz.getSimpleName() , "'s Parameterized Type: "
, types.length);
return Object.class;
}
if (!(types[index] instanceof Class)) {
LogUtil.d(clazz.getSimpleName() ," not set the actual class on superclass generic parameter");
return Object.class;
}
return (Class<?>) types[index];
}
public static void setSuperPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
}
public static Object invokeStaticMethod(String className, String methodName, Class[] classes, Object[] objects) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
Class clazz = Class.forName(className);
Method method = clazz.getDeclaredMethod(methodName, classes);
method.setAccessible(true);
return method.invoke(null, objects);
}
public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, Object[] objects) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = instance.getClass().getDeclaredMethod(methodName, classes);
method.setAccessible(true);
return method.invoke(instance, objects);
}
public static Object invokeSuperPrivateMethod(Object instance, String methodName, Class[] classes, Object[] objects) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = instance.getClass().getSuperclass().getDeclaredMethod(methodName, classes);
method.setAccessible(true);
return method.invoke(instance, objects);
}
public static Object invokePrivateMethodAnyway(Object instance, String methodName, Class[] classes, Object[] objects) throws InvocationTargetException, IllegalAccessException {
Object result = null;
Method method = null;
try {
method = instance.getClass().getDeclaredMethod(methodName, classes);
} catch (NoSuchMethodException e) {
method = getPrivateMethodAnyway(instance.getClass().getSuperclass(), methodName, classes, objects);
}
method.setAccessible(true);
result = method.invoke(instance, objects);
return result;
}
public static Method getPrivateMethodAnyway(Class clazz, String methodName, Class[] classes, Object[] objects) throws InvocationTargetException, IllegalAccessException {
Method method = null;
try {
method = clazz.getDeclaredMethod(methodName, classes);
} catch (NoSuchMethodException e) {
method = getPrivateMethodAnyway(clazz.getSuperclass(), methodName, classes, objects);
}
return method;
}
public static Field getFiled(Object instance, String name) throws NoSuchFieldException {
Field field = instance.getClass().getDeclaredField(name);
field.setAccessible(true);
return field;
}
public static Field getFiled(Class clazz, String name) throws NoSuchFieldException {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return field;
}
// 反射调用删除final修饰符
public static void removeFieldFinalModifier(Field field) {
try {
// DoallJREsimplementFieldwithaprivateivarcalled"modifiers"?
//java写法
// final Field modifiersField=Field.class.getDeclaredField("modifiers");
//android写法
final Field modifiersField= Field.class.getDeclaredField("accessFlags");
final boolean doForceAccess = !modifiersField.isAccessible();
if (doForceAccess) {
modifiersField.setAccessible(true);
}
try {
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
modifiersField.setInt(field, field.getModifiers() | Modifier.PUBLIC);
} finally {
System.out.println("shizhikang - final " + Modifier.isFinal(field.getModifiers()));
if (doForceAccess) {
modifiersField.setAccessible(false);
}
System.out.println("shizhikang - final modifiersField:" + modifiersField);
}
} catch (final NoSuchFieldException ignored) {
ignored.printStackTrace();
// Thefieldclasscontainsalwaysamodifiersfield
} catch (final IllegalAccessException ignored) {
// Themodifiersfieldismadeaccessible
ignored.printStackTrace();
}
}