Skip to content

Commit

Permalink
fix(java): fix classloader get npe (apache#1792)
Browse files Browse the repository at this point in the history
## What does this PR do?

 fix classloader get npe

## Related issues

Closes apache#1763


## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?


## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
chaokunyang authored Aug 5, 2024
1 parent 0e18130 commit 497fe0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ public FuryBuilder withScalaOptimizationEnabled(boolean enableScalaOptimization)
private void finish() {
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = Fury.class.getClassLoader();
}
}
if (language != Language.JAVA) {
stringRefIgnored = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public class FuryPooledObjectFactory {

/** ThreadLocal: ClassLoader. */
private final ThreadLocal<ClassLoader> classLoaderLocal =
ThreadLocal.withInitial(() -> Thread.currentThread().getContextClassLoader());
ThreadLocal.withInitial(
() -> {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = Fury.class.getClassLoader();
}
return loader;
});

/**
* Dynamic capacity expansion and contraction The user sets the minimum number of object pools.
Expand Down Expand Up @@ -84,6 +91,7 @@ public FuryPooledObjectFactory(
public ClassLoaderFuryPooled getPooledCache() {
try {
ClassLoader classLoader = classLoaderLocal.get();
assert classLoader != null;
ClassLoaderFuryPooled classLoaderFuryPooled =
classLoaderFuryPooledCache.getIfPresent(classLoader);
if (classLoaderFuryPooled == null) {
Expand Down

0 comments on commit 497fe0a

Please sign in to comment.