/**
 * Copyright (c) 2015-2017, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.aop;


import com.google.inject.*;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
import io.jboot.aop.injector.JbootHystrixInjector;
import io.jboot.aop.injector.JbootrpcMembersInjector;
import io.jboot.aop.interceptor.JbootHystrixCommandInterceptor;
import io.jboot.aop.interceptor.JbootrpcInterceptor;
import io.jboot.core.hystrix.annotation.EnableHystrixCommand;
import io.jboot.rpc.annotation.JbootrpcService;

import java.lang.reflect.Field;

/**
 * Inject管理器
 */
public class JbootInjectManager {

    static JbootInjectManager me = new JbootInjectManager();

    public static JbootInjectManager me() {
        return me;
    }


    private Injector injector;

    private JbootInjectManager() {
        injector = Guice.createInjector(appModule);
    }


    public Injector getInjector() {
        return injector;
    }


    private Module appModule = new Module() {
        @Override
        public void configure(Binder binder) {
            binder.bindListener(Matchers.any(), rpcListener);
            binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(JbootrpcService.class), new JbootrpcInterceptor());
            binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(EnableHystrixCommand.class), new JbootHystrixCommandInterceptor());
        }
    };


    private TypeListener rpcListener = new TypeListener() {
        @Override
        public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            Class clazz = type.getRawType();
            if (clazz == null) return;

            for (Field field : clazz.getDeclaredFields()) {
                if (field.isAnnotationPresent(JbootrpcService.class)) {
                    encounter.register(new JbootrpcMembersInjector(field));
                }

                if (field.isAnnotationPresent(EnableHystrixCommand.class)) {
                    encounter.register(new JbootHystrixInjector(field));
                }
            }
        }
    };


}

@@ -1259,16 +1259,48 @@
 Manager 
+implements Module, TypeListener 
 %7B%0A%0A    s
@@ -1534,25 +1534,20 @@
 njector(
-appModule
+this
 );%0A    %7D
@@ -1626,54 +1626,73 @@
     
-private Module appModule = new Module() %7B%0A
+/**%0A     * module implements%0A     *%0A     * @param binder%0A 
     
+*/%0A
     
@@ -1693,36 +1693,32 @@
 /%0A    @Override%0A
-    
     public void 
@@ -1744,36 +1744,32 @@
 nder) %7B%0A        
-    
 binder.bindListe
@@ -1788,27 +1788,20 @@
 .any(), 
-rpcListener
+this
 );%0A     
@@ -1795,36 +1795,32 @@
  this);%0A        
- 
-   
 binder.bindInter
@@ -1913,28 +1913,24 @@
 rceptor());%0A
-    
         bind
@@ -2063,34 +2063,25 @@
     
- 
-   
 %7D
+%0A
 %0A    
-%7D;%0A%0A
+/**
 %0A    
-private
+ *
  Typ
@@ -2094,47 +2094,96 @@
 ner 
-rpcListener = new TypeListener() %7B
+ implements%0A     *%0A     * @param type%0A     * @param encounter%0A     * @param %3CI%3E
 %0A    
+ */%0A
     
@@ -2192,20 +2192,16 @@
 verride%0A
-    
     publ
@@ -2276,20 +2276,16 @@
         
-  
-  
 Class cl
@@ -2313,28 +2313,24 @@
 ();%0A        
-    
 if (clazz ==
@@ -2353,20 +2353,16 @@
         
-  
-  
 for (Fie
@@ -2397,28 +2397,24 @@
 Fields()) %7B%0A
-    
             
@@ -2477,36 +2477,32 @@
                 
-  
-  
 encounter.regist
@@ -2553,20 +2553,16 @@
         
-    
 %7D%0A%0A     
@@ -2560,36 +2560,32 @@
  %7D%0A%0A            
-  
-  
 if (field.isAnno
@@ -2637,36 +2637,32 @@
                 
-    
 encounter.regist
@@ -2702,26 +2702,8 @@
 ));%0A
-                %7D%0A
     
@@ -2727,14 +2727,11 @@
  %7D%0A    %7D
-;%0A%0A
 %0A%7D%0A

optimize JbootInjectManager