From 8686ba04a113dcc0191289012e8e0d17492afcbb Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Sun, 25 Aug 2024 18:17:47 +0200 Subject: [PATCH] Avoid using string concatenation to forge impl name Also avoid using hashCode(). My understanding is that it was used to make sure the name wouldn't clash but I think it's better to use an approach to what CDI did. --- .../io/smallrye/config/ConfigMappingInterface.java | 11 ++++++++++- .../java/io/smallrye/config/ConfigMappingLoader.java | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java index 8704ebc35..b34d6e9c8 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java @@ -47,7 +47,7 @@ protected ConfigMappingInterface computeValue(final Class type) { ConfigMappingInterface(final Class interfaceType, final ConfigMappingInterface[] superTypes, final Property[] properties) { this.interfaceType = interfaceType; - this.className = interfaceType.getName() + interfaceType.getName().hashCode() + "Impl"; + this.className = getImplementationClassName(interfaceType); this.superTypes = superTypes; List filteredProperties = new ArrayList<>(); @@ -67,6 +67,15 @@ protected ConfigMappingInterface computeValue(final Class type) { this.toStringMethod = toStringMethod; } + static String getImplementationClassName(Class type) { + // do not use string concatenation here + // make sure the impl name doesn't clash with potential user classes + String className = type.getName(); + StringBuilder implementationClassName = new StringBuilder(className.length() + 8); + implementationClassName.append(className).append("$$CMImpl"); + return implementationClassName.toString(); + } + /** * Get the configuration interface information for the given interface class. This information is cached. * diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingLoader.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingLoader.java index bb9c85023..bbbde4079 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingLoader.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingLoader.java @@ -127,7 +127,8 @@ static T configMappingObject(final Class interfaceType, final ConfigMappi @SuppressWarnings("unchecked") public static Class getImplementationClass(final Class type) { try { - Class implementationClass = type.getClassLoader().loadClass(type.getName() + type.getName().hashCode() + "Impl"); + Class implementationClass = type.getClassLoader() + .loadClass(ConfigMappingInterface.getImplementationClassName(type)); if (type.isAssignableFrom(implementationClass)) { return (Class) implementationClass; }