From 795a84005fb86a6fc3649810807624e39e3bdd5c Mon Sep 17 00:00:00 2001 From: LiZhen Date: Tue, 11 Dec 2018 10:40:22 +0800 Subject: [PATCH] Fix telnet can not find method with enum type (#2803) --- .../dubbo/telnet/InvokeTelnetHandler.java | 9 +++++++++ .../rpc/protocol/dubbo/support/DemoService.java | 2 +- .../protocol/dubbo/support/DemoServiceImpl.java | 4 ++-- .../dubbo/telnet/InvokerTelnetHandlerTest.java | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java index 82591ab90c1..b250c1e1f1c 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.utils.CompatibleTypeUtils; import org.apache.dubbo.common.utils.PojoUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -75,6 +76,14 @@ private static boolean isMatch(Class[] types, List args) { } if (ReflectUtils.isPrimitive(arg.getClass())) { + if (arg instanceof String && type.isEnum()) { + try { + CompatibleTypeUtils.compatibleTypeConvert(arg, type); + } catch (RuntimeException e) { + return false; + } + continue; + } if (!ReflectUtils.isPrimitive(type)) { return false; } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java index 357b0321d36..db85cc51b3d 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java @@ -47,7 +47,7 @@ public interface DemoService { Type enumlength(Type... types); -// Type enumlength(Type type); + Type getType(Type type); String get(CustomArgument arg1); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java index e404af0e46c..ed38344a324 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java @@ -71,8 +71,8 @@ public Type enumlength(Type... types) { return Type.Lower; return types[0]; } - - public Type enumlength(Type type) { + + public Type getType(Type type) { return type; } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index a785330aa86..dc63c8b8b53 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -108,6 +108,23 @@ public void testInvokeByPassingNullValue() throws RemotingException { } } + @Test + public void testInvokeByPassingEnumValue() throws RemotingException { + mockInvoker = mock(Invoker.class); + given(mockInvoker.getInterface()).willReturn(DemoService.class); + given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20886/demo")); + given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); + mockChannel = mock(Channel.class); + given(mockChannel.getAttribute("telnet.service")).willReturn(null); + given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); + given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); + + DubboProtocol.getDubboProtocol().export(mockInvoker); + String result = invoke.telnet(mockChannel, "getType(\"High\")"); + assertTrue(result.contains("ok")); + } + + @SuppressWarnings("unchecked") @Test public void testInvokeAutoFindMethod() throws RemotingException {