You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by si...@apache.org on 2022/04/22 00:36:56 UTC

[hudi] 09/15: Revert "Cleaning up `ReflectionUtils`"

This is an automated email from the ASF dual-hosted git repository.

sivabalan pushed a commit to branch release-0.11-0-apr21-5378-patched
in repository https://gitbox.apache.org/repos/asf/hudi.git

commit b77f026b5498d55cc26d2ab7ed9059801d0d37b2
Author: Alexey Kudinkin <al...@infinilake.com>
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);
   }
 
   /**