optimize isProtobufClass method impl (#13557)

* optimize isProtobufClass method impl

* build error for unknown reason

* fix build error
This commit is contained in:
宝宝爸师 2023-12-26 17:31:44 +08:00 committed by GitHub
parent 738f3ff69a
commit 1368e21d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 184 additions and 16 deletions

View File

@ -99,5 +99,10 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;