Skip to content

Commit d938571

Browse files
committed
changes for PR 172
1 parent 2984561 commit d938571

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

src/main/java/com/googlecode/jsonrpc4j/JsonRpcBasicServer.java

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import com.fasterxml.jackson.databind.JavaType;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.fasterxml.jackson.databind.node.*;
9-
import com.fasterxml.jackson.databind.type.TypeFactory;
8+
import com.fasterxml.jackson.databind.node.ArrayNode;
9+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
10+
import com.fasterxml.jackson.databind.node.JsonNodeType;
11+
import com.fasterxml.jackson.databind.node.NullNode;
12+
import com.fasterxml.jackson.databind.node.ObjectNode;
1013
import com.googlecode.jsonrpc4j.ErrorResolver.JsonError;
1114
import net.iharder.Base64;
1215
import org.slf4j.Logger;
@@ -17,11 +20,22 @@
1720
import java.io.InputStream;
1821
import java.io.OutputStream;
1922
import java.lang.annotation.Annotation;
20-
import java.lang.reflect.*;
23+
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.lang.reflect.Method;
26+
import java.lang.reflect.Proxy;
27+
import java.lang.reflect.Type;
28+
import java.lang.reflect.UndeclaredThrowableException;
2129
import java.math.BigDecimal;
2230
import java.nio.charset.StandardCharsets;
23-
import java.util.*;
24-
import java.util.logging.Level;
31+
32+
import java.util.ArrayList;
33+
import java.util.Collection;
34+
import java.util.HashSet;
35+
import java.util.Iterator;
36+
import java.util.List;
37+
import java.util.Map;
38+
import java.util.Set;
2539
import java.util.regex.Pattern;
2640

2741
import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.ERROR_NOT_HANDLED;
@@ -610,33 +624,7 @@ private AMethodWithItsArgs findBestMethodByParamsNode(Set<Method> methods, JsonN
610624
throw new IllegalArgumentException("Unknown params node type: " + paramsNode.toString());
611625
}
612626
if(matchedMethod==null) {
613-
for (Method method : methods) {
614-
if(method.getParameterTypes().length!=1) {
615-
continue;
616-
}
617-
if(method.isVarArgs()) {
618-
matchedMethod = new AMethodWithItsArgs(method);
619-
620-
if (paramsNode.isArray()) {
621-
ArrayNode arrayNode = ArrayNode.class.cast(paramsNode);
622-
for (int i = 0; i < paramsNode.size(); i++) {
623-
matchedMethod.arguments.add(arrayNode.get(i));
624-
}
625-
}
626-
627-
if (paramsNode.isObject()) {
628-
ObjectNode objectNode = ObjectNode.class.cast(paramsNode);
629-
Iterator<Map.Entry<String, JsonNode>> items = objectNode.fields();
630-
while (items.hasNext()) {
631-
Map.Entry<String, JsonNode> item = items.next();
632-
JsonNode name = JsonNodeFactory.instance.objectNode().put(item.getKey(),item.getKey());
633-
matchedMethod.arguments.add(name.get(item.getKey()));
634-
matchedMethod.arguments.add(item.getValue());
635-
}
636-
}
637-
break;
638-
}
639-
}
627+
matchedMethod = findBestMethodForVarargs(methods, paramsNode);
640628
}
641629
return matchedMethod;
642630
}
@@ -687,6 +675,37 @@ private Method getBestMatchingArgTypeMethod(ArrayNode paramNodes, int numParams,
687675
}
688676
return bestMethod;
689677
}
678+
679+
private AMethodWithItsArgs findBestMethodForVarargs(Set<Method> methods, JsonNode paramsNode) {
680+
for (Method method : methods) {
681+
if(method.getParameterTypes().length!=1) {
682+
continue;
683+
}
684+
if(method.isVarArgs()) {
685+
AMethodWithItsArgs matchedMethod = new AMethodWithItsArgs(method);
686+
687+
if (paramsNode.isArray()) {
688+
ArrayNode arrayNode = ArrayNode.class.cast(paramsNode);
689+
for (int i = 0; i < paramsNode.size(); i++) {
690+
matchedMethod.arguments.add(arrayNode.get(i));
691+
}
692+
}
693+
694+
if (paramsNode.isObject()) {
695+
ObjectNode objectNode = ObjectNode.class.cast(paramsNode);
696+
Iterator<Map.Entry<String, JsonNode>> items = objectNode.fields();
697+
while (items.hasNext()) {
698+
Map.Entry<String, JsonNode> item = items.next();
699+
JsonNode name = JsonNodeFactory.instance.objectNode().put(item.getKey(),item.getKey());
700+
matchedMethod.arguments.add(name.get(item.getKey()));
701+
matchedMethod.arguments.add(item.getValue());
702+
}
703+
}
704+
return matchedMethod;
705+
}
706+
}
707+
return null;
708+
}
690709

691710
private int getNumArgTypeMatches(ArrayNode paramNodes, int numParams, List<Class<?>> parameterTypes) {
692711
int numMatches = 0;

0 commit comments

Comments
 (0)