mirror of
https://github.com/apache/sqoop.git
synced 2025-05-07 00:20:58 +08:00
SQOOP-1425: Sqoop2: Improve ClassUtils to enable use of primitive types and subclasses
This commit is contained in:
parent
b90c6a2b80
commit
68becd49d4
@ -95,23 +95,20 @@ public static Object instantiate(Class klass, Object ... args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Class []argumentTypes = new Class[args.length];
|
||||
for(int i = 0; i < args.length; i++) {
|
||||
Class type = args[i].getClass();
|
||||
argumentTypes[i] = type;
|
||||
}
|
||||
Constructor []constructors = klass.getConstructors();
|
||||
|
||||
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);
|
||||
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;
|
||||
|
@ -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 class TestClassUtils {
|
||||
|
||||
// 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]);
|
||||
// }
|
||||
// }
|
||||
@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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user