Skip to content

Commit

Permalink
Avoid using string concatenation to forge impl name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gsmet committed Aug 25, 2024
1 parent 491069d commit 8686ba0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Property> filteredProperties = new ArrayList<>();
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ static <T> T configMappingObject(final Class<T> interfaceType, final ConfigMappi
@SuppressWarnings("unchecked")
public static <T> Class<? extends ConfigMappingObject> getImplementationClass(final Class<T> 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<? extends ConfigMappingObject>) implementationClass;
}
Expand Down

0 comments on commit 8686ba0

Please sign in to comment.