com.jianggujin.http.support.JApiStore Maven / Gradle / Ivy
package com.jianggujin.http.support;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
/**
* API仓库
*
* @author jianggujin
*
*/
public class JApiStore {
private static JApiInvocationHandler handler = new JApiInvocationHandler();
private static Map, Object> apiCache = new HashMap, Object>();
/**
* 获得API
*
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static T getApi(Class clazz) {
if (clazz == null) {
throw new IllegalArgumentException("clazz must not be null");
}
Object obj = apiCache.get(clazz);
if (obj == null) {
synchronized (apiCache) {
obj = apiCache.get(clazz);
if (obj == null) {
obj = (T) Proxy.newProxyInstance(clazz.getClassLoader(),
clazz.isInterface() ? new Class>[] { clazz } : clazz.getInterfaces(), handler);
apiCache.put(clazz, obj);
}
}
}
return (T) obj;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy