You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by su...@apache.org on 2020/09/30 21:23:22 UTC

[hadoop] branch trunk updated: HADOOP-17287. Support new Instance by non default constructor by ReflectionUtils (#2341)

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

sunchao pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new d68d2a5  HADOOP-17287. Support new Instance by non default constructor by ReflectionUtils (#2341)
d68d2a5 is described below

commit d68d2a5c1ed53bcab546f6f870cf2084d22ca498
Author: maobaolong <30...@qq.com>
AuthorDate: Thu Oct 1 05:22:55 2020 +0800

    HADOOP-17287. Support new Instance by non default constructor by ReflectionUtils (#2341)
---
 .../org/apache/hadoop/util/ReflectionUtils.java    | 24 ++++++++++++++++--
 .../apache/hadoop/util/TestReflectionUtils.java    | 29 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
index 1ae71d1..31b0546 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
@@ -124,15 +124,35 @@ public class ReflectionUtils {
    */
   @SuppressWarnings("unchecked")
   public static <T> T newInstance(Class<T> theClass, Configuration conf) {
+    return newInstance(theClass, conf, EMPTY_ARRAY);
+  }
+
+  /** Create an object for the given class and initialize it from conf
+   *
+   * @param theClass class of which an object is created
+   * @param conf Configuration
+   * @param argTypes the types of the arguments
+   * @param values the values of the arguments
+   * @return a new object
+   */
+  @SuppressWarnings("unchecked")
+  public static <T> T newInstance(Class<T> theClass, Configuration conf,
+      Class<?>[] argTypes, Object ... values) {
     T result;
+    if (argTypes.length != values.length) {
+      throw new IllegalArgumentException(argTypes.length
+          + " parameters are required but "
+          + values.length
+          + " arguments are provided");
+    }
     try {
       Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass);
       if (meth == null) {
-        meth = theClass.getDeclaredConstructor(EMPTY_ARRAY);
+        meth = theClass.getDeclaredConstructor(argTypes);
         meth.setAccessible(true);
         CONSTRUCTOR_CACHE.put(theClass, meth);
       }
-      result = meth.newInstance();
+      result = meth.newInstance(values);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
index 62cd625..03c67c8 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
@@ -28,6 +28,7 @@ import java.util.List;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.*;
 
+import org.apache.hadoop.test.GenericTestUtils;
 import org.apache.hadoop.test.GenericTestUtils.LogCapturer;
 import org.junit.Before;
 import org.junit.Test;
@@ -168,6 +169,34 @@ public class TestReflectionUtils {
         containsString("Process Thread Dump: " + title));
   }
 
+  @Test
+  public void testNewInstanceForNonDefaultConstructor() {
+    Object x = ReflectionUtils.newInstance(
+        NoDefaultCtor.class, null, new Class[] {int.class}, 1);
+    assertTrue(x instanceof NoDefaultCtor);
+  }
+
+  @Test
+  public void testNewInstanceForNonDefaultConstructorWithException() {
+    try {
+      ReflectionUtils.newInstance(
+          NoDefaultCtor.class, null, new Class[]{int.class}, 1, 2);
+      fail("Should have failed before this point");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "1 parameters are required but 2 arguments are provided", e);
+    }
+
+    try {
+      ReflectionUtils.newInstance(
+          NoDefaultCtor.class, null, new Class[]{int.class});
+      fail("Should have failed before this point");
+    } catch (IllegalArgumentException e) {
+      GenericTestUtils.assertExceptionContains(
+          "1 parameters are required but 0 arguments are provided", e);
+    }
+  }
+
   // Used for testGetDeclaredFieldsIncludingInherited
   private class Parent {
     private int parentField;


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org