This Is A FineDay

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  93 随笔 :: 0 文章 :: 69 评论 :: 0 Trackbacks

JavaBean中Property命名不规范出现了问题:
public class StockGuessInfo {

 private String SN;

 private String A_NUM;
 public String getA_NUM() {
  return A_NUM;
 }

 public void setA_NUM(String a_num) {
  A_NUM = a_num;
 }


这段代码用Apache的Common中的Util反射就得不到预期的结果
<bean:write name="stockGuessActionForm" property="A_NUM" />   出现错误
<bean:write name="stockGuessActionForm" property="a_NUM" />   可以得到结果

//Apache的Common中的Util要求javaBean中Property变量要求两个字母要大写都大写,小写都小写,否则反射失败
NAME   成功
Name   失败
NaME   失败




==============

    public static BeanInfo getBeanInfo(Class<?> beanClass)
 throws IntrospectionException
    {
 if (!ReflectUtil.isPackageAccessible(beanClass)) {
     return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
 }
...................
    }  
    private BeanInfo getBeanInfo() throws IntrospectionException {
 ..........

 PropertyDescriptor pds[] = getTargetPropertyInfo();

 ......... 
    }

 private PropertyDescriptor[] getTargetPropertyInfo() {
 ..........
 if{
 ............
 }else {

     // Apply some reflection to the current class.

     // First get an array of all the public methods at this level
     Method methodList[] = getPublicDeclaredMethods(beanClass);

     // Now analyze each method.
     for (int i = 0; i < methodList.length; i++) {
         Method method = methodList[i];
  if (method == null) {
      continue;
  }
         // skip static methods.
  int mods = method.getModifiers();
  if (Modifier.isStatic(mods)) {
      continue;
  }
         String name = method.getName();
         Class argTypes[] = method.getParameterTypes();
         Class resultType = method.getReturnType();
  int argCount = argTypes.length;
  PropertyDescriptor pd = null;

  if (name.length() <= 3 && !name.startsWith(IS_PREFIX)) {
      // Optimization. Don't bother with invalid propertyNames.
      continue;
  }

  try {

             if (argCount == 0) {
          if (name.startsWith(GET_PREFIX)) {
              // Simple getter
============================文问题就出现在这里 decapitalize(name.substring(3)=========

    pd = new PropertyDescriptor(decapitalize(name.substring(3)),
      method, null);      
                 } else if (resultType == boolean.class && name.startsWith(IS_PREFIX)) {
              // Boolean getter
                     pd = new PropertyDescriptor(decapitalize(name.substring(2)),
      method, null);
          }
............................................
   }

   }

 public static String decapitalize(String name) {
 if (name == null || name.length() == 0) {
     return name;
 }
 if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
   Character.isUpperCase(name.charAt(0))){
     return name;
 }
 char chars[] = name.toCharArray();
 chars[0] = Character.toLowerCase(chars[0]);
 return new String(chars);
    }
// pd = new PropertyDescriptor(decapitalize(name.substring(3)),
//就是在这里了decapitalize(name)中要求Property变量要求两个字母要大写都大写,小写都小写,否则反射失败
,否则反射失败

posted on 2007-03-16 16:20 Peter Pan 阅读(724) 评论(0)  编辑  收藏 所属分类: J2EE

只有注册用户登录后才能发表评论。


网站导航: