反射工具类

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值