Skip to content

Commit

Permalink
fix(java): fix ClassLoader npe in loadOrGenCodecClass (#1346)
Browse files Browse the repository at this point in the history
fix ClassLoader npe in `loadOrGenCodecClass` mentioned in #1340
  • Loading branch information
chaokunyang authored Jan 21, 2024
1 parent 9ec9adc commit 5b2b5e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,18 @@ protected Expression getOrCreateSerializer(Class<?> cls) {
Class<? extends Serializer> serializerClass =
visitFury(f -> f.getClassResolver().getSerializerClass(cls));
Preconditions.checkNotNull(serializerClass, "Unsupported for class " + cls);
ClassLoader beanClassClassLoader =
beanClass.getClassLoader() == null
? Thread.currentThread().getContextClassLoader()
: beanClass.getClassLoader();
if (!ReflectionUtils.isPublic(serializerClass)) {
// TODO(chaokunyang) add jdk17+ unexported class check.
// non-public class can't be accessed in generated class.
serializerClass = Serializer.class;
} else {
ClassLoader beanClassClassLoader = beanClass.getClassLoader();
if (beanClassClassLoader == null) {
beanClassClassLoader = Thread.currentThread().getContextClassLoader();
if (beanClassClassLoader == null) {
beanClassClassLoader = Fury.class.getClassLoader();
}
}
try {
beanClassClassLoader.loadClass(serializerClass.getName());
} catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static <T> Class<? extends Serializer<T>> loadOrGenCodecClass(
beanClass.getClassLoader() == null
? Thread.currentThread().getContextClassLoader()
: beanClass.getClassLoader();
if (beanClassClassLoader == null) {
beanClassClassLoader = fury.getClass().getClassLoader();
}
try {
// generated code imported fury classes.
beanClassClassLoader.loadClass(Fury.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public void returnFury(Fury fury) {

/** todo setClassLoader support LoaderBinding.StagingType */
public void setClassLoader(ClassLoader classLoader, LoaderBinding.StagingType stagingType) {
if (classLoader == null) {
// may be used to clear some classloader
classLoader = Fury.class.getClassLoader();
}
classLoaderLocal.set(classLoader);
getOrAddCache(classLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public void setClassLoader(ClassLoader classLoader) {
*/
public void setClassLoader(ClassLoader classLoader, StagingType stagingType) {
if (this.loader != classLoader) {
if (classLoader == null) {
// may be used to clear some classloader
classLoader = Fury.class.getClassLoader();
}
this.loader = classLoader;
switch (stagingType) {
case NO_STAGING:
Expand Down

0 comments on commit 5b2b5e3

Please sign in to comment.