From b3d1fff6b5e10bba2822895246d0ac8ced0492d5 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 30 Jun 2017 20:54:54 +0900 Subject: [PATCH] Spring bean reflection --- .../loafle/overflow/proxy/ServiceInvoker.java | 27 +++++++++------ .../overflow/proxy/ServiceInvokerTest.java | 34 +++++++++++++------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java index e55e08e..8446f3d 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java @@ -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; @@ -99,7 +97,12 @@ public class ServiceInvoker { } } } 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; } - 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 params) throws IOException { + private Object[] getParameters(Cache.ParameterCache[] parameterCaches, List 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 params) throws IOException { + private Object[] getParametersForByteString(Cache.ParameterCache[] parameterCaches, List 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 params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException { + public String invoke(String serviceName, String methodName, List 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 params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException { + public String invokeForByteString(String serviceName, String methodName, List params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException, InstantiationException { Cache serviceCache = getServiceCache(serviceName); Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName); diff --git a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java index 448ec26..3ef4bcf 100644 --- a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java +++ b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java @@ -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 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 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 params = new ArrayList<>(1); +// params.add("1"); +// +// serviceInvoker.invoke("InfraOSService", "read", params); + + List params = new ArrayList<>(1); + params.add("1"); + + serviceInvoker.invoke("MemberService", "read", params); } + + } \ No newline at end of file