package com.dtflys.forest.proxy;

import com.dtflys.forest.config.ForestConfiguration;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gongjun[dt_flys@hotmail.com]
 * @since 2016-03-25 18:17
 */
public class ProxyFactory<T> {
    private Map<Class<T>, T> instanceCache = new HashMap<Class<T>, T>();
    private ForestConfiguration configuration;
    private Class<T> interfaceClass;

    public ProxyFactory(ForestConfiguration configuration, Class<T> interfaceClass) {
        this.configuration = configuration;
        this.interfaceClass = interfaceClass;
    }

    public Class<T> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public synchronized T createInstance() {
        T instance = instanceCache.get(interfaceClass);
        if (instance != null) {
            return instance;
        }
        InterfaceProxyHandler<T> interfaceProxyHandler = new InterfaceProxyHandler<T>(configuration, this, interfaceClass);
        instance = (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] {interfaceClass}, interfaceProxyHandler);
        instanceCache.put(interfaceClass, instance);
        return instance;
    }

}

@@ -280,16 +280,23 @@
 private 
+static 
 Map%3CClas
@@ -296,22 +296,24 @@
 ap%3CClass
-%3CT%3E, T
+, Object
 %3E instan
@@ -338,19 +338,8 @@
 Map%3C
-Class%3CT%3E, T
 %3E();
@@ -816,21 +816,8 @@
 lic 
-synchronized 
 T cr
@@ -829,16 +829,186 @@
 nstance(
+) %7B%0A        T instance = (T) instanceCache.get(interfaceClass);%0A        if (instance != null) %7B%0A            return instance;%0A        %7D%0A        synchronized (instanceCache
 ) %7B%0A    
@@ -1003,33 +1003,35 @@
 ache) %7B%0A        
+   
-T
  instance = inst
@@ -1025,16 +1025,20 @@
 stance =
+ (T)
  instanc
@@ -1065,32 +1065,36 @@
 Class);%0A        
+    
 if (instance != 
@@ -1105,32 +1105,36 @@
 ) %7B%0A            
+ 
+   
 return instance;
@@ -1126,32 +1126,36 @@
 eturn instance;%0A
+    
         %7D%0A      
@@ -1156,16 +1156,20 @@
         
+    
 Interfac
@@ -1276,32 +1276,36 @@
 Class);%0A        
+   
+ 
 instance = (T) P
@@ -1370,17 +1370,16 @@
  Class%5B%5D
- 
 %7Binterfa
@@ -1412,32 +1412,36 @@
 ndler);%0A        
+  
+  
 instanceCache.pu
@@ -1469,32 +1469,36 @@
 tance);%0A        
+ 
+   
 return instance;
@@ -1494,21 +1494,31 @@
 n instance;%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

fix 修改instanceCache为静态属性