diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java index 790d8076056..39e16fa7cdd 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.serialize.MultipleSerialization; import org.apache.dubbo.common.serialize.support.DefaultSerializationSelector; import org.apache.dubbo.common.stream.StreamObserver; -import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.config.Constants; import org.apache.dubbo.rpc.model.MethodDescriptor; import org.apache.dubbo.rpc.model.PackableMethod; @@ -34,8 +33,7 @@ import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Stream; import static org.apache.dubbo.common.constants.CommonConstants.$ECHO; import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; @@ -93,7 +91,8 @@ public ReflectionPackableMethod(MethodDescriptor method, URL url, String seriali .getExtension(url.getParameter(Constants.MULTI_SERIALIZATION_KEY, CommonConstants.DEFAULT_KEY)); - this.requestPack = new WrapRequestPack(serialization, url, serializeName, singleArgument); + this.requestPack = new WrapRequestPack(serialization, url, serializeName, actualRequestTypes, + singleArgument); this.responsePack = new WrapResponsePack(serialization, url, actualResponseType); this.requestUnpack = new WrapRequestUnpack(serialization, url, actualRequestTypes); this.responseUnpack = new WrapResponseUnpack(serialization, url, actualResponseType); @@ -323,16 +322,10 @@ private WrapResponsePack(MultipleSerialization multipleSerialization, URL url, @Override public byte[] pack(Object obj) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - Class clz; - if (obj != null) { - clz = obj.getClass(); - } else { - clz = actualResponseType; - } - multipleSerialization.serialize(url, serialize, clz, obj, bos); + multipleSerialization.serialize(url, serialize, actualResponseType, obj, bos); return TripleCustomerProtocolWapper.TripleResponseWrapper.Builder.newBuilder() .setSerializeType(serialize) - .setType(clz.getName()) + .setType(actualResponseType.getName()) .setData(bos.toByteArray()) .build() .toByteArray(); @@ -341,17 +334,15 @@ public byte[] pack(Object obj) throws IOException { private static class WrapResponseUnpack implements UnPack { - private final Map> classCache = new ConcurrentHashMap<>(); - private final MultipleSerialization serialization; private final URL url; + private final Class returnClass; - private final Class actualResponseType; - private WrapResponseUnpack(MultipleSerialization serialization, URL url, Class actualResponseType) { + private WrapResponseUnpack(MultipleSerialization serialization, URL url, Class returnClass) { this.serialization = serialization; this.url = url; - this.actualResponseType = actualResponseType; + this.returnClass = returnClass; } @Override @@ -360,8 +351,7 @@ public Object unpack(byte[] data) throws IOException, ClassNotFoundException { .parseFrom(data); final String serializeType = convertHessianFromWrapper(wrapper.getSerializeType()); ByteArrayInputStream bais = new ByteArrayInputStream(wrapper.getData()); - Class clz = getClassFromCache(wrapper.getType(), classCache, actualResponseType); - return serialization.deserialize(url, serializeType, clz, bais); + return serialization.deserialize(url, serializeType, returnClass, bais); } } @@ -369,16 +359,19 @@ private static class WrapRequestPack implements Pack { private final String serialize; private final MultipleSerialization multipleSerialization; + private final String[] argumentsType; private final URL url; private final boolean singleArgument; private WrapRequestPack(MultipleSerialization multipleSerialization, URL url, String serialize, + Class[] actualRequestTypes, boolean singleArgument) { this.url = url; this.serialize = convertHessianToWrapper(serialize); this.multipleSerialization = multipleSerialization; + this.argumentsType = Stream.of(actualRequestTypes).map(Class::getName).toArray(String[]::new); this.singleArgument = singleArgument; } @@ -392,8 +385,10 @@ public byte[] pack(Object obj) throws IOException { } final TripleCustomerProtocolWapper.TripleRequestWrapper.Builder builder = TripleCustomerProtocolWapper.TripleRequestWrapper.Builder.newBuilder(); builder.setSerializeType(serialize); + for (String type : argumentsType) { + builder.addArgTypes(type); + } for (Object argument : arguments) { - builder.addArgTypes(argument.getClass().getName()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); multipleSerialization.serialize(url, serialize, argument.getClass(), argument, bos); builder.addArgs(bos.toByteArray()); @@ -436,8 +431,6 @@ public byte[] pack(Object obj) throws IOException { private class WrapRequestUnpack implements UnPack { - private final Map> classCache = new ConcurrentHashMap<>(); - private final MultipleSerialization serialization; private final URL url; @@ -458,32 +451,11 @@ public Object unpack(byte[] data) throws IOException, ClassNotFoundException { for (int i = 0; i < wrapper.getArgs().size(); i++) { ByteArrayInputStream bais = new ByteArrayInputStream( wrapper.getArgs().get(i)); - String className = wrapper.getArgTypes().get(i); - Class clz = getClassFromCache(className, classCache, actualRequestTypes[i]); - ret[i] = serialization.deserialize(url, wrapper.getSerializeType(), clz, bais); + ret[i] = serialization.deserialize(url, wrapper.getSerializeType(), + actualRequestTypes[i], + bais); } return ret; } - - - } - - - private static Class getClassFromCache(String className, Map> classCache, Class expectedClass) { - if (expectedClass.getName().equals(className)) { - return expectedClass; - } - - Class clz = classCache.get(className); - if (clz == null) { - try { - clz = ClassUtils.forName(className); - } catch (Exception e) { - // To catch IllegalStateException, LinkageError, ClassNotFoundException - clz = expectedClass; - } - classCache.put(className, clz); - } - return clz; } } diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/MultipleSerialization.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/MultipleSerialization.java index f72a769c7ab..55b67fcad6f 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/MultipleSerialization.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/MultipleSerialization.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionScope; import org.apache.dubbo.common.extension.SPI; -import org.apache.dubbo.common.utils.ClassUtils; import java.io.IOException; import java.io.InputStream; @@ -29,22 +28,8 @@ @SPI(scope = ExtensionScope.FRAMEWORK) public interface MultipleSerialization { - - default void serialize(URL url, String serializeType, String clzStr, Object obj, OutputStream os) throws IOException, ClassNotFoundException { - Class clz = ClassUtils.forName(clzStr); - serialize(url, serializeType, clz, obj, os); - } - - void serialize(URL url, String serializeType, Class clz, Object obj, OutputStream os) throws IOException; - - default Object deserialize(URL url, String serializeType, String clzStr, InputStream os) throws IOException, ClassNotFoundException { - Class clz = ClassUtils.forName(clzStr); - return deserialize(url, serializeType, clz, os); - } - - Object deserialize(URL url, String serializeType, Class clz, InputStream os) throws IOException, ClassNotFoundException; }