tiagomlalves commented on code in PR #3982:
URL: https://github.com/apache/cassandra/pull/3982#discussion_r1995571929
##########
src/java/org/apache/cassandra/config/ParameterizedClass.java:
##########
@@ -74,32 +76,61 @@ static public <K> K newInstance(ParameterizedClass
parameterizedClass, List<Stri
if (!searchPackage.isEmpty() && !searchPackage.endsWith("."))
searchPackage = searchPackage + '.';
String name = searchPackage + parameterizedClass.class_name;
- Class<?> providerClass = Class.forName(name);
- try
- {
- Constructor<?> constructor =
providerClass.getConstructor(Map.class);
- K instance = (K)
constructor.newInstance(parameterizedClass.parameters);
- return instance;
- }
- catch (Exception constructorEx)
- {
- //no-op
- }
- // fallback to no arg constructor if no params present
- if (parameterizedClass.parameters == null ||
parameterizedClass.parameters.isEmpty())
- {
- Constructor<?> constructor =
providerClass.getConstructor();
- K instance = (K) constructor.newInstance();
- return instance;
- }
+ providerClass = Class.forName(name);
}
- // there are about 5 checked exceptions that could be thrown here.
- catch (Exception e)
+ catch (ClassNotFoundException e)
{
- last = e;
+ //no-op
}
}
- throw new ConfigurationException("Unable to create parameterized class
" + parameterizedClass.class_name, last);
+
+ if (providerClass == null)
+ {
+ String pkgList = '[' + searchPackages.stream().map(p -> '"' + p +
'"').collect(Collectors.joining(",")) + ']';
+ String error = "Unable to find class " +
parameterizedClass.class_name + " in packages " + pkgList;
+ throw new ConfigurationException(error);
+ }
+
+ try
+ {
+ Constructor<?> mapConstructor = filterConstructor(providerClass, c
-> c.getParameterTypes().length == 1 &&
c.getParameterTypes()[0].equals(Map.class));
+ if (mapConstructor != null)
+ return (K)
mapConstructor.newInstance(parameterizedClass.parameters == null ?
Collections.emptyMap() : parameterizedClass.parameters);
+
+ // Falls-back to no-arg constructor
+ Constructor<?> noArgsConstructor =
filterConstructor(providerClass, c -> c.getParameterTypes().length == 0);
+ if (noArgsConstructor != null)
+ {
+ if (parameterizedClass.parameters != null &&
!parameterizedClass.parameters.isEmpty())
+ throw new ConfigurationException("Invalid parameters for
class " + parameterizedClass.class_name);
Review Comment:
@smiklosovic these two lines need to be removed to allow instantiating
classes with empty constructor with parameters.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]