zoukankan      html  css  js  c++  java
  • 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的一个简单实现.

     

     

  • 相关阅读:
    IIS是如何处理ASP.NET请求的
    c# Socket通讯中关于粘包,半包的处理,加分割符
    windows2008(64位)下iis7.5中的url伪静态化重写(urlrewrite)
    C#微信公众号/订阅号开发 接口源码
    C#线程池多线程Socket通讯 服务器端和客户端示例
    百度地图JS调用示例
    c# 图片转二进制/字符串 二进制/字符串反转成图片
    电商项目面试总结
    96. Unique Binary Search Trees
    92.Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/trekxu/p/12836014.html
Copyright © 2011-2022 走看看