You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by ab...@apache.org on 2014/08/12 08:36:43 UTC
git commit: SQOOP-1425: Sqoop2: Improve ClassUtils to enable use of
primitive types and subclasses
Repository: sqoop
Updated Branches:
refs/heads/SQOOP-1367 199d342df -> bbe35ea36
SQOOP-1425: Sqoop2: Improve ClassUtils to enable use of primitive types and subclasses
Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/bbe35ea3
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/bbe35ea3
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/bbe35ea3
Branch: refs/heads/SQOOP-1367
Commit: bbe35ea36d1e805b84e7afae7933f27fad696192
Parents: 199d342
Author: Jarek Jarcec Cecho <ja...@apache.org>
Authored: Mon Aug 11 23:34:24 2014 -0700
Committer: Abraham Elmahrek <ab...@elmahrek.com>
Committed: Mon Aug 11 23:34:24 2014 -0700
----------------------------------------------------------------------
.../java/org/apache/sqoop/utils/ClassUtils.java | 31 ++-
.../org/apache/sqoop/utils/TestClassUtils.java | 189 +++++++++++--------
2 files changed, 128 insertions(+), 92 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/sqoop/blob/bbe35ea3/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java b/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
index 970afe9..eca9f7e 100644
--- a/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
+++ b/common/src/main/java/org/apache/sqoop/utils/ClassUtils.java
@@ -95,23 +95,20 @@ public final class ClassUtils {
return null;
}
- Class []argumentTypes = new Class[args.length];
- for(int i = 0; i < args.length; i++) {
- Class type = args[i].getClass();
- argumentTypes[i] = type;
- }
-
- try {
- Constructor constructor = klass.getConstructor(argumentTypes);
- return constructor.newInstance(args);
- } catch (NoSuchMethodException e) {
- LOG.debug("Can't find such constructor.", e);
- } catch (InvocationTargetException e) {
- LOG.debug("Can't instantiate object.", e);
- } catch (InstantiationException e) {
- LOG.debug("Can't instantiate object.", e);
- } catch (IllegalAccessException e) {
- LOG.debug("Can't instantiate object.", e);
+ Constructor []constructors = klass.getConstructors();
+
+ for (Constructor constructor : constructors) {
+ try {
+ return constructor.newInstance(args);
+ } catch (InvocationTargetException e) {
+ LOG.debug("Can't instantiate object.", e);
+ } catch (InstantiationException e) {
+ LOG.trace("Can't instantiate object.", e);
+ } catch (IllegalAccessException e) {
+ LOG.trace("Can't instantiate object.", e);
+ } catch (IllegalArgumentException e) {
+ LOG.trace("Can't instantiate object.", e);
+ }
}
return null;
http://git-wip-us.apache.org/repos/asf/sqoop/blob/bbe35ea3/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java b/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
index aad5eff..16c2015 100644
--- a/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
+++ b/common/src/test/java/org/apache/sqoop/utils/TestClassUtils.java
@@ -17,83 +17,122 @@
*/
package org.apache.sqoop.utils;
-import junit.framework.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
/**
*
*/
-public class TestClassUtils extends TestCase {
-
-// public void testLoadClass() {
-// assertNull(ClassUtils.loadClass("A"));
-// assertEquals(A.class, ClassUtils.loadClass(A.class.getName()));
-// }
-//
-// public void testInstantiateNull() {
-// assertNull(ClassUtils.instantiate((Class) null));
-// }
-//
-// public void testInstantiate() {
-// A a = (A) ClassUtils.instantiate(A.class, "a");
-// assertNotNull(a);
-// assertEquals(1, a.num);
-// assertEquals("a", a.a);
-//
-// A b = (A) ClassUtils.instantiate(A.class, "b", 3, 5);
-// assertNotNull(b);
-// assertEquals(3, b.num);
-// assertEquals("b", b.a);
-// assertEquals(3, b.b);
-// assertEquals(5, b.c);
-// }
-//
-// public static class A {
-// String a;
-// int b;
-// int c;
-// int num;
-//
-// public A(String a) {
-// num = 1;
-// this.a = a;
-// }
-// public A(String a, Integer b, Integer c) {
-// this(a);
-//
-// num = 3;
-// this.b = b;
-// this.c = c;
-// }
-// }
-//
-// public void testGetEnumStrings() {
-// assertNull(ClassUtils.getEnumStrings(A.class));
-//
-// assertEquals(
-// new String[] {"A", "B", "C"},
-// ClassUtils.getEnumStrings(EnumA.class)
-// );
-// assertEquals(
-// new String[] {"X", "Y"},
-// ClassUtils.getEnumStrings(EnumX.class)
-// );
-// }
-//
-// enum EnumX {
-// X, Y
-// }
-//
-// enum EnumA {
-// A, B, C
-// }
-//
-// public void assertEquals(String[] expected, String[] actual) {
-// assertEquals("Arrays do not have same length", expected.length, actual.length);
-//
-// for(int i = 0; i < expected.length; i++) {
-// assertEquals("Items on position " + i + " differs, expected "
-// + expected[i] + ", actual " + actual[i],
-// expected[i], actual[i]);
-// }
-// }
+public class TestClassUtils {
+
+ @Test
+ public void testLoadClass() {
+ assertNull(ClassUtils.loadClass("A"));
+ assertEquals(A.class, ClassUtils.loadClass(A.class.getName()));
+ }
+
+ @Test
+ public void testInstantiateNull() {
+ assertNull(ClassUtils.instantiate((Class) null));
+ }
+
+ @Test
+ public void testInstantiate() {
+ // Just object calls
+ A a = (A) ClassUtils.instantiate(A.class, "a");
+ assertNotNull(a);
+ assertEquals(1, a.num);
+ assertEquals("a", a.a);
+
+ // Automatic wrapping primitive -> objects
+ A b = (A) ClassUtils.instantiate(A.class, "b", 3, 5);
+ assertNotNull(b);
+ assertEquals(3, b.num);
+ assertEquals("b", b.a);
+ assertEquals(3, b.b);
+ assertEquals(5, b.c);
+
+ // Primitive types in the constructor definition
+ Primitive p = (Primitive) ClassUtils.instantiate(Primitive.class, 1, 1.0f, true);
+ assertNotNull(p);
+ assertEquals(1, p.i);
+ assertEquals(1.0f, p.f, 0.0f);
+ assertEquals(true, p.b);
+
+ // Subclasses can be used in the constructor call
+ A c = (A) ClassUtils.instantiate(A.class, new Child());
+ assertNotNull(c);
+ assertNotNull(c.p);
+ assertEquals(Child.class, c.p.getClass());
+ }
+
+ public static class Parent {
+ }
+
+ public static class Child extends Parent {
+ }
+
+
+ public static class A {
+ String a;
+ int b;
+ int c;
+ int num;
+ Parent p;
+
+ public A(String a) {
+ num = 1;
+ this.a = a;
+ }
+ public A(String a, Integer b, Integer c) {
+ this(a);
+
+ num = 3;
+ this.b = b;
+ this.c = c;
+ }
+
+ public A(Parent p) {
+ this.p = p;
+ }
+ }
+
+ public static class Primitive {
+ int i;
+ float f;
+ boolean b;
+
+ public Primitive(int i, float f, boolean b) {
+ this.i = i;
+ this.f = f;
+ this.b = b;
+ }
+ }
+
+ @Test
+ public void testGetEnumStrings() {
+ assertNull(ClassUtils.getEnumStrings(A.class));
+
+ assertArrayEquals(
+ new String[]{"A", "B", "C"},
+ ClassUtils.getEnumStrings(EnumA.class)
+ );
+ assertArrayEquals(
+ new String[]{"X", "Y"},
+ ClassUtils.getEnumStrings(EnumX.class)
+ );
+ }
+
+ enum EnumX {
+ X, Y
+ }
+
+ enum EnumA {
+ A, B, C
+ }
}