Skip to content

Commit

Permalink
fix: ClassCastException when in multi classLoader env.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtu222 committed Apr 20, 2018
1 parent 18a20e4 commit 2471249
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a
<dependency>
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>
~~~

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<name>BeanUtils</name>
<version>1.0.5</version>
<version>1.0.6</version>
<description>BeanUtils library is a Java bean copy utility with powerful functionality and high performance.</description>
<url>https://github.com/yangtu222/BeanUtils</url>

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/tuyang/beanutils/config/BeanCopyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -257,7 +258,13 @@ else if( item.convertorClass != null ) {
beanCopyCtClass.addMethod(copyBeanMethod);
beanCopyCtClass.addInterface(beanCopyInterface);
// beanCopyCtClass.writeFile("D:/tmp");
Class<BeanCopier> classBeanCopy = beanCopyCtClass.toClass();

ClassLoader classLoader = BeanCopyConfig.instance().getClassLoader();
if( classLoader == null ) {
classLoader = targetClass.getClassLoader();
}

Class<BeanCopier> classBeanCopy = beanCopyCtClass.toClass(classLoader, null);

beanCopyCtClass.detach();

Expand Down

0 comments on commit 2471249

Please sign in to comment.