Skip to content

Commit

Permalink
Use application classloader for proxies (#419)
Browse files Browse the repository at this point in the history
Signed-off-by: tevans <tevans@uk.ibm.com>
  • Loading branch information
tevans78 authored Oct 14, 2020
1 parent d86322e commit 3f5e25f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ protected ConfigMappingInterface computeValue(final Class<?> type) {
ConfigMappingInterface(final Class<?> interfaceType, final ConfigMappingInterface[] superTypes,
final Property[] properties) {
this.interfaceType = interfaceType;
this.className = getClass().getPackage().getName() + "." + interfaceType.getSimpleName()
+ interfaceType.getName().hashCode() + "Impl";
this.className = interfaceType.getName() + interfaceType.getName().hashCode() + "Impl";
this.superTypes = superTypes;
this.properties = properties;
this.propertiesByName = Stream.of(properties).collect(toMap(p -> p.getMethod().getName(), p -> p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ static <T> T configMappingObject(Class<T> interfaceType, ConfigMappingContext co
@SuppressWarnings("unchecked")
static <T> Class<? extends ConfigMappingObject> getImplementationClass(Class<T> type) {
final ConfigMappingMetadata mappingMetadata = ConfigMappingInterface.getConfigurationInterface(type);
return (Class<? extends ConfigMappingObject>) loadClass(type.getClassLoader(),
return (Class<? extends ConfigMappingObject>) loadClass(type,
mappingMetadata.getClassName(),
mappingMetadata.getClassBytes());
}

static Class<?> loadClass(final ClassLoader classLoader, final String className, final byte[] classBytes) {
static Class<?> loadClass(final Class<?> parent, final String className, final byte[] classBytes) {
// Check if the interface implementation was already loaded. If not we will load it.
try {
return classLoader.loadClass(className);
return parent.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
return loadClass(className, classBytes);
return defineClass(parent, className, classBytes);
}
}

private static Class<?> loadClass(final String className, final byte[] classBytes) {
return ClassDefiner.defineClass(LOOKUP, ConfigMappingLoader.class, className, classBytes);
private static Class<?> defineClass(final Class<?> parent, final String className, final byte[] classBytes) {
return ClassDefiner.defineClass(LOOKUP, parent, className, classBytes);
}

private static final class ConfigMappingObjectHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void multipleLoads() {
void loadManually() {
List<ConfigMappingMetadata> configMappingsMetadata = ConfigMappingLoader.getConfigMappingsMetadata(ServerManual.class);
configMappingsMetadata.forEach(
mappingMetadata -> ConfigMappingLoader.loadClass(ServerManual.class.getClassLoader(),
mappingMetadata -> ConfigMappingLoader.loadClass(ServerManual.class,
mappingMetadata.getClassName(), mappingMetadata.getClassBytes()));
ConfigMappingLoader.getImplementationClass(ServerManual.class);
ConfigMappingLoader.getImplementationClass(ServerManual.class);
Expand Down

0 comments on commit 3f5e25f

Please sign in to comment.