diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java index de4235ef65..bc237b91bb 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java @@ -16,17 +16,19 @@ */ package org.apache.dubbo.rpc.cluster.directory; +import org.apache.dubbo.rpc.AttachmentsAdapter; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; + import java.util.HashMap; import java.util.Map; + import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import org.apache.dubbo.common.utils.MapUtils; import static org.apache.dubbo.rpc.Constants.TOKEN_KEY; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; /** * MockInvocation.java @@ -73,7 +75,7 @@ public class MockDirInvocation implements Invocation { } public Map getAttachments() { - return MapUtils.objectToStringMap(attachments); + return new AttachmentsAdapter.ObjectToStringMap(attachments); } @Override @@ -88,7 +90,7 @@ public class MockDirInvocation implements Invocation { @Override public void setAttachment(String key, Object value) { - setObjectAttachment(key, value); + setObjectAttachment(key, value); } @Override diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java deleted file mode 100644 index 00454a5ed9..0000000000 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.dubbo.common.utils; - -import java.util.HashMap; -import java.util.Map; - -/** - * Map tools - */ -public class MapUtils { - - /** - * switch Map to Map - * - * If the value of the original Map is not of type String, then toString() of value will be called - * - * @param originMap - * @return - */ - public static Map objectToStringMap(Map originMap) { - Map newStrMap = new HashMap<>(); - - if (originMap == null) { - return newStrMap; - } - - for (Map.Entry entry : originMap.entrySet()) { - String stringValue = convertToString(entry.getValue()); - if (stringValue != null) { - newStrMap.put(entry.getKey(), stringValue); - } - } - - return newStrMap; - } - - /** - * use {@link Object#toString()} switch Obj to String - * - * @param obj - * @return - */ - private static String convertToString(Object obj) { - if (obj == null) { - return null; - } else { - return obj.toString(); - } - } -} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java index 3a1b76eeef..a2cb52e1a8 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.service; -import org.apache.dubbo.common.utils.MapUtils; +import org.apache.dubbo.rpc.AttachmentsAdapter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -79,7 +79,7 @@ public class MockInvocation implements Invocation { } public Map getAttachments() { - return MapUtils.objectToStringMap(attachments); + return new AttachmentsAdapter.ObjectToStringMap(attachments); } @Override diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java index 65d859aa4b..27df08acdc 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java @@ -17,7 +17,6 @@ package org.apache.dubbo.rpc; -import org.apache.dubbo.common.utils.MapUtils; import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler; import java.util.HashMap; @@ -122,7 +121,7 @@ public class AppResponse implements Result { @Override @Deprecated public Map getAttachments() { - return MapUtils.objectToStringMap(attachments); + return new AttachmentsAdapter.ObjectToStringMap(attachments); } @Override diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java index e6cb643357..def349a753 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java @@ -22,18 +22,12 @@ import java.util.Map; /** * This class provides map adapters to support attachments in RpcContext, Invocation and Result switch from * to - * - * please use {@link org.apache.dubbo.common.utils.MapUtils} - * */ -@Deprecated public class AttachmentsAdapter { - @Deprecated public static class ObjectToStringMap extends HashMap { private Map attachments; - @Deprecated public ObjectToStringMap(Map attachments) { for (Entry entry : attachments.entrySet()) { String convertResult = convert(entry.getValue()); @@ -57,11 +51,10 @@ public class AttachmentsAdapter { } private String convert(Object obj) { - if (obj == null) { - return null; - } else { - return obj.toString(); + if (obj instanceof String) { + return (String) obj; } + return null; // or JSON.toString(obj); } @Override diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java index 0a48f7cde5..8077067378 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.Experimental; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.threadlocal.InternalThreadLocal; import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.MapUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -547,15 +546,13 @@ public class RpcContext { } /** - * get String type attachments. - * - * Best to use {{@link #getObjectAttachments()}} + * get attachments. * * @return attachments */ @Deprecated public Map getAttachments() { - return MapUtils.objectToStringMap(this.getObjectAttachments()); + return new AttachmentsAdapter.ObjectToStringMap(this.getObjectAttachments()); } /** diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index 116f3e64fb..f94c001989 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -17,7 +17,6 @@ package org.apache.dubbo.rpc; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.MapUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.model.ApplicationModel; @@ -278,7 +277,7 @@ public class RpcInvocation implements Invocation, Serializable { @Deprecated @Override public Map getAttachments() { - return MapUtils.objectToStringMap(attachments); + return new AttachmentsAdapter.ObjectToStringMap(attachments); } @Deprecated diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/AppResponseTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/AppResponseTest.java index 3641185b45..f9490886e1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/AppResponseTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/AppResponseTest.java @@ -117,6 +117,9 @@ public class AppResponseTest { Assertions.assertEquals(1, response.getObjectAttachment("objectKey3")); Assertions.assertEquals(3, response.getObjectAttachments().size()); + response.getAttachments().put("objectKey4", "value4"); + Assertions.assertEquals(response.getAttachment("objectKey4"), "value4"); + HashMap map = new HashMap<>(); map.put("mapKey1", 1); map.put("mapKey2", "mapValue2"); diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java index 3c7b495b95..2c70a127e6 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java @@ -108,8 +108,13 @@ public class RpcContextTest { Assertions.assertNull(context.getAttachment("_33")); Assertions.assertEquals("3333", context.getAttachment(".33")); + context.getAttachments().put("44", "4444"); + Assertions.assertEquals(context.getAttachment("44"), "4444"); + context.clearAttachments(); Assertions.assertNull(context.getAttachment("_11")); + + } @Test diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcInvocationTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcInvocationTest.java index 168a42a8a8..f3050e31d3 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcInvocationTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcInvocationTest.java @@ -37,6 +37,10 @@ public class RpcInvocationTest { Assertions.assertEquals(1, invocation.getObjectAttachment("objectKey3")); Assertions.assertEquals(3, invocation.getObjectAttachments().size()); + invocation.getAttachments().put("object4", "value4"); + Assertions.assertEquals(invocation.getAttachment("object4"), "value4"); + + HashMap map = new HashMap<>(); map.put("mapKey1", 1); map.put("mapKey2", "mapValue2"); diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java index 2eb2ddd635..586c990bdd 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.rpc.support; -import org.apache.dubbo.common.utils.MapUtils; +import org.apache.dubbo.rpc.AttachmentsAdapter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -75,7 +75,7 @@ public class MockInvocation implements Invocation { } public Map getAttachments() { - return MapUtils.objectToStringMap(attachments); + return new AttachmentsAdapter.ObjectToStringMap(attachments); } @Override