Please share your thoughts to resolve following problem statement.
Objective: I am trying to develop an Utility in Java 1.6 using reflection. This Utility will access each attribute, non-nullable attribute(s) will be passed through a specific validation routine.
Problem Statement: I am able to access attributes of class ParentBean, ClassB and ClassC. But I am unable to access attributes of ClassD, ClassE. My code goes here.
public class ParentBean extends ActionForm {
private String fullName;
private ClassB classB;
private ClassC classC;
// getters and setters
}
public class ClassB extends ActionForm {
private long classBid;
private Collection hobby;
private ClassD classD;
private ClassE classE;
// getters and setters
}
public class ClassC extends ActionForm {
private long classCid;
// getters and setters
}
public class ClassD extends ActionForm {
private long classDid;
private String addr1;
// getters and setters
}
public class ClassE extends ActionForm {
private long classEid;
private String contactVal;
private ClassF classF;
// getters and setters
}
public class ClassF extends ActionForm {
private long classFid;
private String attribute;
// getters and setters
}
public class Validate {
public static void main(String[] args) {
ParentBean objParentBean = new ParentBean();
objParentBean.setFullName("MALCOM ANDRWES");
ClassB objClassB = new ClassB();
Collection hobby = new ArrayList();
hobby.add("CRICKET");
objClassB.addHobby(hobby);
ClassD objClassD = new ClassD();
objClassD.setAddr1("123 MAIN STREET");
objClassB.setClassD(objClassD);
ClassE objClassE = new ClassE();
objClassE.setContactVal("97862082202");
ClassF objClassF = new ClassF();
objClassF.setAttribute("XXX");
objClassE.setClassF(objClassF);
objClassB.setClassE(objClassE);
// Call Validation Routine
performValidation(actionFormObj);
}
private static void performValidation(ActionForm form) {
Object formObj = form;
List<Field> formFields = getAllFields(formObj.getClass());
List<Field> childFormAttributeLst = validateForm(formObj, formFields);
if (!childFormAttributeLst.isEmpty()) {
validateChildForm(childFormAttributeLst, formObj, form);
}
}
private static List<Field> validateForm(Object formObj,List<Field> formFields) {
List<Field> childFormAttributeLst = new ArrayList<Field>();
for (Field field : formFields) {
if (isPrimitive(field.getType())) {
validate(formObj, field);
} else if (isCollection(field.getType())) {
isCollectionNotNull(formObj, field);
} else {
childFormAttributeLst.add(field);
}
}
return childFormAttributeLst;
}
private static void validateChildForm(List<Field> childFormAttributeLst,Object formObj, ActionForm form) {
List<Field> childFormLst = new ArrayList<Field>();
Method m = null;
Object chldFormObj = null;
for (Field field : childFormAttributeLst) {
try {
StringBuffer fieldName = getFieldName(field);
m = formObj.getClass().getMethod(fieldName.toString(),
new Class<?>[] {});
try {
chldFormObj = m.invoke(formObj, null);
List<Field> formFields = getAllFields(chldFormObj.getClass());
childFormLst = validateForm(chldFormObj, formFields);
// Accessing inner Form bean attributes defined
if(!childFormLst.isEmpty()){
for (Field field2 : childFormLst) {
try{
StringBuffer fieldName1 = getFieldName(field2);
m = field.getType().getMethod(fieldName1.toString(),
new Class<?>[] {});
try{
String invokingClass = field.getType().getName();
// Below line throws error: java.lang.IllegalArgumentException: object is not an instance of declaring class
Object formObj2 = m.invoke(formObj, null);
} catch (NullPointerException npe) {
npe.printStackTrace();
}
} catch(Exception e){
e.printStackTrace();
}
}
}
} catch (NullPointerException npe) {
npe.printStackTrace();
}
} // catch Blocks
}
}
private static StringBuffer getFieldName(Field field) {
StringBuffer fieldName = new StringBuffer("get"
+ field.getName().substring(0, 1).toUpperCase()
+ field.getName().substring(1, field.getName().length()));
return fieldName;
}
private static List<Field> getAllFields(Class<?> type) {
List<Field> fields = new ArrayList<Field>();
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
if (!(Object.class == c || ActionForm.class == c)) {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
}
}
return fields;
}
private static boolean isPrimitive(Class<?> typeClass) {
if (typeClass == String.class
|| (typeClass == Integer.class || typeClass == Integer.TYPE)
|| typeClass == Timestamp.class
|| (typeClass == Boolean.class || typeClass == Boolean.TYPE)
|| (typeClass == Double.class || typeClass == double.class)
|| (typeClass == Long.class || typeClass == long.class)
|| (typeClass == Float.class || typeClass == float.class)
|| typeClass == java.util.Date.class
|| (typeClass == Character.class || typeClass == char.class)
|| (typeClass == Byte.class || typeClass == byte.class)) {
return true;
} else {
return false;
}
}
private static void validate(Object formObj, Field field) {
try {
field.setAccessible(true);
Object obj = field.get(formObj);
if (null != obj) {
// Perform validation
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static boolean isCollection(Class<?> typeClass) {
if (Collection.class.isAssignableFrom(typeClass)
|| Map.class.isAssignableFrom(typeClass)) {
return true;
} else {
return false;
}
}
private static void isCollectionNotNull(Object formObj, Field field) {
field.setAccessible(true);
try {
if (Collection.class.isAssignableFrom(field.getType())) {
validateCollection(formObj, field);
} else if (Map.class.isAssignableFrom(field.getType())) {
validateMap(formObj, field);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@SuppressWarnings("rawtypes")
private static void validateMap(Object formObj, Field field)
throws IllegalAccessException {
Map map = (Map) field.get(formObj);
if (!(null == map || map.isEmpty())) {
Set setOfKeys = map.keySet();
for (Object object : setOfKeys) {
// perform validation
}
}
}
@SuppressWarnings("rawtypes")
private static void validateCollection(Object formObj, Field field)
throws IllegalAccessException {
Collection coll = (Collection) field.get(formObj);
if (!(null == coll || coll.isEmpty())) {
for (Object object : coll) {
// perform validation
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire