From 2471249b9c48153f69d7081e940447f32355e865 Mon Sep 17 00:00:00 2001 From: yangtu222 Date: Fri, 20 Apr 2018 12:56:47 +0800 Subject: [PATCH] fix: ClassCastException when in multi classLoader env. --- README.md | 5 ++++- pom.xml | 2 +- .../com/tuyang/beanutils/config/BeanCopyConfig.java | 10 ++++++++++ .../internal/javassist/JavassistBeanCopyFactory.java | 9 ++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 53c01b0..0461889 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a com.github.yangtu222 BeanUtils - 1.0.5 + 1.0.6 ~~~ @@ -247,6 +247,9 @@ name mapping/ignore/optionClass is the same to CopyProperty. If you want to use other class as default implementation class, you can change BeanCopyConfig's related properties. +* ClassLoader (1.0.6): You can specify a classLoader instance to BeanCopyConfig, otherwise BeanUtils will use toBean.getClass().getClassLoader() as the classLoader to avoid multi-classLoader issue. + + ## License [Apache-2.0 License](LICENSE) diff --git a/pom.xml b/pom.xml index 1498211..b3ed309 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.yangtu222 BeanUtils BeanUtils - 1.0.5 + 1.0.6 BeanUtils library is a Java bean copy utility with powerful functionality and high performance. https://github.com/yangtu222/BeanUtils diff --git a/src/main/java/com/tuyang/beanutils/config/BeanCopyConfig.java b/src/main/java/com/tuyang/beanutils/config/BeanCopyConfig.java index e90efd6..33d448b 100644 --- a/src/main/java/com/tuyang/beanutils/config/BeanCopyConfig.java +++ b/src/main/java/com/tuyang/beanutils/config/BeanCopyConfig.java @@ -61,6 +61,16 @@ public static enum DumpOption { private DumpOption dumpOption = DumpOption.AutoDumpAtFirstCopy; + private ClassLoader classLoader = null; + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + public static BeanCopyConfig instance() { return INSTANCE; } diff --git a/src/main/java/com/tuyang/beanutils/internal/javassist/JavassistBeanCopyFactory.java b/src/main/java/com/tuyang/beanutils/internal/javassist/JavassistBeanCopyFactory.java index 61ed693..1cc9aa0 100644 --- a/src/main/java/com/tuyang/beanutils/internal/javassist/JavassistBeanCopyFactory.java +++ b/src/main/java/com/tuyang/beanutils/internal/javassist/JavassistBeanCopyFactory.java @@ -37,6 +37,7 @@ import com.tuyang.beanutils.BeanCopier; import com.tuyang.beanutils.BeanCopyConvertor; import com.tuyang.beanutils.annotation.CopyFeature; +import com.tuyang.beanutils.config.BeanCopyConfig; import com.tuyang.beanutils.internal.cache.BeanCopyPropertyItem; import com.tuyang.beanutils.internal.factory.BeanCopierFactory; import com.tuyang.beanutils.internal.logger.Logger; @@ -257,7 +258,13 @@ else if( item.convertorClass != null ) { beanCopyCtClass.addMethod(copyBeanMethod); beanCopyCtClass.addInterface(beanCopyInterface); // beanCopyCtClass.writeFile("D:/tmp"); - Class classBeanCopy = beanCopyCtClass.toClass(); + + ClassLoader classLoader = BeanCopyConfig.instance().getClassLoader(); + if( classLoader == null ) { + classLoader = targetClass.getClassLoader(); + } + + Class classBeanCopy = beanCopyCtClass.toClass(classLoader, null); beanCopyCtClass.detach();