Don't inject helpers into the same classloader twice

This commit is contained in:
Andrew Kent 2017-12-15 10:08:45 -08:00
parent 887a864118
commit 069f481884
1 changed files with 11 additions and 5 deletions

View File

@ -18,6 +18,7 @@ import net.bytebuddy.utility.JavaModule;
public class HelperInjector implements Transformer { public class HelperInjector implements Transformer {
private final Set<String> helperClassNames; private final Set<String> helperClassNames;
private Map<TypeDescription, byte[]> helperMap = null; private Map<TypeDescription, byte[]> helperMap = null;
private final Set<ClassLoader> injectedClassLoaders = new HashSet<ClassLoader>();
/** /**
* Construct HelperInjector. * Construct HelperInjector.
@ -49,12 +50,17 @@ public class HelperInjector implements Transformer {
ClassLoader classLoader, ClassLoader classLoader,
JavaModule module) { JavaModule module) {
if (helperClassNames.size() > 0 && classLoader != null) { if (helperClassNames.size() > 0 && classLoader != null) {
synchronized (this) {
if (!injectedClassLoaders.contains(classLoader)) {
try { try {
new ClassInjector.UsingReflection(classLoader).inject(getHelperMap()); new ClassInjector.UsingReflection(classLoader).inject(getHelperMap());
} catch (ClassNotFoundException cnfe) { } catch (ClassNotFoundException cnfe) {
log.error("Failed to inject helper classes into " + classLoader, cnfe); log.error("Failed to inject helper classes into " + classLoader, cnfe);
throw new RuntimeException(cnfe); throw new RuntimeException(cnfe);
} }
injectedClassLoaders.add(classLoader);
}
}
} }
return builder; return builder;
} }