This is an automated email from the ASF dual-hosted git repository. xushiyan pushed a commit to branch rc3-patched-for-test in repository https://gitbox.apache.org/repos/asf/hudi.git
commit b8a1c28bf91b7d6cc6ffe04de7c8469028ec057e Author: Alexey Kudinkin <[email protected]> AuthorDate: Wed Apr 20 22:22:47 2022 -0700 Revert "Cleaning up `ReflectionUtils`" This reverts commit 6775bded7861bef7930124477440abe72e004ad4. --- .../apache/hudi/common/util/ReflectionUtils.java | 46 +++++----------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java b/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java index 8ac4b0fa58..a4ef09641d 100644 --- a/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java +++ b/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java @@ -38,8 +38,6 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Stream; -import static org.apache.hudi.TypeUtils.unsafeCast; - /** * A utility class for reflection. */ @@ -87,14 +85,11 @@ public class ReflectionUtils { * Creates an instance of the given class. Use this version when dealing with interface types as constructor args. */ public static Object loadClass(String clazz, Class<?>[] constructorArgTypes, Object... constructorArgs) { - return newInstanceUnchecked(getClass(clazz), constructorArgTypes, constructorArgs); - } - - /** - * Creates an instance of the given class. Constructor arg types are inferred. - */ - public static Object loadClass(String clazz, Object... constructorArgs) { - return newInstanceUnchecked(getClass(clazz), constructorArgs); + try { + return getClass(clazz).getConstructor(constructorArgTypes).newInstance(constructorArgs); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new HoodieException("Unable to instantiate class " + clazz, e); + } } /** @@ -116,34 +111,11 @@ public class ReflectionUtils { } /** - * Creates a new instance of provided {@link Class} by invoking ctor identified by the - * provided specific arguments - * - * @param klass target class to instantiate - * @param ctorArgs specific constructor arguments - * @return new instance of the class - */ - public static <T> T newInstanceUnchecked(Class<T> klass, Object... ctorArgs) { - Class<?>[] ctorArgTypes = Arrays.stream(ctorArgs) - .map(arg -> Objects.requireNonNull(arg).getClass()) - .toArray(Class<?>[]::new); - return newInstanceUnchecked(klass, ctorArgTypes, ctorArgs); - } - - /** - * Creates a new instance of provided {@link Class} by invoking ctor identified by the - * provided specific arguments - * - * @param klass target class to instantiate - * @param ctorArgs specific constructor arguments - * @return new instance of the class + * Creates an instance of the given class. Constructor arg types are inferred. */ - public static <T> T newInstanceUnchecked(Class<T> klass, Class<?>[] ctorArgTypes, Object... ctorArgs) { - try { - return unsafeCast(klass.getConstructor(ctorArgTypes).newInstance(ctorArgs)); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - throw new HoodieException(String.format("Unable to instantiate class %s", klass.getSimpleName()), e); - } + public static Object loadClass(String clazz, Object... constructorArgs) { + Class<?>[] constructorArgTypes = Arrays.stream(constructorArgs).map(Object::getClass).toArray(Class<?>[]::new); + return loadClass(clazz, constructorArgTypes, constructorArgs); } /**
