package org.nutz.lang;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.nutz.castor.Castors;
import org.nutz.lang.inject.Injecting;

public class Lang2 {
    public static Object inject(Object model, Type type){
        Mirror<?> me = Mirror.me(type);
//        if(me.is(List.class)){
//            return injectList(me);
//        } else if(me.is(Map.class)){
//            return injectMap(me);
//        } else if(me.is(Set.class)){
//            return injectSet(me);
//        } else if(me.getType().isArray()){
//            return injectArray(me);
//        }
        return injectObj(model, me);
    }
    
    @SuppressWarnings({ "unused", "rawtypes" })
    private static Object injectList(Object model, Mirror<?> me){
        Map re = null;
        if(me.isInterface()){
            re = new HashMap();
        } else {
            re = (Map) me.born();
        }
        return null;
    }
    
    private static Object injectObj(Object model, Mirror<?> me) {
        Object obj = me.born();
        Map<String, ?> map = (Map<String, ?>) model;
        for(String key : map.keySet()){
            try{
                Field field = me.getField(key);
                Object val = map.get(key);
                Injecting in = me.getInjecting(key);
                if(!(val instanceof Map || val instanceof List)){
                    in.inject(obj, Castors.me().castTo(val, field.getType()));
                    continue;
                }
                in.inject(obj, inject(val, field.getGenericType()));
            } catch (NoSuchFieldException e){
                continue;
            }
        }
        return obj;
    }
    
}

@@ -80,16 +80,44 @@
 t.Type;%0A
+import java.util.ArrayList;%0A
 import j
@@ -182,30 +182,8 @@
 Map;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -367,26 +367,24 @@
 r.me(type);%0A
-//
         if(m
@@ -394,34 +394,32 @@
 s(List.class))%7B%0A
-//
             retu
@@ -432,23 +432,28 @@
 ectList(
+model, 
 me);%0A
-//
         
@@ -473,34 +473,32 @@
 is(Map.class))%7B%0A
-//
             retu
@@ -510,16 +510,23 @@
 jectMap(
+model, 
 me);%0A// 
@@ -676,26 +676,24 @@
 tArray(me);%0A
-//
         %7D%0A  
@@ -769,10 +769,13 @@
  %22un
-us
+check
 ed%22,
@@ -821,20 +821,19 @@
 t inject
-List
+Map
 (Object 
@@ -1012,31 +1012,951 @@
     
-return null;%0A    %7D%0A    
+%0A        Map map = (Map) model;%0A        for(Object key : map.keySet())%7B%0A            Object val = map.get(key);%0A            if(isLeaf(val))%7B%0A                re.put(key, Castors.me().castTo(val, (Class%3C?%3E) me.getGenericsType(1)));%0A            %7D%0A            re.put(key, inject(val, me.getGenericsType(1)));%0A        %7D%0A        return re;%0A    %7D%0A    %0A    @SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)%0A    private static Object injectList(Object model, Mirror%3C?%3E me)%7B%0A        List re = null;%0A        if(me.isInterface())%7B%0A            re = new ArrayList();%0A        %7D else %7B%0A            re = (List) me.born();%0A        %7D%0A        %0A        for(Object obj : (List) model)%7B%0A            if(isLeaf(obj))%7B%0A                re.add(Castors.me().castTo(obj, (Class%3C?%3E) me.getGenericsType(0)));%0A                continue;%0A            %7D%0A            re.add(inject(obj, me.getGenericsType(0)));%0A        %7D%0A        return re;%0A    %7D%0A    %0A    @SuppressWarnings(%22unchecked%22)
 %0A   
@@ -2327,51 +2327,18 @@
  if(
-!(val instanceof Map %7C%7C val instanceof List
+isLeaf(val
 ))%7B%0A
@@ -2660,10 +2660,221 @@
  %7D%0A    %0A
+    private static boolean isLeaf(Object obj)%7B%0A        if(obj instanceof Map)%7B%0A            return false;%0A        %7D%0A        if(obj instanceof List)%7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A
 %7D%0A

add inject List and Map