|
|
|
@ -1,8 +1,6 @@
|
|
|
|
|
package com.fanruan.proxy.interceptor; |
|
|
|
|
|
|
|
|
|
import com.fanruan.annotation.LocalMethod; |
|
|
|
|
import com.fanruan.annotation.NotImplemented; |
|
|
|
|
import com.fanruan.annotation.RemoteClass; |
|
|
|
|
import com.fanruan.annotation.*; |
|
|
|
|
import com.fanruan.pojo.message.RpcRequest; |
|
|
|
|
import com.fanruan.service.jdbc.AbstractBind; |
|
|
|
|
import com.fanruan.service.jdbc.ServiceInputStream; |
|
|
|
@ -10,35 +8,39 @@ import com.fanruan.service.jdbc.ServiceReader;
|
|
|
|
|
import com.fanruan.utils.Commons; |
|
|
|
|
|
|
|
|
|
import java.io.Reader; |
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
import java.lang.reflect.Parameter; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Yichen Dai |
|
|
|
|
*/ |
|
|
|
|
public class InterceptorUtils { |
|
|
|
|
|
|
|
|
|
private static final String[] WRAPPER_CLASS_LIST = new String[]{ |
|
|
|
|
"Boolean", |
|
|
|
|
"Integer", |
|
|
|
|
"Double", |
|
|
|
|
"Long", |
|
|
|
|
"Character", |
|
|
|
|
"Byte", |
|
|
|
|
"Short", |
|
|
|
|
"Float" |
|
|
|
|
private static final Map<String, Class<?>> WRAPPER_CLASS_MAP = new HashMap<String, Class<?>>(){ |
|
|
|
|
{ |
|
|
|
|
put("Integer", Integer.TYPE); |
|
|
|
|
put("Short", Short.TYPE); |
|
|
|
|
put("Long", Long.TYPE); |
|
|
|
|
put("Double", Double.TYPE); |
|
|
|
|
put("Float", Float.TYPE); |
|
|
|
|
put("Byte", Byte.TYPE); |
|
|
|
|
put("Character", Character.TYPE); |
|
|
|
|
put("Boolean", Boolean.TYPE); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isWraps(Object o){ |
|
|
|
|
for(String ex : WRAPPER_CLASS_LIST){ |
|
|
|
|
if(ex.equals(getClassName(o.getClass().getName()))){ |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
Class<?> clz = o.getClass(); |
|
|
|
|
if(clz == null) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
return WRAPPER_CLASS_MAP.containsKey(getClassName(clz.getName())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getClassName(String fullyQualifiedClassName){ |
|
|
|
|
String[] arr = fullyQualifiedClassName.split("\\."); |
|
|
|
|
int n = arr.length; |
|
|
|
@ -53,10 +55,17 @@ public class InterceptorUtils {
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
Class<?> clazz = o.getClass(); |
|
|
|
|
if(clazz.isAnnotationPresent(RemoteClass.class)){ |
|
|
|
|
return true; |
|
|
|
|
return clazz.isAnnotationPresent(RemoteClass.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void replaceBindingParameter(Method method, Object[] args){ |
|
|
|
|
Parameter[] pars = method.getParameters(); |
|
|
|
|
for(int i=0; i<pars.length; i++){ |
|
|
|
|
Parameter par = pars[i]; |
|
|
|
|
if(par.isAnnotationPresent(BindingParamter.class) && isInBindList(args[i])){ |
|
|
|
|
args[i] = getBindID(args[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void setBindID(Object returnObj, String ID){ |
|
|
|
@ -88,6 +97,10 @@ public class InterceptorUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static RpcRequest generateRequest(Class<?> clazz, Object o, Method method, Object[] objects){ |
|
|
|
|
if(isWithBindingParameter(method)){ |
|
|
|
|
replaceBindingParameter(method, objects); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RpcRequest rpcRequest = new RpcRequest(); |
|
|
|
|
rpcRequest |
|
|
|
|
.setID(Commons.getID()) |
|
|
|
@ -114,17 +127,24 @@ public class InterceptorUtils {
|
|
|
|
|
return rpcRequest; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean isNotImplemented(Method method) { |
|
|
|
|
if(method.isAnnotationPresent(NotImplemented.class)){ |
|
|
|
|
return true; |
|
|
|
|
public static Class<?>[] getArgTypes(Object[] objects){ |
|
|
|
|
int n = objects.length; |
|
|
|
|
Class<?>[] argTypes = new Class<?>[n]; |
|
|
|
|
for(int i=0; i<n; i++){ |
|
|
|
|
argTypes[i] = objects[i].getClass(); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
return argTypes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean isNotImplemented(Method method) { |
|
|
|
|
return method.isAnnotationPresent(NotImplemented.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean isLocalMethod(Method method) { |
|
|
|
|
if(method.isAnnotationPresent(LocalMethod.class)){ |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
return method.isAnnotationPresent(LocalMethod.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean isWithBindingParameter(Method method){ |
|
|
|
|
return method.isAnnotationPresent(WithBindingParameter.class); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|