Spring bean reflection
This commit is contained in:
parent
1c3e89e799
commit
b3d1fff6b5
|
@ -1,5 +1,6 @@
|
|||
package com.loafle.overflow.proxy;
|
||||
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.google.protobuf.ByteString;
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
@ -8,10 +9,7 @@ import org.springframework.context.ApplicationContext;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -98,10 +96,15 @@ public class ServiceInvoker {
|
|||
parameterCache.genericClazzes[j] = Class.forName(actualTypeArguments[j].getTypeName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
if (Primitives.allPrimitiveTypes().contains(parameterType.getTypeName())) {
|
||||
|
||||
} else {
|
||||
parameterCache.clazz = Class.forName(parameterType.getTypeName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return methodCache;
|
||||
}
|
||||
|
@ -146,12 +149,16 @@ public class ServiceInvoker {
|
|||
return serviceCache;
|
||||
}
|
||||
|
||||
private Object getValue(Cache.ParameterCache parameterCache, String json) throws IOException {
|
||||
private Object getValue(Cache.ParameterCache parameterCache, String json) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
Object result = null;
|
||||
if (List.class == parameterCache.clazz) {
|
||||
result = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, parameterCache.genericClazzes[0]));
|
||||
} else if (Map.class == parameterCache.clazz) {
|
||||
|
||||
} else if (parameterCache.clazz.isPrimitive()) {
|
||||
Class wrapperClazz = Primitives.wrap(parameterCache.clazz);
|
||||
Constructor con = wrapperClazz.getConstructor(String.class);
|
||||
result = con.newInstance(json);
|
||||
} else {
|
||||
result = objectMapper.readValue(json, parameterCache.clazz);
|
||||
}
|
||||
|
@ -159,7 +166,7 @@ public class ServiceInvoker {
|
|||
return result;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Cache.ParameterCache[] parameterCaches, List<String> params) throws IOException {
|
||||
private Object[] getParameters(Cache.ParameterCache[] parameterCaches, List<String> params) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
|
||||
if (null == parameterCaches || null == params) {
|
||||
return null;
|
||||
}
|
||||
|
@ -177,7 +184,7 @@ public class ServiceInvoker {
|
|||
return result;
|
||||
}
|
||||
|
||||
private Object[] getParametersForByteString(Cache.ParameterCache[] parameterCaches, List<ByteString> params) throws IOException {
|
||||
private Object[] getParametersForByteString(Cache.ParameterCache[] parameterCaches, List<ByteString> params) throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||
if (null == parameterCaches || null == params) {
|
||||
return null;
|
||||
}
|
||||
|
@ -195,7 +202,7 @@ public class ServiceInvoker {
|
|||
return result;
|
||||
}
|
||||
|
||||
public String invoke(String serviceName, String methodName, List<String> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
|
||||
public String invoke(String serviceName, String methodName, List<String> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException, InstantiationException {
|
||||
Cache serviceCache = getServiceCache(serviceName);
|
||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||
|
||||
|
@ -211,7 +218,7 @@ public class ServiceInvoker {
|
|||
return jsonInString;
|
||||
}
|
||||
|
||||
public String invokeForByteString(String serviceName, String methodName, List<ByteString> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException {
|
||||
public String invokeForByteString(String serviceName, String methodName, List<ByteString> params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException, InstantiationException {
|
||||
Cache serviceCache = getServiceCache(serviceName);
|
||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.loafle.overflow.proxy;
|
||||
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.loafle.overflow.module.target.service.TargetDiscoveryService;
|
||||
import com.loafle.overflow.spring.AppConfig;
|
||||
import com.loafle.overflow.spring.JdbcConfiguration;
|
||||
|
@ -10,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -27,18 +29,30 @@ public class ServiceInvokerTest {
|
|||
@Test
|
||||
public void invoke() throws Exception {
|
||||
|
||||
List<String> params = new ArrayList<>(2);
|
||||
params.add("[{\"id\": 1," +
|
||||
"\"ip\":2312132112," +
|
||||
"\"mac\":12312312," +
|
||||
"\"os\": \"Windows\"," +
|
||||
"\"target\":true}]");
|
||||
params.add("{\"id\": 1," +
|
||||
"\"ip\":2312132112," +
|
||||
"\"probeKey\":\"sdfsdfsdfsdfsd\"}");
|
||||
// List<String> params = new ArrayList<>(2);
|
||||
// params.add("[{\"id\": 1," +
|
||||
// "\"ip\":2312132112," +
|
||||
// "\"mac\":12312312," +
|
||||
// "\"os\": \"Windows\"," +
|
||||
// "\"target\":true}]");
|
||||
// params.add("{\"id\": 1," +
|
||||
// "\"ip\":2312132112," +
|
||||
// "\"probeKey\":\"sdfsdfsdfsdfsd\"}");
|
||||
//
|
||||
// serviceInvoker.invoke("TargetDiscoveryService", "saveAllTarget", params);
|
||||
|
||||
serviceInvoker.invoke("TargetDiscoveryService", "saveAllTarget", params);
|
||||
// List<String> params = new ArrayList<>(1);
|
||||
// params.add("1");
|
||||
//
|
||||
// serviceInvoker.invoke("InfraOSService", "read", params);
|
||||
|
||||
List<String> params = new ArrayList<>(1);
|
||||
params.add("1");
|
||||
|
||||
serviceInvoker.invoke("MemberService", "read", params);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user