Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: optimize the reflection operation in class SerializerServiceLoader #6780

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6761](https://github.com/apache/incubator-seata/pull/6761)] optimize the namingserver code to improve readability
- [[#6768](https://github.com/apache/incubator-seata/pull/6768)] report the tcc fence transaction isolation level
- [[#6770](https://github.com/apache/incubator-seata/pull/6770)] Automatic deletion of namingserver vgroup through Caffeine map
- [[#6780](https://github.com/apache/incubator-seata/pull/6780)] optimize the reflection operation in class `SerializerServiceLoader`

### refactor:

Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [[#6761](https://github.com/apache/incubator-seata/pull/6761)] 提升namingserver manager代码可读性
- [[#6768](https://github.com/apache/incubator-seata/pull/6768)] 上报tcc fence事务隔离级别
- [[#6770](https://github.com/apache/incubator-seata/pull/6770)] 通过caffeine map支持namingserver事务分组的过期删除
- [[#6780](https://github.com/apache/incubator-seata/pull/6780)] 优化类 `SerializerServiceLoader` 中的反射操作


### refactor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ public static Class<?> getClassByName(String className) throws ClassNotFoundExce
return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
}

/**
* Is class present boolean.
*
* @param className the class name
* @return boolean true if the class is present
*/
public static boolean isClassPresent(String className) {
try {
Class.forName(className, false, Thread.currentThread().getContextClassLoader());
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

/**
* Get the wrapped class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public void testGetClassByName() throws ClassNotFoundException {
ReflectionUtil.getClassByName("java.lang.String"));
}

@Test
public void testIsClassPresent() {
Assertions.assertTrue(ReflectionUtil.isClassPresent("java.lang.String"));
Assertions.assertFalse(ReflectionUtil.isClassPresent("java.lang.String2"));
}

@Test
public void testGetWrappedClass() {
Assertions.assertEquals(Byte.class, ReflectionUtil.getWrappedClass(byte.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private SerializerServiceLoader() {
}

private static final String PROTOBUF_SERIALIZER_CLASS_NAME = "org.apache.seata.serializer.protobuf.ProtobufSerializer";
private static final boolean CONTAINS_PROTOBUF_DEPENDENCY = ReflectionUtil.isClassPresent(PROTOBUF_SERIALIZER_CLASS_NAME);

/**
* Load the service of {@link Serializer}
Expand All @@ -65,15 +66,13 @@ private SerializerServiceLoader() {
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static Serializer load(SerializerType type, byte version) throws EnhancedServiceNotFoundException {
if (type == SerializerType.PROTOBUF) {
try {
ReflectionUtil.getClassByName(PROTOBUF_SERIALIZER_CLASS_NAME);
} catch (ClassNotFoundException e) {
throw new EnhancedServiceNotFoundException("'ProtobufSerializer' not found. " +
"Please manually reference 'org.apache.seata:seata-serializer-protobuf' dependency ", e);
}
// The following code is only used to kindly prompt users to add missing dependencies.
if (type == SerializerType.PROTOBUF && !CONTAINS_PROTOBUF_DEPENDENCY) {
throw new EnhancedServiceNotFoundException("The class '" + PROTOBUF_SERIALIZER_CLASS_NAME + "' not found. " +
"Please manually reference 'org.apache.seata:seata-serializer-protobuf' dependency.");
}


String key = serialzerKey(type, version);
Serializer serializer = SERIALIZER_MAP.get(key);
if (serializer == null) {
Expand Down
Loading