Skip to content

Commit

Permalink
fix 修改COLLECT_BATCH接口的参数,Http Param支持解析List结构
Browse files Browse the repository at this point in the history
  • Loading branch information
tzfun committed Apr 12, 2023
1 parent 3008904 commit 594ebb4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,17 @@ public static Set<Method> scanMethodAnnotation(Set<Class<?>> classes, Class<? ex
return result;
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
public static Object parseValueFromStr(Class<?> type, String source) {
if (source == null) {
return null;
}
if (type.isAssignableFrom(String.class)) {
return source;
} else if (type.isAssignableFrom(byte.class)) {
return Byte.parseByte(source);
} else if (type.isAssignableFrom(Byte.class)) {
return Byte.valueOf(source);
} else if (type.isAssignableFrom(short.class)) {
return Short.parseShort(source);
} else if (type.isAssignableFrom(Short.class)) {
Expand All @@ -209,7 +213,7 @@ public static Object parseValueFromStr(Class<?> type, String source) {
return Long.parseLong(source);
} else if (type.isAssignableFrom(Long.class)) {
return Long.valueOf(source);
} else if (type.isAssignableFrom(Enum.class)) {
} else if (type.isEnum()) {
return Enum.valueOf((Class<? extends Enum>) type, source);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -165,7 +167,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked","rawtypes"})
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
PairKey<URI, String> pair = filterUri(ctx, msg);
if (pair == null) {
Expand Down Expand Up @@ -220,6 +222,14 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) thro
Array.set(array, j, ReflexUtil.parseValueFromStr(componentType, valueArr.get(j)));
}
parameter[i] = array;
} else if (parameterType.isAssignableFrom(List.class)) {
List<String> valueArr = (List<String>) params.get(key + "[]");
Type componentType = ((ParameterizedType) method.getGenericParameterTypes()[0]).getActualTypeArguments()[0];
List list = new ArrayList(valueArr.size());
for (String s : valueArr) {
list.add(ReflexUtil.parseValueFromStr((Class<?>) componentType, s));
}
parameter[i] = list;
} else {
parameter[i] = ReflexUtil.parseValueFromStr(parameterType, (String) value);
if (parameter[i] == null) {
Expand Down Expand Up @@ -287,7 +297,7 @@ private Map<String, Object> loadParam(URI uri) throws UnsupportedEncodingExcepti
if (kv.length <= 1) {
continue;
}
String key = kv[0];
String key = URLDecoder.decode(kv[0], "UTF-8");
String value = URLDecoder.decode(kv[1], "UTF-8");
if (key.endsWith("[]")) {
List<String> array = (List<String>) params.computeIfAbsent(key, o -> new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {

public abstract Logger logger();

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
public void handleRequest(ChannelHandlerContext ctx, JvmmRequest reqMsg) {
try {
if (!handleBefore(ctx, reqMsg)) {
Expand Down Expand Up @@ -273,7 +273,7 @@ public void handleRequest(ChannelHandlerContext ctx, JvmmRequest reqMsg) {
}
ctx.channel().writeAndFlush(response.serialize());
});
} else if (parameterType.isAssignableFrom(Enum.class)) {
} else if (parameterType.isEnum()) {
parameter[i] = Enum.valueOf((Class<? extends Enum>) parameterType, reqMsg.getData().toString());
} else {
parameter[i] = new Gson().fromJson(reqMsg.getData(), method.getGenericParameterTypes()[i]);
Expand Down
Loading

0 comments on commit 594ebb4

Please sign in to comment.