-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Official and Stable ORM Framework for MongoDB close(#829)
- Loading branch information
Showing
9 changed files
with
110 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package com.xiaomi.youpin.docean; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.xiaomi.youpin.docean.anno.RequestMapping; | ||
|
@@ -41,10 +42,12 @@ | |
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -138,11 +141,32 @@ private void registerControllerMethods(Bean bean) { | |
hrm.setObj(bean.getObj()); | ||
hrm.setMethod(m); | ||
hrm.setHttpMethod(rm.method()); | ||
hrm.setGenericSuperclassTypeArguments(getGenericSuperclassTypeArguments(bean.getClazz())); | ||
ioc.publishEvent(new Event(EventType.initController, path)); | ||
requestMethodMap.put(path, hrm); | ||
})); | ||
} | ||
|
||
public static Map<String, Class> getGenericSuperclassTypeArguments(Class clazz) { | ||
List<String> list = Arrays.stream(clazz.getSuperclass().getTypeParameters()).map(it -> it.getName()).collect(Collectors.toList()); | ||
if (list.size() == 0) { | ||
return Maps.newHashMap(); | ||
} | ||
Map<String, Class> map = new HashMap<>(); | ||
Type genericSuperclass = clazz.getGenericSuperclass(); | ||
if (genericSuperclass instanceof ParameterizedType) { | ||
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass; | ||
Type[] typeArguments = parameterizedType.getActualTypeArguments(); | ||
for (int i = 0; i < typeArguments.length; i++) { | ||
Type argument = typeArguments[i]; | ||
if (argument instanceof Class<?>) { | ||
map.put(list.get(i), (Class) argument); | ||
} | ||
} | ||
} | ||
return map; | ||
} | ||
|
||
|
||
private static final class LazyHolder { | ||
private static final Mvc ins = new Mvc(Ioc.ins()); | ||
|
@@ -187,6 +211,16 @@ public void callService(MvcContext context, MvcRequest request, MvcResponse resp | |
response.writeAndFlush(context, new Gson().toJson(mr)); | ||
} | ||
|
||
public List<Class> mapMethodParametersToClasses(Method method, Map<String, Class> map) { | ||
return Arrays.stream(method.getParameters()).map(it -> { | ||
String name = it.getParameterizedType().getTypeName(); | ||
if ((it.getType() instanceof Object) && map.containsKey(name)) { | ||
return map.get(name); | ||
} | ||
return it.getType(); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
public void callMethod(MvcContext context, MvcRequest request, MvcResponse response, MvcResult<Object> result, HttpRequestMethod method) { | ||
Safe.run(() -> { | ||
Object[] params = new Object[]{null}; | ||
|
@@ -199,7 +233,10 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo | |
params[0] = context; | ||
} else { | ||
try { | ||
params = methodInvoker.getMethodParams(method.getMethod(), args); | ||
//可能方法中有泛型,这里给fix调,用实际的Class | ||
List<Class> list = mapMethodParametersToClasses(method.getMethod(), method.getGenericSuperclassTypeArguments()); | ||
Class[] types = list.toArray(new Class[]{}); | ||
params = methodInvoker.getMethodParams(args, types); | ||
} catch (Exception e) { | ||
log.error("getMethodParams error,path:{},params:{},method:{}", context.getPath(), | ||
GsonUtils.gson.toJson(context.getParams()), request.getMethod().toLowerCase(Locale.ROOT), e); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import lombok.Data; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -36,5 +37,7 @@ public class HttpRequestMethod { | |
|
||
private long timeout; | ||
|
||
private Map<String, Class> genericSuperclassTypeArguments; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters