Skip to content

Commit

Permalink
Merge pull request #1435 from bokken/master
Browse files Browse the repository at this point in the history
retry looking up a class if trying to define it fails
  • Loading branch information
raphw authored May 4, 2023
2 parents fa4b2ac + 3913271 commit 92243f4
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1818,9 +1818,24 @@ public Map<String, Class<?>> injectRaw(Map<? extends String, byte[]> types) {
: classLoader) {
for (Map.Entry<? extends String, byte[]> entry : types.entrySet()) {
try {
// first try to load the class from the given class loader
result.put(entry.getKey(), Class.forName(entry.getKey(), false, classLoader));
} catch (ClassNotFoundException ignored) {
result.put(entry.getKey(), dispatcher.defineClass(classLoader, entry.getKey(), entry.getValue(), protectionDomain));
try {
// if the class does not exist, try to define it
result.put(entry.getKey(), dispatcher.defineClass(classLoader, entry.getKey(), entry.getValue(), protectionDomain));
} catch (Exception defFailure) {
// when the bootstrap loader is in use (i.e. the classloader is null), synchronizing on the BOOTSTRAP_LOADER_LOCK
// is sufficient to prevent this instance of ClassInjector from concurrently trying to define a class.
// however, in an environment where many ClassInjectors exist (either because shaded into multiple projects or
// it is present in multiple classloaders)
try {
result.put(entry.getKey(), Class.forName(entry.getKey(), false, classLoader));
} catch (ClassNotFoundException ignored2) {
//throw the failure from actually trying to define the class
throw defFailure;
}
}
}
}
}
Expand Down

0 comments on commit 92243f4

Please sign in to comment.