diff options
3 files changed, 80 insertions, 10 deletions
diff --git a/luni-kernel/src/main/java/java/lang/Class.java b/luni-kernel/src/main/java/java/lang/Class.java index 6adf4db..b8e3903 100644 --- a/luni-kernel/src/main/java/java/lang/Class.java +++ b/luni-kernel/src/main/java/java/lang/Class.java @@ -469,6 +469,7 @@ public final class Class<T> implements Serializable, AnnotatedElement, GenericDe * * @param parameterTypes * the parameter types of the requested constructor. + * {@code (Class[]) null} is equivalent to the empty array. * @return the constructor described by {@code parameterTypes}. * @throws NoSuchMethodException * if the constructor can not be found. @@ -587,6 +588,7 @@ public final class Class<T> implements Serializable, AnnotatedElement, GenericDe * * @param parameterTypes * the parameter types of the requested constructor. + * {@code (Class[]) null} is equivalent to the empty array. * @return the constructor described by {@code parameterTypes}. * @throws NoSuchMethodException * if the requested constructor can not be found. @@ -659,12 +661,14 @@ public final class Class<T> implements Serializable, AnnotatedElement, GenericDe sb.append(getSimpleName()); sb.append('('); boolean first = true; - for (Class<?> p : parameterTypes) { - if (!first) { - sb.append(','); + if (parameterTypes != null) { + for (Class<?> p : parameterTypes) { + if (!first) { + sb.append(','); + } + first = false; + sb.append(p.getSimpleName()); } - first = false; - sb.append(p.getSimpleName()); } sb.append(')'); throw new NoSuchMethodException(sb.toString()); @@ -741,6 +745,7 @@ public final class Class<T> implements Serializable, AnnotatedElement, GenericDe * the requested method's name. * @param parameterTypes * the parameter types of the requested method. + * {@code (Class[]) null} is equivalent to the empty array. * @return the method described by {@code name} and {@code parameterTypes}. * @throws NoSuchMethodException * if the requested constructor can not be found. @@ -991,6 +996,7 @@ public final class Class<T> implements Serializable, AnnotatedElement, GenericDe * the requested method's name. * @param parameterTypes * the parameter types of the requested method. + * {@code (Class[]) null} is equivalent to the empty array. * @return the public field specified by {@code name}. * @throws NoSuchMethodException * if the method can not be found. diff --git a/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java b/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java index 6bdb55a..e9554dd 100644 --- a/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java +++ b/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java @@ -104,7 +104,11 @@ public class ConstructorTest extends junit.framework.TestCase { public GenericConstructorTestHelper(T t, S s) {} public GenericConstructorTestHelper() throws E{} } - + + static class NoPublicConstructorTestHelper { + // This class has no public constructor. + } + // Used to test synthetic constructor. // // static class Outer { @@ -479,7 +483,6 @@ public class ConstructorTest extends junit.framework.TestCase { } catch (Exception e) { fail("Exception during getGenericExceptionTypes test:" + e.toString()); } - System.out.println(Arrays.toString(types)); assertEquals("Wrong number of exception types returned", 1, types.length); @@ -555,7 +558,35 @@ public class ConstructorTest extends junit.framework.TestCase { .equals( "public tests.api.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)")); } - + + /** + * @tests java.lang.reflect.Constructor#getConstructor((Class[]) null) + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getConstructor", + args = {} + ) + public void test_getConstructor() throws Exception { + // Passing new Class[0] should be equivalent to (Class[]) null. + Class<ConstructorTestHelper> c2 = ConstructorTestHelper.class; + assertEquals(c2.getConstructor(new Class[0]), c2.getConstructor((Class[]) null)); + assertEquals(c2.getDeclaredConstructor(new Class[0]), + c2.getDeclaredConstructor((Class[]) null)); + + // We can get a non-public constructor via getDeclaredConstructor... + Class<NoPublicConstructorTestHelper> c1 = NoPublicConstructorTestHelper.class; + c1.getDeclaredConstructor((Class[]) null); + // ...but not with getConstructor (which only returns public constructors). + try { + c1.getConstructor((Class[]) null); + fail("Should throw NoSuchMethodException"); + } catch (NoSuchMethodException ex) { + // Expected. + } + } + /** * Sets up the fixture, for example, open a network connection. This method * is called before a test is executed. @@ -570,4 +601,3 @@ public class ConstructorTest extends junit.framework.TestCase { protected void tearDown() { } } - diff --git a/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java b/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java index 884bc8c..506d173 100644 --- a/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java +++ b/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java @@ -226,7 +226,41 @@ public class MethodTest extends junit.framework.TestCase { } assertTrue("Inherited method returned not-equal", m1.equals(m2)); } - + + /** + * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getMethod", + args = {java.lang.String.class, java.lang.Class[].class}, + clazz = java.lang.Class.class + ) + public void test_getMethod() throws NoSuchMethodException, SecurityException { + // Check that getMethod treats null parameterTypes the same as an empty array. + Method m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]); + Method m2 = TestMethod.class.getMethod("invokeInstanceTest", (Class[]) null); + assertEquals(m1, m2); + } + + /** + * @tests java.lang.Class#getDeclaredMethod(java.lang.String, java.lang.Class[]) + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getDeclaredMethod", + args = {java.lang.String.class, java.lang.Class[].class}, + clazz = java.lang.Class.class + ) + public void test_getDeclaredMethod() throws NoSuchMethodException, SecurityException { + // Check that getDeclaredMethod treats null parameterTypes the same as an empty array. + Method m1 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", new Class[0]); + Method m2 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", (Class[]) null); + assertEquals(m1, m2); + } + /** * @tests java.lang.reflect.Method#getDeclaringClass() */ |