1 2 3 4 5 | <validator name="int" classname="org.i505.validator.MyTypeValidator" method="validateInt" methodParams="java.lang.Object,org.apache.commons.validator.Field" msg="errors.int"/> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | // Add these two Objects to the resources since they reference // the current validator action and field hResources.put(VALIDATOR_ACTION_KEY, va); hResources.put(FIELD_KEY, field); Class c = getClassLoader().loadClass(va.getClassname()); List lParams = va.getMethodParamsList(); int size = lParams.size(); int beanIndexPos = -1; int fieldIndexPos = -1; Class[] paramClass = new Class[size]; Object[] paramValue = new Object[size]; for (int x = 0; x < size; x++) { String paramKey = (String) lParams.get(x); if (BEAN_KEY.equals(paramKey)) { beanIndexPos = x; } if (FIELD_KEY.equals(paramKey)) { fieldIndexPos = x; } // There were problems calling getClass on paramValue[] paramClass[x] = getClassLoader().loadClass(paramKey); paramValue[x] = hResources.get(paramKey); } Method m = c.getMethod(va.getMethod(), paramClass); // If the method is static we don't need an instance of the class // to call the method. If it isn't, we do. if (!Modifier.isStatic(m.getModifiers())) { try { if (va.getClassnameInstance() == null) { va.setClassnameInstance(c.newInstance()); } } catch (Exception ex) { log.error( "Couldn't load instance " + "of class " + va.getClassname() + ". " + ex.getMessage()); } } Object result = null; if (field.isIndexed()) { Object oIndexed = PropertyUtils.getProperty( hResources.get(BEAN_KEY), field.getIndexedListProperty()); Object indexedList[] = new Object[0]; if (oIndexed instanceof Collection) { indexedList = ((Collection) oIndexed).toArray(); } else if (oIndexed.getClass().isArray()) { indexedList = (Object[]) oIndexed; } // Set current iteration object to the parameter array paramValue[beanIndexPos] = indexedList[pos]; // Set field clone with the key modified to represent // the current field Field indexedField = (Field) field.clone(); indexedField.setKey( ValidatorUtil.replace( indexedField.getKey(), Field.TOKEN_INDEXED, "[" + pos + "]")); paramValue[fieldIndexPos] = indexedField; result = m.invoke(va.getClassnameInstance(), paramValue); results.add(field, va.getName(), isValid(result), result); if (!isValid(result)) { return false; } } else { result = m.invoke(va.getClassnameInstance(), paramValue); results.add(field, va.getName(), isValid(result), result); if (!isValid(result)) { return false; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |