This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 6981a0f847 Fix ClassPool issue when multi-classloader (#13015)
6981a0f847 is described below
commit 6981a0f8473350d5867c3736b61455cd30e7a25c
Author: wien13 <[email protected]>
AuthorDate: Mon Sep 25 19:36:39 2023 +0800
Fix ClassPool issue when multi-classloader (#13015)
---
.../dubbo/common/bytecode/ClassGenerator.java | 13 ++++++---
.../org/apache/dubbo/common/bytecode/Wrapper.java | 6 +---
.../dubbo/common/bytecode/ClassGeneratorTest.java | 33 ++++++++++++++++++++++
3 files changed, 43 insertions(+), 9 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
index b665c57075..f6a12df2ef 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
@@ -92,10 +92,15 @@ public final class ClassGenerator {
ClassPool pool = POOL_MAP.get(loader);
if (pool == null) {
- pool = new ClassPool(true);
- pool.insertClassPath(new LoaderClassPath(loader));
- pool.insertClassPath(new DubboLoaderClassPath());
- POOL_MAP.put(loader, pool);
+ synchronized (POOL_MAP) {
+ pool = POOL_MAP.get(loader);
+ if (pool == null) {
+ pool = new ClassPool(true);
+ pool.insertClassPath(new LoaderClassPath(loader));
+ pool.insertClassPath(new DubboLoaderClassPath());
+ POOL_MAP.put(loader, pool);
+ }
+ }
}
return pool;
}
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
index bde471afae..d5be038cb5 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
@@ -22,8 +22,6 @@ import org.apache.dubbo.common.utils.ReflectUtils;
import javassist.ClassPool;
import javassist.CtMethod;
-import javassist.LoaderClassPath;
-
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -158,9 +156,7 @@ public abstract class Wrapper {
pts.put(fn, ft);
}
- final ClassPool classPool = new ClassPool(ClassPool.getDefault());
- classPool.insertClassPath(new LoaderClassPath(cl));
- classPool.insertClassPath(new DubboLoaderClassPath());
+ final ClassPool classPool = ClassGenerator.getClassPool(cl);
List<String> allMethod = new ArrayList<>();
try {
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java
index 607f6010c1..2eed4129aa 100644
---
a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.dubbo.common.bytecode;
+import javassist.ClassPool;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -23,6 +24,9 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
interface Builder<T> {
T getName(Bean bean);
@@ -197,6 +201,35 @@ class ClassGeneratorTest {
builder.setName(b, "ok");
System.out.println(b.getName());
}
+
+ @Test
+ public void test_getClassPool() throws InterruptedException {
+ int threadCount = 5;
+ CountDownLatch LATCH = new CountDownLatch(threadCount);
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ List<Integer> hashCodeList = new ArrayList<>();
+ for (int i = 0; i < threadCount; i++) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ ClassPool classPool = ClassGenerator.getClassPool(loader);
+ int currentHashCode = classPool.hashCode();
+ hashCodeList.add(currentHashCode);
+ System.out.println(currentHashCode);
+ LATCH.countDown();
+ }
+ }).start();
+ }
+ LATCH.await();
+ Integer firstHashCode = null;
+ for (Integer currentHashCode : hashCodeList) {
+ if (firstHashCode == null) {
+ firstHashCode = currentHashCode;
+ continue;
+ }
+ Assertions.assertTrue(firstHashCode.intValue() ==
currentHashCode.intValue());
+ }
+ }
}
class Bean {