• 反射详解四


    • 反射的get方法
      /*
            getter方法
            o:要操作类的对象
            args:属性名
         */
        public static <T> T getXxx(T o,String args) throws NoSuchFieldException {
            Class cls = o.getClass();
            //判断该属性是否存在
            Field field = field = cls.getDeclaredField(args);
            if(field == null){
                field = cls.getField(args);
            }
            if(field == null){
                return null;
            }
    
    
            String fieldName = "get"+args.substring(0,1).toUpperCase()+(args.length()>1?args.substring(1):"");
            Method method = null;
            try {
                method = cls.getMethod(fieldName);
                return (T)method.invoke(o);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return null;
        }
    •  反射的set方法
    /*
            setter方法
            o:要操作类的对象
            args:属性名
            attributeValue:属性值
         */
        public static void setXxx(Object o,String args,Object attributeValue){
            Class cls = o.getClass();
            //判断该属性是否存在
            Field field = null;
            try {
                field = cls.getDeclaredField(args);
                if(field == null){
                    field = cls.getField(args);
                }
                if(field == null){
                    return;
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
    
            String fieldName = "set"+args.substring(0,1).toUpperCase()+(args.length()>1?args.substring(1):"");
            Method method = null;
            try {
                method = cls.getMethod(fieldName,attributeValue.getClass());
                method.invoke(o,attributeValue);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    •  反射的相关方法
    getName():获得类的完整名字。  
    newInstance():通过类的不带参数的构造方法创建这个类的一个对象。
    
    getFields():获得类的public类型的属性。  
    getDeclaredFields():获得类的所有属性。
    
    getMethods():获得类的public类型的方法。  
    getDeclaredMethods():获得类的所有方法。  
    getMethod(String name, Class[] parameterTypes):获得类的特定方法。
    
    getModifiers()和Modifier.toString():获得属修饰符,例如private,public,static等  
    getReturnType():获得方法的返回类型  
    getParameterTypes():获得方法的参数类型
    
    getConstructors():获得类的public类型的构造方法。  
    getConstructor(Class[] parameterTypes):获得类的特定构造方法。
    
    getSuperclass():获取某类的父类  
    getInterfaces():获取某类实现的接口
    • 获取权限名
    Modifier.toString(field.getModifiers())
    Modifier.toString(method.getModifiers())

     getFields()            只能获取public的字段,包括父类的。
    getDeclaredFields()    只能获取自己声明的各种字段,包括public,protected,private。
    getFields()和 getDeclaredFields(),返回的都是Field对象,获取名称直接field.getName(), 但是属性值则是field.get(Object),这个object是该field所属的!!!

    故乡明
  • 相关阅读:
    CSS相关(2)
    CSS相关(1)
    Git(待补充)
    Jenkins学习(1)-什么是Jenkins
    Yaml(待补充)
    Tomcat(待补充)
    Json(待补充)
    Java语言学习(8)-Java中的异常处理
    Java语言学习(7)-Java中IO
    Java语言学习(6)-Java中封装类(正则表达式操作类)
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14151527.html
Copyright © 2020-2023  润新知