Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using string concatenation to forge impl name #1211

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Comment on lines +130 to +131
Copy link
Contributor Author

@gsmet gsmet Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW to clarify, the point was to fix the issue here. But... I broke the tests by doing so because ConfigMappingInterface had the same code. So I made sure they would share the code now.

if (type.isAssignableFrom(implementationClass)) {
return (Class<? extends ConfigMappingObject>) implementationClass;
}
Expand Down