You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`private static boolean isMatch(Class[] types, List args) {
if (types.length != args.size()) {
return false;
}
for (int i = 0; i < types.length; i++) {
Class type = types[i];
Object arg = args.get(i);
if (arg == null) {
// if the type is primitive, the method to invoke will cause NullPointerException definitely
// so we can offer a specified error message to the invoker in advance and avoid unnecessary invoking
if (type.isPrimitive()) {
throw new NullPointerException(String.format(
"The type of No.%d parameter is primitive(%s), but the value passed is null.", i + 1, type.getName()));
}
// if the type is not primitive, we choose to believe what the invoker want is a null value
continue;
}
if (ReflectUtils.isPrimitive(arg.getClass())) {
if (!ReflectUtils.isPrimitive(type)) {
return false;
}
} else if (arg instanceof Map) {
String name = (String) ((Map<?, ?>) arg).get("class");
Class<?> cls = arg.getClass();
if (name != null && name.length() > 0) {
cls = ReflectUtils.forName(name);
}
if (!type.isAssignableFrom(cls)) {
return false;
}
} else if (arg instanceof Collection) {
if (!type.isArray() && !type.isAssignableFrom(arg.getClass())) {
return false;
}
} else {
if (!type.isAssignableFrom(arg.getClass())) {
return false;
}
}
}
return true;
}`
another problem,if a method contains complex parameters,Such as class Student below,the invoke command can never invoke the correct method since com.alibaba.fastjson.JSON#parseArray(java.lang.String, java.lang.Class) can not parse args to correct class,in blow method to parse the args (com.alibaba.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler#telnet; list = JSON.parseArray("[" + args + "]", Object.class);)
public String telnet(Channel channel, String message) {
if (message == null || message.length() == 0) {
return "Please input method name, eg: \r\ninvoke xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke com.xxx.XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})";
}
StringBuilder buf = new StringBuilder();
String service = (String) channel.getAttribute(ChangeTelnetHandler.SERVICE_KEY);
if (service != null && service.length() > 0) {
buf.append("Use default service " + service + ".\r\n");
}
int i = message.indexOf("(");
if (i < 0 || !message.endsWith(")")) {
return "Invalid parameters, format: service.method(args)";
}
String method = message.substring(0, i).trim();
String args = message.substring(i + 1, message.length() - 1).trim();
i = method.lastIndexOf(".");
if (i >= 0) {
service = method.substring(0, i).trim();
method = method.substring(i + 1).trim();
}
List<Object> list;
try {
list = JSON.parseArray("[" + args + "]", Object.class);
} catch (Throwable t) {
return "Invalid json argument, cause: " + t.getMessage();
}
...
}
public class Student implements Serializable {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
,not like the Telnet Command Reference decribed about the invoke command.So I think there is necessary to enhance the invoke command and make the argument type known to it.
Activity
lihuagang03 commentedon Nov 11, 2018
为何调用的第一个参数值是字符串,如
("13", Set["1"]),而不是(13, Set["1"])呢zonghaishang commentedon Nov 12, 2018
传递13也是可以的,一样
kexianjun commentedon Nov 12, 2018
`private static boolean isMatch(Class[] types, List args) { if (types.length != args.size()) { return false; } for (int i = 0; i < types.length; i++) { Class type = types[i];
Object arg = args.get(i);
这个方法判断有问题吧
'if (ReflectUtils.isPrimitive(arg.getClass())) {
if (!ReflectUtils.isPrimitive(type)) {
return false;
}'
这里只判断了是不是同为基本类型,如果同为基本类型没有继续判断类型是否兼容了,导致不管示例中第一个参数传什么,都是匹配到第一个方法了
zonghaishang commentedon Nov 12, 2018
yes, @kexianjun Welcome to fix this bug, :-)
tswstarplanet commentedon Nov 14, 2018
我试了下,参数带L后缀不能被正确解析成Long类型的数字
tswstarplanet commentedon Nov 14, 2018
字符串带L不能正确解析
tswstarplanet commentedon Nov 14, 2018
#2787
kexianjun commentedon Nov 15, 2018
another problem,if a method contains complex parameters,Such as class Student below,the invoke command can never invoke the correct method since com.alibaba.fastjson.JSON#parseArray(java.lang.String, java.lang.Class) can not parse args to correct class,in blow method to parse the args (com.alibaba.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler#telnet; list = JSON.parseArray("[" + args + "]", Object.class);)
,not like the Telnet Command Reference decribed about the invoke command.So I think there is necessary to enhance the invoke command and make the argument type known to it.
beiwei30 commentedon Dec 11, 2018
@kexianjun I merged your change. Pls. check my comments on the pull request, and consider to update doc.