This Is A FineDay

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            93 隨筆 :: 0 文章 :: 69 評(píng)論 :: 0 Trackbacks

          JavaBean中Property命名不規(guī)范出現(xiàn)了問(wèn)題:
          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反射就得不到預(yù)期的結(jié)果
          <bean:write name="stockGuessActionForm" property="A_NUM" />?? 出現(xiàn)錯(cuò)誤
          <bean:write name="stockGuessActionForm" property="a_NUM" />?? 可以得到結(jié)果

          //Apache的Common中的Util要求javaBean中Property變量要求兩個(gè)字母要大寫都大寫,小寫都小寫,否則反射失敗
          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
          ============================文問(wèn)題就出現(xiàn)在這里 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變量要求兩個(gè)字母要大寫都大寫,小寫都小寫,否則反射失敗
          ,否則反射失敗

          posted on 2007-03-16 16:20 Peter Pan 閱讀(733) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 勐海县| 长寿区| 神农架林区| 甘肃省| 安化县| 祁门县| 北川| 定南县| 宜章县| 隆安县| 镇江市| 大新县| 金华市| 杭州市| 宁陕县| 博罗县| 河东区| 甘德县| 富阳市| 宁强县| 交城县| 六枝特区| 公主岭市| 布尔津县| 资阳市| 静宁县| 漳州市| 宜宾县| 泗阳县| 霍山县| 新干县| 浦北县| 南川市| 扶风县| 绥德县| 疏勒县| 荃湾区| 延长县| 柘城县| 莲花县| 新巴尔虎右旗|