From 1368e21d9bb1afda195b0411091cb90003658456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=9D=E5=AE=9D=E7=88=B8=E5=B8=88?= <2267431887@qq.com> Date: Tue, 26 Dec 2023 17:31:44 +0800 Subject: [PATCH] optimize isProtobufClass method impl (#13557) * optimize isProtobufClass method impl * build error for unknown reason * fix build error --- dubbo-common/pom.xml | 5 ++ .../dubbo/common/utils/ProtobufUtils.java | 46 ++++++++++++++++++ .../dubbo/common/utils/ProtobufUtilsTest.java | 38 +++++++++++++++ .../apache/dubbo/rpc/model/HelloReply.java | 47 +++++++++++++++++++ .../apache/dubbo/rpc/model/HelloRequest.java | 47 +++++++++++++++++++ .../tri/ReflectionPackableMethod.java | 17 +------ 6 files changed, 184 insertions(+), 16 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/ProtobufUtilsTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloReply.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloRequest.java diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml index f59666e545..d0c050673a 100644 --- a/dubbo-common/pom.xml +++ b/dubbo-common/pom.xml @@ -99,5 +99,10 @@ javax.annotation javax.annotation-api + + com.google.protobuf + protobuf-java + test + diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java new file mode 100644 index 0000000000..a126fc61cd --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java @@ -0,0 +1,46 @@ +/* + * 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 org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; + +import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; + +public class ProtobufUtils { + + private static final Logger logger = LoggerFactory.getLogger(ProtobufUtils.class); + + private static Class protobufClss; + + private ProtobufUtils() {} + + static { + try { + protobufClss = ClassUtils.forName(PROTOBUF_MESSAGE_CLASS_NAME, ProtobufUtils.class.getClassLoader()); + } catch (Throwable t) { + logger.info("protobuf's dependency is absent"); + } + } + + public static boolean isProtobufClass(Class pojoClazz) { + if (protobufClss != null) { + return protobufClss.isAssignableFrom(pojoClazz); + } + return false; + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ProtobufUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ProtobufUtilsTest.java new file mode 100644 index 0000000000..e7ca7bc01b --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ProtobufUtilsTest.java @@ -0,0 +1,38 @@ +/* + * 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 org.apache.dubbo.common.vo.UserVo; +import org.apache.dubbo.rpc.model.HelloReply; +import org.apache.dubbo.rpc.model.HelloRequest; +import org.apache.dubbo.rpc.model.Person; +import org.apache.dubbo.rpc.model.SerializablePerson; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class ProtobufUtilsTest { + + @Test + void testIsProtobufClass() { + Assertions.assertTrue(ProtobufUtils.isProtobufClass(HelloRequest.class)); + Assertions.assertTrue(ProtobufUtils.isProtobufClass(HelloReply.class)); + Assertions.assertFalse(ProtobufUtils.isProtobufClass(Person.class)); + Assertions.assertFalse(ProtobufUtils.isProtobufClass(SerializablePerson.class)); + Assertions.assertFalse(ProtobufUtils.isProtobufClass(UserVo.class)); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloReply.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloReply.java new file mode 100644 index 0000000000..3ecd4585e5 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloReply.java @@ -0,0 +1,47 @@ +/* + * 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.rpc.model; + +import com.google.protobuf.Message; + +public final class HelloReply extends com.google.protobuf.GeneratedMessageV3 { + + @Override + protected FieldAccessorTable internalGetFieldAccessorTable() { + return null; + } + + @Override + protected Message.Builder newBuilderForType(BuilderParent builderParent) { + return null; + } + + @Override + public Message.Builder newBuilderForType() { + return null; + } + + @Override + public Message.Builder toBuilder() { + return null; + } + + @Override + public Message getDefaultInstanceForType() { + return null; + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloRequest.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloRequest.java new file mode 100644 index 0000000000..1ea1cc6136 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/HelloRequest.java @@ -0,0 +1,47 @@ +/* + * 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.rpc.model; + +import com.google.protobuf.Message; + +public final class HelloRequest extends com.google.protobuf.GeneratedMessageV3 { + + @Override + protected FieldAccessorTable internalGetFieldAccessorTable() { + return null; + } + + @Override + protected Message.Builder newBuilderForType(BuilderParent builderParent) { + return null; + } + + @Override + public Message.Builder newBuilderForType() { + return null; + } + + @Override + public Message.Builder toBuilder() { + return null; + } + + @Override + public Message getDefaultInstanceForType() { + return null; + } +} 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 4c19ca2d12..046410a20d 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 @@ -41,7 +41,7 @@ import java.util.stream.Stream; import com.google.protobuf.Message; import static org.apache.dubbo.common.constants.CommonConstants.$ECHO; -import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME; +import static org.apache.dubbo.common.utils.ProtobufUtils.isProtobufClass; public class ReflectionPackableMethod implements PackableMethod { @@ -269,21 +269,6 @@ public class ReflectionPackableMethod implements PackableMethod { return RX_RETURN_CLASS.equalsIgnoreCase(clz.getName()); } - static boolean isProtobufClass(Class clazz) { - while (clazz != Object.class && clazz != null) { - Class[] interfaces = clazz.getInterfaces(); - if (interfaces.length > 0) { - for (Class clazzInterface : interfaces) { - if (PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) { - return true; - } - } - } - clazz = clazz.getSuperclass(); - } - return false; - } - private static String convertHessianFromWrapper(String serializeType) { if (TripleConstant.HESSIAN4.equals(serializeType)) { return TripleConstant.HESSIAN2;