• 003 对象属性Property


    public final class PropertyNamer {
    
      private PropertyNamer() {
        // Prevent Instantiation of Static Class
      }
    
      public static String methodToProperty(String name) {
        if (name.startsWith("is")) {
          name = name.substring(2);
        } else if (name.startsWith("get") || name.startsWith("set")) {
          name = name.substring(3);
        } else {
          throw new ReflectionException("Error parsing property name '" + name + "'.  Didn't start with 'is', 'get' or 'set'.");
        }
    
        if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
          name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
        }
    
        return name;
      }
    
      public static boolean isProperty(String name) {
        return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
      }
    
      public static boolean isGetter(String name) {
        return name.startsWith("get") || name.startsWith("is");
      }
    
      public static boolean isSetter(String name) {
        return name.startsWith("set");
      }
    
    }
    

     上面的PropertyNamer实际上就是一个工具类,通过方法名获取对应的属性.

    实际上,就是我们的javaBean的方式,以此来推断属性的.

    public final class PropertyCopier {
    
      private PropertyCopier() {
        // Prevent Instantiation of Static Class
      }
    
      public static void copyBeanProperties(Class<?> type, Object sourceBean, Object destinationBean) {
        Class<?> parent = type;
        while (parent != null) {
          final Field[] fields = parent.getDeclaredFields();
          for (Field field : fields) {
            try {
              try {
                field.set(destinationBean, field.get(sourceBean));
              } catch (IllegalAccessException e) {
                if (Reflector.canControlMemberAccessible()) {
                  field.setAccessible(true);
                  field.set(destinationBean, field.get(sourceBean));
                } else {
                  throw e;
                }
              }
            } catch (Exception e) {
              // Nothing useful to do, will only fail on final fields, which will be ignored.
            }
          }
          parent = parent.getSuperclass();
        }
      }
    
    }
    

     上面为属性拷贝工具类,实际上就是我们之前使用的BeanCopyUtils的一个简单实现.

     

     

  • 相关阅读:
    Java数据类型+练习
    在vue中使用echars不能自适应的解决方法
    使用js将Unix时间戳转换为普通时间
    vue-router2.0二级路由的简单使用
    vue父组件向子组件传递参数
    vue子组件向父组件传递参数的基本方式
    vuex----mutation和action的基本使用
    vuex----------state的基础用法
    数组判断重复
    在vue项目中快速使用element UI
  • 原文地址:https://www.cnblogs.com/trekxu/p/12836014.html
Copyright © 2020-2023  润新知