Spring bean reflection
This commit is contained in:
parent
1c3e89e799
commit
b3d1fff6b5
|
@ -1,5 +1,6 @@
|
||||||
package com.loafle.overflow.proxy;
|
package com.loafle.overflow.proxy;
|
||||||
|
|
||||||
|
import com.google.common.primitives.Primitives;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import org.codehaus.jackson.map.DeserializationConfig;
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
@ -8,10 +9,7 @@ import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.*;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -99,7 +97,12 @@ public class ServiceInvoker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parameterCache.clazz = Class.forName(parameterType.getTypeName());
|
|
||||||
|
if (Primitives.allPrimitiveTypes().contains(parameterType.getTypeName())) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
parameterCache.clazz = Class.forName(parameterType.getTypeName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,12 +149,16 @@ public class ServiceInvoker {
|
||||||
return serviceCache;
|
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;
|
Object result = null;
|
||||||
if (List.class == parameterCache.clazz) {
|
if (List.class == parameterCache.clazz) {
|
||||||
result = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, parameterCache.genericClazzes[0]));
|
result = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, parameterCache.genericClazzes[0]));
|
||||||
} else if (Map.class == parameterCache.clazz) {
|
} 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 {
|
} else {
|
||||||
result = objectMapper.readValue(json, parameterCache.clazz);
|
result = objectMapper.readValue(json, parameterCache.clazz);
|
||||||
}
|
}
|
||||||
|
@ -159,7 +166,7 @@ public class ServiceInvoker {
|
||||||
return result;
|
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) {
|
if (null == parameterCaches || null == params) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -177,7 +184,7 @@ public class ServiceInvoker {
|
||||||
return result;
|
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) {
|
if (null == parameterCaches || null == params) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -195,7 +202,7 @@ public class ServiceInvoker {
|
||||||
return result;
|
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 serviceCache = getServiceCache(serviceName);
|
||||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||||
|
|
||||||
|
@ -211,7 +218,7 @@ public class ServiceInvoker {
|
||||||
return jsonInString;
|
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 serviceCache = getServiceCache(serviceName);
|
||||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.loafle.overflow.proxy;
|
package com.loafle.overflow.proxy;
|
||||||
|
|
||||||
|
import com.google.common.primitives.Primitives;
|
||||||
import com.loafle.overflow.module.target.service.TargetDiscoveryService;
|
import com.loafle.overflow.module.target.service.TargetDiscoveryService;
|
||||||
import com.loafle.overflow.spring.AppConfig;
|
import com.loafle.overflow.spring.AppConfig;
|
||||||
import com.loafle.overflow.spring.JdbcConfiguration;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -27,18 +29,30 @@ public class ServiceInvokerTest {
|
||||||
@Test
|
@Test
|
||||||
public void invoke() throws Exception {
|
public void invoke() throws Exception {
|
||||||
|
|
||||||
List<String> params = new ArrayList<>(2);
|
// List<String> params = new ArrayList<>(2);
|
||||||
params.add("[{\"id\": 1," +
|
// params.add("[{\"id\": 1," +
|
||||||
"\"ip\":2312132112," +
|
// "\"ip\":2312132112," +
|
||||||
"\"mac\":12312312," +
|
// "\"mac\":12312312," +
|
||||||
"\"os\": \"Windows\"," +
|
// "\"os\": \"Windows\"," +
|
||||||
"\"target\":true}]");
|
// "\"target\":true}]");
|
||||||
params.add("{\"id\": 1," +
|
// params.add("{\"id\": 1," +
|
||||||
"\"ip\":2312132112," +
|
// "\"ip\":2312132112," +
|
||||||
"\"probeKey\":\"sdfsdfsdfsdfsd\"}");
|
// "\"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