beanutil.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 654 行 · 第 1/2 页

JAVA
654
字号
        method = getSetMethod(info.getBeanDescriptor().getBeanClass(),			  propertyName);    if (method != null)      return method;        PropertyDescriptor []pds = info.getPropertyDescriptors();    Method bestMethod = null;    for (int i = 0; i < pds.length; i++) {      if (pds[i].getName().equals(propertyName)	  && pds[i].getWriteMethod() != null) {        method = pds[i].getWriteMethod();        if (method.getParameterTypes()[0].equals(String.class))          return method;	else	  bestMethod = method;      }    }    if (method != null)      return method;    return null;  }    /**   * Returns a set method matching the property name.   */  public static Method getSetMethod(Class cl, String propertyName)  {    Method method = getSetMethod(cl, propertyName, false);    if (method != null)      return method;    return getSetMethod(cl, propertyName, true);  }  /**   * Returns a set method matching the property name.   */  public static Method getSetMethod(Class cl,                                    String propertyName,                                    boolean ignoreCase)  {    String setName = "set" + propertyNameToMethodName(propertyName);    Method bestMethod = null;        for (Class ptrCl = cl; ptrCl != null; ptrCl = ptrCl.getSuperclass()) {      Method method = getSetMethod(ptrCl.getMethods(),                                   setName,                                   ignoreCase);      if (method != null && method.getParameterTypes()[0].equals(String.class))        return method;      else if (method != null)        bestMethod = method;    }    if (bestMethod != null)      return bestMethod;    Class []interfaces = cl.getInterfaces();    for (int i = 0; i < interfaces.length; i++) {      Method method = getSetMethod(interfaces[i].getMethods(),                                   setName,                                   ignoreCase);      if (method != null && method.getParameterTypes()[0].equals(String.class))        return method;      else if (method != null)        bestMethod = method;    }    if (bestMethod != null)      return bestMethod;    return null;  }  /**   * Finds the matching set method   *   * @param method the methods for the class   * @param setName the method name   */  private static Method getSetMethod(Method []methods,                                     String setName,                                     boolean ignoreCase)  {    Method bestMethod = null;        for (int i = 0; i < methods.length; i++) {      Method method = methods[i];      // The method name must match      if (! ignoreCase && ! method.getName().equals(setName))        continue;            // The method name must match      if (ignoreCase && ! method.getName().equalsIgnoreCase(setName))        continue;            // The method must be public      if (! Modifier.isPublic(method.getModifiers()))        continue;      // It must be in a public class or interface      if (! Modifier.isPublic(method.getDeclaringClass().getModifiers()))        continue;      // It must have a single parameter      if (method.getParameterTypes().length != 1)        continue;            // It must return void      if (! method.getReturnType().equals(void.class))	continue;      Class paramType = method.getParameterTypes()[0];            if (paramType.equals(String.class))	return method;      else if (bestMethod == null)	bestMethod = method;      else if (paramType.getName().compareTo(bestMethod.getParameterTypes()[0].getName()) < 0)	bestMethod = method;    }    return bestMethod;  }    /**   * Returns a set method matching the property name.   */  public static Method getGetMethod(BeanInfo info, String propertyName)  {    PropertyDescriptor []pds = info.getPropertyDescriptors();    for (int i = 0; i < pds.length; i++) {      if (pds[i].getName().equals(propertyName) &&          pds[i].getReadMethod() != null) {	if (! Modifier.isPublic(pds[i].getReadMethod().getDeclaringClass().getModifiers())) {	  try {	    pds[i].getReadMethod().setAccessible(true);	  } catch (Throwable e) {	    continue;	  }	}	        return pds[i].getReadMethod();    }    }    return getGetMethod(info.getBeanDescriptor().getBeanClass(), propertyName);  }  /**   * Returns a get method matching the property name.   */  public static Method getGetMethod(Class cl, String propertyName)  {    Method method = getGetMethod(cl, propertyName, false);    if (method != null)      return method;        return getGetMethod(cl, propertyName, true);  }  /**   * Returns a get method matching the property name.   */  public static Method getGetMethod(Class cl,                                    String propertyName,                                    boolean ignoreCase)  {    String getName = "get" + propertyNameToMethodName(propertyName);    String isName = "is" + propertyNameToMethodName(propertyName);    for (Class ptrCl = cl; ptrCl != null; ptrCl = ptrCl.getSuperclass()) {      Method method = getGetMethod(ptrCl.getDeclaredMethods(), getName,                                   isName, ignoreCase);      if (method != null)        return method;      Class []interfaces = ptrCl.getInterfaces();      for (int i = 0; i < interfaces.length; i++) {	method = getGetMethod(interfaces[i].getDeclaredMethods(),			      getName, isName, ignoreCase);	if (method != null)	  return method;      }    }    return null;  }  /**   * Finds the matching set method   *   * @param method the methods for the class   * @param setName the method name   */  private static Method getGetMethod(Method []methods,                                     String getName,                                     String isName,                                     boolean ignoreCase)  {    for (int i = 0; i < methods.length; i++) {      Method method = methods[i];      // The method must be public      if (! Modifier.isPublic(method.getModifiers()))        continue;      // It must be in a public class or interface      if (! Modifier.isPublic(method.getDeclaringClass().getModifiers()))        continue;      // It must have no parameters      if (method.getParameterTypes().length != 0)        continue;            // It must not return void      if (method.getReturnType().equals(void.class))        continue;      // If it matches the get name, it's the right method      else if (! ignoreCase && methods[i].getName().equals(getName))        return methods[i];            // If it matches the get name, it's the right method      else if (ignoreCase && methods[i].getName().equalsIgnoreCase(getName))        return methods[i];      // The is methods must return boolean      else if (! methods[i].getReturnType().equals(boolean.class))        continue;            // If it matches the is name, it must return boolean      else if (! ignoreCase && methods[i].getName().equals(isName))        return methods[i];            // If it matches the is name, it must return boolean      else if (ignoreCase && methods[i].getName().equalsIgnoreCase(isName))        return methods[i];    }    return null;  }  /**   * Converts a user's property name to a bean method name.   *   * @param propertyName the user property name   * @return the equivalent bean method name   */  public static String propertyNameToMethodName(String propertyName)  {    char ch = propertyName.charAt(0);    if (Character.isLowerCase(ch))      propertyName = Character.toUpperCase(ch) + propertyName.substring(1);    return propertyName;  }  /**   * Converts a user's property name to a bean method name.   *   * @param methodName the method name   * @return the equivalent property name   */  public static String methodNameToPropertyName(BeanInfo info,                                                String methodName)  {    PropertyDescriptor []pds = info.getPropertyDescriptors();    for (int i = 0; i < pds.length; i++) {      if (pds[i].getReadMethod() != null &&          pds[i].getReadMethod().getName().equals(methodName))        return pds[i].getName();      if (pds[i].getWriteMethod() != null &&          pds[i].getWriteMethod().getName().equals(methodName))        return pds[i].getName();    }    return methodNameToPropertyName(methodName);  }  /**   * Converts a user's property name to a bean method name.   *   * @param methodName the method name   * @return the equivalent property name   */  public static String methodNameToPropertyName(String methodName)  {    if (methodName.startsWith("get"))      methodName = methodName.substring(3);    else if (methodName.startsWith("set"))      methodName = methodName.substring(3);    else if (methodName.startsWith("is"))      methodName = methodName.substring(2);    if (methodName.length() == 0)      return null;    char ch = methodName.charAt(0);    if (Character.isUpperCase(ch) &&        (methodName.length() == 1 ||         ! Character.isUpperCase(methodName.charAt(1)))) {      methodName = Character.toLowerCase(ch) + methodName.substring(1);    }    return methodName;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?