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:51 UTC
[hudi] 04/15: Added new reflection utisl to instantiate already loaded class
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 1cbc580f3a3db16142a42f0d8a8dbcf9bedbc063
Author: Alexey Kudinkin <al...@infinilake.com>
AuthorDate: Wed Apr 20 15:19:58 2022 -0700
Added new reflection utisl to instantiate already loaded class
---
.../apache/hudi/common/util/ReflectionUtils.java | 44 +++++++++++++++++-----
1 file changed, 35 insertions(+), 9 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 a4ef09641d..ec361d9f9a 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,6 +38,8 @@ 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.
*/
@@ -85,11 +87,14 @@ 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) {
- try {
- return getClass(clazz).getConstructor(constructorArgTypes).newInstance(constructorArgs);
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
- throw new HoodieException("Unable to instantiate class " + clazz, e);
- }
+ 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);
}
/**
@@ -111,11 +116,32 @@ public class ReflectionUtils {
}
/**
- * Creates an instance of the given class. Constructor arg types are inferred.
+ * 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 Object loadClass(String clazz, Object... constructorArgs) {
- Class<?>[] constructorArgTypes = Arrays.stream(constructorArgs).map(Object::getClass).toArray(Class<?>[]::new);
- return loadClass(clazz, constructorArgTypes, constructorArgs);
+ public static <T> T newInstanceUnchecked(Class<T> klass, Object ...ctorArgs) {
+ Class<?>[] ctorArgTypes = Arrays.stream(ctorArgs).map(Object::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
+ */
+ 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);
+ }
}
/**