利用Java反射 递归创建对象

最近同事问到一个涉及到Java反射的问题,整理下来如下:

 

需求:给定父对象,和map关系如下:

 

  MyObject1 myObject1 = new MyObject1();
  Map<String, Object> resultMap = new HashMap<String,Object>();
  resultMap.put("myObject1.myobject2.mysub1.name", "sub1Name");
  resultMap.put("myObject1.myobject2.mysub1.password", "111111");
  resultMap.put("myObject1.myobject2.mysub1.mobile", "13900000000");
  resultMap.put("myObject1.myobject2.mysub2.name", "sub2Name");
  resultMap.put("myObject1.myobject2.name", "object2");
  
  myObject1 = (MyObject1)ChaletUtils.getParentObject(resultMap,myObject1);
  System.out.println(myObject1.getMyobject2().getMysub1().getName());
  System.out.println(myObject1.getMyobject2().getMysub1().getPassword());
  System.out.println(myObject1.getMyobject2().getMysub1().getMobile());
  System.out.println(myObject1.getMyobject2().getMysub2().getName());
  System.out.println(myObject1.getMyobject2().getName());

 

写一个util方法,返回对象myObject1,同时上面的所有子对象如果没有则自动创建 

 

util方法如下:

 

public static Object getParentObject(Map<String,Object> resultMap, Object myObject1){
  String classMap = "";
  Iterator<String> ite = resultMap.keySet().iterator();
  while( ite.hasNext() ){
   classMap = ite.next();
   String value = (String)resultMap.get(classMap);
   classMap = classMap.replace(".", ",");
   String[] classes = classMap.split(",");
   try {
    Class temp = myObject1.getClass();
    Method[] methods = temp.getDeclaredMethods();
    
    Object[] subArray = new Object[classes.length-1];
    
    for( int i = 0; i < subArray.length; i++ ){
     if( i != subArray.length-1 ){
      subArray[i] = null;
     }
     if( i != 0 ){
      methods = subArray[i-1].getClass().getDeclaredMethods();
     }
     for( Method method : methods ){
      if( method.getName().startsWith("set") && method.getParameterTypes().length > 0 && method.getName().substring(3).equalsIgnoreCase(classes[i+1]) ){
       if( i != subArray.length -1 ){
        subArray[i] = Class.forName(method.getParameterTypes()[0].getName()).newInstance();
       }
       if( i == 0 ){
        Method getterMethod = myObject1.getClass().getMethod("get"+method.getName().substring(3), null);
        if( getterMethod.invoke(myObject1, null) != null ){
         subArray[i] = getterMethod.invoke(myObject1, null);
        }
        method.invoke(myObject1, subArray[i]);
       }else if( i == subArray.length -1  ){
        method.invoke(subArray[i-1], value);
       }else{
        Method getterMethod = subArray[i-1].getClass().getMethod("get"+method.getName().substring(3), null);
        if( getterMethod.invoke(subArray[i-1], null) != null ){
         subArray[i] = getterMethod.invoke(subArray[i-1], null);
        }
        method.invoke(subArray[i-1], subArray[i]);
       }
      }
     }
    }
    
   } catch (InstantiationException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   } catch (IllegalArgumentException e) {
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   } catch (SecurityException e) {
    e.printStackTrace();
   } catch (NoSuchMethodException e) {
    e.printStackTrace();
   }
  }
  
  return myObject1;
 }

你可能感兴趣的:(java,反射,递归创建对象)