Maxwell-Guo commented on code in PR #3931: URL: https://github.com/apache/cassandra/pull/3931#discussion_r1975473553
########## src/java/org/apache/cassandra/config/ParameterizedClass.java: ########## @@ -74,32 +77,53 @@ 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 error = "Unable to find class " + parameterizedClass.class_name + " in packages [" + + searchPackages.stream().map(p -> '"' + p + '"').collect(Collectors.joining(",")) + ']'; + throw new ConfigurationException(error); + } + + try + { + Constructor<?>[] declaredConstructors = providerClass.getDeclaredConstructors(); + + Constructor mapConstructor = Arrays.stream(declaredConstructors) + .filter(c -> c.getParameterTypes().length == 1 && c.getParameterTypes()[0].equals(Map.class)) Review Comment: `filter(c -> c.getParameterTypes().length == 1 && c.getParameterTypes()[0]` can this filter cover all the cases we have ? That is to say do these providerClasses not matter what we already have or we are going to implement (if we are going to have a new auth class) meet the requirements of such a constructor ? ########## test/unit/org/apache/cassandra/config/ParameterizedClassTest.java: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.config; + +import java.util.List; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.*; + +import org.apache.cassandra.auth.AllowAllAuthorizer; +import org.apache.cassandra.auth.IAuthorizer; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.junit.rules.ExpectedException; + +public class ParameterizedClassTest +{ + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Test + public void newInstance_NonExistentClass_FailsWithConfigurationException() + { + exceptionRule.expect(ConfigurationException.class); + + String expectedError = "Unable to find class NonExistentClass in packages [\"org.apache.cassandra.config\"]"; + exceptionRule.expectMessage(expectedError); + + ParameterizedClass parameterizedClass = new ParameterizedClass("NonExistentClass"); + ParameterizedClass.newInstance(parameterizedClass, List.of("org.apache.cassandra.config")); + fail(); + } + + @Test + public void newInstance_WithSingleEmptyConstructor_UsesEmptyConstructor() + { + ParameterizedClass parameterizedClass = new ParameterizedClass(AllowAllAuthorizer.class.getName()); + IAuthorizer instance = ParameterizedClass.newInstance(parameterizedClass, null); + assertNotNull(instance); + } + + @Test + public void newInstance_SingleEmptyConstructorWithParameters_FailsWithConfigurationException() + { + exceptionRule.expect(ConfigurationException.class); + exceptionRule.expectMessage(startsWith("No valid constructor found for class")); + + Map<String, String> parameters = Map.of("key", "value"); + ParameterizedClass parameterizedClass = new ParameterizedClass(AllowAllAuthorizer.class.getName(), parameters); + + ParameterizedClass.newInstance(parameterizedClass, null); + fail(); + } + + @Test + public void newInstance_WithValidConstructors_FavorsMapConstructor() + { + ParameterizedClass parameterizedClass = new ParameterizedClass(ParameterizedClassExample.class.getName()); + ParameterizedClassExample instance = ParameterizedClass.newInstance(parameterizedClass, null); + + assertTrue(instance.calledMapConstructor); + } + + @Test + public void newInstance_WithConstructorException_PreservesOriginalFailure() Review Comment: I don't know wether this kind of function naming is ok ? ########## test/unit/org/apache/cassandra/config/ParameterizedClassTest.java: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.config; + +import java.util.List; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.*; Review Comment: please expand the imports -- 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: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org