1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.lang.reflect; 19 20 import java.lang.annotation.Annotation; 21 import java.lang.annotation.ElementType; 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 import java.lang.annotation.Target; 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Method; 27 import java.lang.reflect.Modifier; 28 import java.lang.reflect.Type; 29 import java.lang.reflect.TypeVariable; 30 import java.util.HashSet; 31 import java.util.Set; 32 33 public class MethodTest extends junit.framework.TestCase { 34 35 static class TestMethod { TestMethod()36 public TestMethod() { 37 } 38 voidMethod()39 public void voidMethod() throws IllegalArgumentException { 40 } 41 parmTest(int x, short y, String s, boolean bool, Object o, long l, byte b, char c, double d, float f)42 public void parmTest(int x, short y, String s, boolean bool, Object o, 43 long l, byte b, char c, double d, float f) { 44 } 45 intMethod()46 public int intMethod() { 47 return 1; 48 } 49 printTest(int x, short y, String s, boolean bool, Object o, long l, byte b, char c, double d, float f)50 public static final void printTest(int x, short y, String s, 51 boolean bool, Object o, long l, byte b, char c, double d, 52 float f) { 53 } 54 doubleMethod()55 public double doubleMethod() { 56 return 1.0; 57 } 58 shortMethod()59 public short shortMethod() { 60 return (short) 1; 61 } 62 byteMethod()63 public byte byteMethod() { 64 return (byte) 1; 65 } 66 floatMethod()67 public float floatMethod() { 68 return 1.0f; 69 } 70 longMethod()71 public long longMethod() { 72 return 1l; 73 } 74 charMethod()75 public char charMethod() { 76 return 'T'; 77 } 78 objectMethod()79 public Object objectMethod() { 80 return new Object(); 81 } 82 prstatic()83 private static void prstatic() { 84 } 85 pustatic()86 public static void pustatic() { 87 } 88 pustatsynch()89 public static synchronized void pustatsynch() { 90 } 91 invokeStaticTest()92 public static int invokeStaticTest() { 93 return 1; 94 } 95 invokeInstanceTest()96 public int invokeInstanceTest() { 97 return 1; 98 } 99 privateInvokeTest()100 private int privateInvokeTest() { 101 return 1; 102 } 103 invokeExceptionTest()104 public int invokeExceptionTest() throws NullPointerException { 105 throw new NullPointerException(); 106 } 107 pustatsynchnat()108 public static synchronized native void pustatsynchnat(); 109 publicVoidVarargs(Object... param)110 public void publicVoidVarargs(Object... param){} publicVoidArray(Object[] param)111 public void publicVoidArray(Object[] param){} 112 annotatedParameter(@estAnno @eprecated int a, @Deprecated int b, int c)113 public void annotatedParameter(@TestAnno @Deprecated int a, 114 @Deprecated int b, int c) { 115 } 116 117 @Deprecated 118 @TestAnno annotatedMethod()119 public void annotatedMethod(){} 120 hashCodeTest(int i)121 public void hashCodeTest(int i){} hashCodeTest(String s)122 public void hashCodeTest(String s){} 123 invokeCastTest1(byte param)124 public void invokeCastTest1(byte param) { 125 } 126 invokeCastTest1(short param)127 public void invokeCastTest1(short param) { 128 } 129 invokeCastTest1(int param)130 public void invokeCastTest1(int param) { 131 } 132 invokeCastTest1(long param)133 public void invokeCastTest1(long param) { 134 } 135 invokeCastTest1(float param)136 public void invokeCastTest1(float param) { 137 } 138 invokeCastTest1(double param)139 public void invokeCastTest1(double param) { 140 } 141 invokeCastTest1(char param)142 public void invokeCastTest1(char param) { 143 } 144 invokeCastTest1(boolean param)145 public void invokeCastTest1(boolean param) { 146 } 147 } 148 149 @Retention(RetentionPolicy.RUNTIME) 150 @Target({ElementType.PARAMETER, ElementType.METHOD}) 151 public static @interface TestAnno{ 152 public static final String DEFAULT_VALUE = "DEFAULT_VALUE"; 153 value()154 String value() default DEFAULT_VALUE; 155 } 156 157 abstract class AbstractTestMethod { puabs()158 public abstract void puabs(); 159 } 160 161 class TestMethodSub extends TestMethod { invokeInstanceTest()162 public int invokeInstanceTest() { 163 return 0; 164 } 165 } 166 167 static interface IBrigeTest<T>{ m()168 T m(); 169 } 170 171 static class BrigeTest implements IBrigeTest<String> { m()172 public String m(){ return null; } 173 } 174 175 static class ExceptionTest<T extends Exception>{ 176 @SuppressWarnings("unused") exceptionTest()177 void exceptionTest() throws T{} 178 } 179 180 static class GenericReturnType<T> { returnGeneric()181 T returnGeneric(){return null;} 182 } 183 184 static class GenericString<T> { 185 public static final String GENERIC = 186 "T org.apache.harmony.tests.java.lang.reflect.MethodTest$GenericString.genericString(T)"; genericString(T t)187 T genericString(T t) { 188 return null; 189 } 190 } 191 192 /** 193 * java.lang.reflect.Method#equals(java.lang.Object) 194 */ test_equalsLjava_lang_Object()195 public void test_equalsLjava_lang_Object() { 196 // Test for method boolean 197 // java.lang.reflect.Method.equals(java.lang.Object) 198 199 Method m1 = null, m2 = null; 200 try { 201 m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]); 202 m2 = TestMethodSub.class.getMethod("invokeInstanceTest", 203 new Class[0]); 204 } catch (Exception e) { 205 fail("Exception during equals test : " + e.getMessage()); 206 } 207 assertTrue("Overriden method returned equal", !m1.equals(m2)); 208 assertTrue("Same method returned not-equal", m1.equals(m1)); 209 try { 210 m1 = TestMethod.class.getMethod("invokeStaticTest", new Class[0]); 211 m2 = TestMethodSub.class 212 .getMethod("invokeStaticTest", new Class[0]); 213 } catch (Exception e) { 214 fail("Exception during equals test : " + e.getMessage()); 215 } 216 assertTrue("Inherited method returned not-equal", m1.equals(m2)); 217 } 218 219 /** 220 * java.lang.Class#getMethod(java.lang.String, java.lang.Class[]) 221 */ test_getMethod()222 public void test_getMethod() throws NoSuchMethodException, SecurityException { 223 // Check that getMethod treats null parameterTypes the same as an empty array. 224 Method m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]); 225 Method m2 = TestMethod.class.getMethod("invokeInstanceTest", (Class[]) null); 226 assertEquals(m1, m2); 227 } 228 229 /** 230 * java.lang.Class#getDeclaredMethod(java.lang.String, java.lang.Class[]) 231 */ test_getDeclaredMethod()232 public void test_getDeclaredMethod() throws NoSuchMethodException, SecurityException { 233 // Check that getDeclaredMethod treats null parameterTypes the same as an empty array. 234 Method m1 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", new Class[0]); 235 Method m2 = TestMethod.class.getDeclaredMethod("invokeInstanceTest", (Class[]) null); 236 assertEquals(m1, m2); 237 } 238 239 /** 240 * java.lang.reflect.Method#getDeclaringClass() 241 */ test_getDeclaringClass()242 public void test_getDeclaringClass() { 243 // Test for method java.lang.Class 244 // java.lang.reflect.Method.getDeclaringClass() 245 246 Method[] mths; 247 248 try { 249 mths = TestMethod.class.getDeclaredMethods(); 250 assertTrue("Returned incorrect declaring class: " 251 + mths[0].getDeclaringClass().toString(), mths[0] 252 .getDeclaringClass().equals(TestMethod.class)); 253 } catch (Exception e) { 254 fail("Exception during getDeclaringClass test: " 255 + e.toString()); 256 } 257 } 258 259 /** 260 * java.lang.reflect.Method#getExceptionTypes() 261 */ test_getExceptionTypes()262 public void test_getExceptionTypes() { 263 // Test for method java.lang.Class [] 264 // java.lang.reflect.Method.getExceptionTypes() 265 266 try { 267 Method mth = TestMethod.class.getMethod("voidMethod", new Class[0]); 268 Class[] ex = mth.getExceptionTypes(); 269 assertEquals("Returned incorrect number of exceptions", 270 1, ex.length); 271 assertTrue("Returned incorrect exception type", ex[0] 272 .equals(IllegalArgumentException.class)); 273 mth = TestMethod.class.getMethod("intMethod", new Class[0]); 274 ex = mth.getExceptionTypes(); 275 assertEquals("Returned incorrect number of exceptions", 276 0, ex.length); 277 } catch (Exception e) { 278 fail("Exception during getExceptionTypes: " + e.toString()); 279 } 280 281 } 282 283 /** 284 * java.lang.reflect.Method#getModifiers() 285 */ test_getModifiers()286 public void test_getModifiers() { 287 // Test for method int java.lang.reflect.Method.getModifiers() 288 289 Class cl = TestMethod.class; 290 int mods = 0; 291 Method mth = null; 292 int mask = 0; 293 try { 294 mth = cl.getMethod("pustatic", new Class[0]); 295 mods = mth.getModifiers(); 296 } catch (Exception e) { 297 fail("Exception during getModfiers test: " + e.toString()); 298 } 299 mask = Modifier.PUBLIC | Modifier.STATIC; 300 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 301 try { 302 mth = cl.getDeclaredMethod("prstatic", new Class[0]); 303 mods = mth.getModifiers(); 304 } catch (Exception e) { 305 fail("Exception during getModfiers test: " + e.toString()); 306 } 307 mask = Modifier.PRIVATE | Modifier.STATIC; 308 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 309 try { 310 mth = cl.getDeclaredMethod("pustatsynch", new Class[0]); 311 mods = mth.getModifiers(); 312 } catch (Exception e) { 313 fail("Exception during getModfiers test: " + e.toString()); 314 } 315 mask = (Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED; 316 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 317 try { 318 mth = cl.getDeclaredMethod("pustatsynchnat", new Class[0]); 319 mods = mth.getModifiers(); 320 } catch (Exception e) { 321 fail("Exception during getModfiers test: " + e.toString()); 322 } 323 mask = ((Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED) 324 | Modifier.NATIVE; 325 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 326 cl = AbstractTestMethod.class; 327 try { 328 mth = cl.getDeclaredMethod("puabs", new Class[0]); 329 mods = mth.getModifiers(); 330 } catch (Exception e) { 331 fail("Exception during getModfiers test: " + e.toString()); 332 } 333 mask = Modifier.PUBLIC | Modifier.ABSTRACT; 334 assertTrue("Incorrect modifiers returned", (mods | mask) == mask); 335 } 336 337 /** 338 * java.lang.reflect.Method#getName() 339 */ test_getName()340 public void test_getName() { 341 // Test for method java.lang.String java.lang.reflect.Method.getName() 342 Method mth = null; 343 try { 344 mth = TestMethod.class.getMethod("voidMethod", new Class[0]); 345 } catch (Exception e) { 346 fail("Exception during getMethodName(): " + e.toString()); 347 } 348 assertEquals("Returned incorrect method name", 349 "voidMethod", mth.getName()); 350 } 351 352 /** 353 * java.lang.reflect.Method#isVarArgs() 354 */ test_isVarArgs()355 public void test_isVarArgs() throws Exception { 356 Method mth = TestMethod.class.getMethod("publicVoidVarargs", 357 Object[].class); 358 assertTrue("Varargs method stated as non vararg.", mth.isVarArgs()); 359 360 mth = TestMethod.class.getDeclaredMethod("publicVoidArray", 361 Object[].class); 362 assertFalse("Non varargs method stated as vararg.", mth.isVarArgs()); 363 } 364 365 /** 366 * java.lang.reflect.Method#isBridge() 367 */ test_isBridge()368 public void test_isBridge() throws Exception { 369 Method[] declaredMethods = BrigeTest.class.getDeclaredMethods(); 370 assertEquals("Bridge method not generated.", 2, declaredMethods.length); 371 boolean foundBridgeMethod = false; 372 for (Method method : declaredMethods) { 373 if (method.getReturnType().equals(Object.class)) { 374 assertTrue("Bridge method not stated as bridge.", method 375 .isBridge()); 376 foundBridgeMethod = true; 377 } 378 } 379 assertTrue("Bridge method not found.", foundBridgeMethod); 380 } 381 382 /** 383 * java.lang.reflect.Method#isSynthetic() 384 */ test_isSynthetic()385 public void test_isSynthetic() throws Exception { 386 Method[] declaredMethods = BrigeTest.class.getDeclaredMethods(); 387 assertEquals("Synthetic method not generated.", 2, 388 declaredMethods.length); 389 boolean foundSyntheticMethod = false; 390 for (Method method : declaredMethods) { 391 if (method.getReturnType().equals(Object.class)) { 392 assertTrue("Synthetic method not stated as synthetic.", method 393 .isSynthetic()); 394 foundSyntheticMethod = true; 395 } 396 } 397 assertTrue("Synthetic method not found.", foundSyntheticMethod); 398 } 399 /** 400 * java.lang.reflect.Method#getParameterAnnotations() 401 */ test_getParameterAnnotations()402 public void test_getParameterAnnotations() throws Exception { 403 Method method = TestMethod.class.getDeclaredMethod( 404 "annotatedParameter", new Class[] { 405 int.class, int.class, int.class}); 406 Annotation[][] annotations = method.getParameterAnnotations(); 407 assertEquals(3, annotations.length); 408 assertEquals( 409 "Wrong number of annotations returned for first parameter", 2, 410 annotations[0].length); 411 Set<Class<?>> annotationSet = new HashSet<Class<?>>(); 412 annotationSet.add(annotations[0][0].annotationType()); 413 annotationSet.add(annotations[0][1].annotationType()); 414 assertTrue("Missing TestAnno annotation", annotationSet 415 .contains(TestAnno.class)); 416 assertTrue("Missing Deprecated annotation", annotationSet 417 .contains(Deprecated.class)); 418 419 assertEquals( 420 "Wrong number of annotations returned for second parameter", 421 1, annotations[1].length); 422 annotationSet = new HashSet<Class<?>>(); 423 annotationSet.add(annotations[1][0].annotationType()); 424 assertTrue("Missing Deprecated annotation", annotationSet 425 .contains(Deprecated.class)); 426 assertEquals( 427 "Wrong number of annotations returned for third parameter", 0, 428 annotations[2].length); 429 } 430 431 /** 432 * java.lang.reflect.Method#getDeclaredAnnotations() 433 */ test_getDeclaredAnnotations()434 public void test_getDeclaredAnnotations() throws Exception { 435 Method method = TestMethod.class.getDeclaredMethod("annotatedMethod"); 436 Annotation[] declaredAnnotations = method.getDeclaredAnnotations(); 437 assertEquals(2, declaredAnnotations.length); 438 439 Set<Class<?>> annotationSet = new HashSet<Class<?>>(); 440 annotationSet.add(declaredAnnotations[0].annotationType()); 441 annotationSet.add(declaredAnnotations[1].annotationType()); 442 assertTrue("Missing TestAnno annotation", annotationSet 443 .contains(TestAnno.class)); 444 assertTrue("Missing Deprecated annotation", annotationSet 445 .contains(Deprecated.class)); 446 } 447 448 /** 449 * java.lang.reflect.Method#getDefaultValue() 450 */ test_getDefaultValue()451 public void test_getDefaultValue() throws Exception { 452 Method method = TestAnno.class.getDeclaredMethod("value"); 453 assertEquals("Wrong default value returned", TestAnno.DEFAULT_VALUE, 454 method.getDefaultValue()); 455 } 456 457 /** 458 * java.lang.reflect.Method#getDefaultValue() 459 */ test_getGenericExceptionTypes()460 public void test_getGenericExceptionTypes() throws Exception { 461 Method method = ExceptionTest.class.getDeclaredMethod("exceptionTest"); 462 Type[] genericExceptionTypes = method.getGenericExceptionTypes(); 463 assertEquals(1, genericExceptionTypes.length); 464 assertTrue(genericExceptionTypes[0] instanceof TypeVariable<?>); 465 @SuppressWarnings("unchecked") 466 TypeVariable<Class<ExceptionTest<?>>> tv = 467 (TypeVariable<Class<ExceptionTest<?>>>) genericExceptionTypes[0]; 468 assertEquals("T", tv.getName()); 469 } 470 471 /** 472 * java.lang.reflect.Method#getGenericReturnType() 473 */ test_getGenericReturnType()474 public void test_getGenericReturnType() throws Exception { 475 Method method = GenericReturnType.class 476 .getDeclaredMethod("returnGeneric"); 477 Type returnType = method.getGenericReturnType(); 478 assertNotNull("getGenericReturnType returned null", returnType); 479 assertTrue(returnType instanceof TypeVariable<?>); 480 @SuppressWarnings("unchecked") 481 TypeVariable<Class<ExceptionTest<?>>> tv = 482 (TypeVariable<Class<ExceptionTest<?>>>) returnType; 483 assertEquals("T", tv.getName()); 484 } 485 486 487 /** 488 * java.lang.reflect.Method#toGenericString() 489 */ test_toGenericString()490 public void test_toGenericString() throws Exception { 491 Method method = GenericString.class.getDeclaredMethod("genericString", 492 Object.class); 493 assertEquals("Wrong generic String returned", GenericString.GENERIC, 494 method.toGenericString()); 495 } 496 497 498 499 500 501 502 /** 503 * java.lang.reflect.Method#hashCode() 504 */ test_hashCode()505 public void test_hashCode() throws Exception { 506 Method mth0 = TestMethod.class.getMethod("hashCodeTest", String.class); 507 Method mth1 = TestMethod.class.getDeclaredMethod("hashCodeTest", 508 int.class); 509 assertEquals("Methods with same name did not return same hashCode.", 510 mth0.hashCode(), mth1.hashCode()); 511 } 512 513 /** 514 * java.lang.reflect.Method#getParameterTypes() 515 */ test_getParameterTypes()516 public void test_getParameterTypes() { 517 // Test for method java.lang.Class [] 518 // java.lang.reflect.Method.getParameterTypes() 519 Class cl = TestMethod.class; 520 Method mth = null; 521 Class[] parms = null; 522 Method[] methods = null; 523 Class[] plist = { int.class, short.class, String.class, boolean.class, 524 Object.class, long.class, byte.class, char.class, double.class, 525 float.class }; 526 try { 527 mth = cl.getMethod("voidMethod", new Class[0]); 528 parms = mth.getParameterTypes(); 529 } catch (Exception e) { 530 fail("Exception during getParameterTypes test: " 531 + e.toString()); 532 } 533 assertEquals("Returned incorrect parameterTypes", 0, parms.length); 534 try { 535 mth = cl.getMethod("parmTest", plist); 536 parms = mth.getParameterTypes(); 537 } catch (Exception e) { 538 fail("Exception during getParameterTypes test: " 539 + e.toString()); 540 } 541 assertTrue("Invalid number of parameters returned", 542 plist.length == parms.length); 543 for (int i = 0; i < plist.length; i++) 544 assertTrue("Incorrect parameter returned", plist[i] 545 .equals(parms[i])); 546 547 // Test same method. but this time pull it from the list of methods 548 // rather than asking for it explicitly 549 methods = cl.getDeclaredMethods(); 550 551 int i; 552 for (i = 0; i < methods.length; i++) 553 if (methods[i].getName().equals("parmTest")) { 554 mth = methods[i]; 555 i = methods.length + 1; 556 } 557 if (i < methods.length) { 558 parms = mth.getParameterTypes(); 559 assertTrue("Incorrect number of parameters returned", 560 parms.length == plist.length); 561 for (i = 0; i < plist.length; i++) 562 assertTrue("Incorrect parameter returned", plist[i] 563 .equals(parms[i])); 564 } 565 } 566 567 /** 568 * java.lang.reflect.Method#getReturnType() 569 */ test_getReturnType()570 public void test_getReturnType() { 571 // Test for method java.lang.Class 572 // java.lang.reflect.Method.getReturnType() 573 Class cl = TestMethod.class; 574 Method mth = null; 575 try { 576 mth = cl.getMethod("charMethod", new Class[0]); 577 } catch (Exception e) { 578 fail("Exception during getReturnType test : " + e.getMessage()); 579 } 580 assertTrue("Gave incorrect returne type, wanted char", mth 581 .getReturnType().equals(char.class)); 582 try { 583 mth = cl.getMethod("longMethod", new Class[0]); 584 } catch (Exception e) { 585 fail("Exception during getReturnType test : " + e.getMessage()); 586 } 587 assertTrue("Gave incorrect returne type, wanted long", mth 588 .getReturnType().equals(long.class)); 589 try { 590 mth = cl.getMethod("shortMethod", new Class[0]); 591 } catch (Exception e) { 592 fail("Exception during getReturnType test : " + e.getMessage()); 593 } 594 assertTrue("Gave incorrect returne type, wanted short", mth 595 .getReturnType().equals(short.class)); 596 try { 597 mth = cl.getMethod("intMethod", new Class[0]); 598 } catch (Exception e) { 599 fail("Exception during getReturnType test : " + e.getMessage()); 600 } 601 assertTrue("Gave incorrect returne type, wanted int: " 602 + mth.getReturnType(), mth.getReturnType().equals(int.class)); 603 try { 604 mth = cl.getMethod("doubleMethod", new Class[0]); 605 } catch (Exception e) { 606 fail("Exception during getReturnType test : " + e.getMessage()); 607 } 608 assertTrue("Gave incorrect returne type, wanted double", mth 609 .getReturnType().equals(double.class)); 610 try { 611 mth = cl.getMethod("byteMethod", new Class[0]); 612 } catch (Exception e) { 613 fail("Exception during getReturnType test : " + e.getMessage()); 614 } 615 assertTrue("Gave incorrect returne type, wanted byte", mth 616 .getReturnType().equals(byte.class)); 617 try { 618 mth = cl.getMethod("byteMethod", new Class[0]); 619 } catch (Exception e) { 620 fail("Exception during getReturnType test:" + e.toString()); 621 } 622 assertTrue("Gave incorrect returne type, wanted byte", mth 623 .getReturnType().equals(byte.class)); 624 try { 625 mth = cl.getMethod("objectMethod", new Class[0]); 626 } catch (Exception e) { 627 fail("Exception during getReturnType test : " + e.getMessage()); 628 } 629 assertTrue("Gave incorrect returne type, wanted Object", mth 630 .getReturnType().equals(Object.class)); 631 632 try { 633 mth = cl.getMethod("voidMethod", new Class[0]); 634 } catch (Exception e) { 635 fail("Exception during getReturnType test : " + e.getMessage()); 636 } 637 assertTrue("Gave incorrect returne type, wanted void", mth 638 .getReturnType().equals(void.class)); 639 } 640 641 /** 642 * java.lang.reflect.Method#invoke(java.lang.Object, 643 * java.lang.Object[]) 644 */ test_invokeLjava_lang_Object$Ljava_lang_Object()645 public void test_invokeLjava_lang_Object$Ljava_lang_Object() throws Exception{ 646 // Test for method java.lang.Object 647 // java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object 648 // []) 649 Class cl = TestMethod.class; 650 Class[] dcl = new Class[0]; 651 652 // Get and invoke a static method 653 Method mth = cl.getDeclaredMethod("invokeStaticTest", dcl); 654 Object ret = mth.invoke(null, new Object[0]); 655 assertEquals("Invoke returned incorrect value", 1, ((Integer) ret) 656 .intValue()); 657 658 // Get and invoke an instance method 659 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 660 ret = mth.invoke(new TestMethod(), new Object[0]); 661 assertEquals("Invoke returned incorrect value", 1, ((Integer) ret) 662 .intValue()); 663 664 // Get and attempt to invoke a private method 665 mth = cl.getDeclaredMethod("privateInvokeTest", dcl); 666 try { 667 ret = mth.invoke(new TestMethod(), new Object[0]); 668 } catch (IllegalAccessException e) { 669 // Correct behaviour 670 } catch (Exception e) { 671 fail("Exception during invoke test : " + e.getMessage()); 672 } 673 // Generate an IllegalArgumentException 674 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 675 676 try { 677 Object[] args = { Object.class }; 678 ret = mth.invoke(new TestMethod(), args); 679 } catch (IllegalArgumentException e) { 680 // Correct behaviour 681 } catch (Exception e) { 682 fail("Exception during invoke test : " + e.getMessage()); 683 } 684 685 // Generate a NullPointerException 686 mth = cl.getDeclaredMethod("invokeInstanceTest", dcl); 687 688 try { 689 ret = mth.invoke(null, new Object[0]); 690 } catch (NullPointerException e) { 691 // Correct behaviour 692 } catch (Exception e) { 693 fail("Exception during invoke test : " + e.getMessage()); 694 } 695 696 // Generate an InvocationTargetException 697 mth = cl.getDeclaredMethod("invokeExceptionTest", dcl); 698 try { 699 ret = mth.invoke(new TestMethod(), new Object[0]); 700 } catch (InvocationTargetException e) { 701 // Correct behaviour 702 } catch (Exception e) { 703 fail("Exception during invoke test : " + e.getMessage()); 704 } 705 706 TestMethod testMethod = new TestMethod(); 707 Method methods[] = cl.getMethods(); 708 for (int i = 0; i < methods.length; i++) { 709 if (methods[i].getName().startsWith("invokeCastTest1")) { 710 Class param = methods[i].getParameterTypes()[0]; 711 712 try { 713 methods[i].invoke(testMethod, new Object[] { new Byte( 714 (byte) 1) }); 715 assertTrue("invalid invoke with Byte: " + methods[i], 716 param == Byte.TYPE || param == Short.TYPE 717 || param == Integer.TYPE 718 || param == Long.TYPE 719 || param == Float.TYPE 720 || param == Double.TYPE); 721 } catch (Exception e) { 722 assertTrue("Byte invalid exception: " + e, 723 e instanceof IllegalArgumentException); 724 assertTrue("Byte invalid failure: " + methods[i], 725 param == Boolean.TYPE || param == Character.TYPE); 726 } 727 728 try { 729 methods[i].invoke(testMethod, new Object[] { new Short( 730 (short) 1) }); 731 assertTrue("invalid invoke with Short: " + methods[i], 732 param == Short.TYPE || param == Integer.TYPE 733 || param == Long.TYPE 734 || param == Float.TYPE 735 || param == Double.TYPE); 736 } catch (Exception e) { 737 assertTrue("Short invalid exception: " + e, 738 e instanceof IllegalArgumentException); 739 assertTrue("Short invalid failure: " + methods[i], 740 param == Byte.TYPE || param == Boolean.TYPE 741 || param == Character.TYPE); 742 } 743 744 try { 745 methods[i].invoke(testMethod, 746 new Object[] { new Integer(1) }); 747 assertTrue("invalid invoke with Integer: " + methods[i], 748 param == Integer.TYPE || param == Long.TYPE 749 || param == Float.TYPE 750 || param == Double.TYPE); 751 } catch (Exception e) { 752 assertTrue("Integer invalid exception: " + e, 753 e instanceof IllegalArgumentException); 754 assertTrue("Integer invalid failure: " + methods[i], 755 param == Byte.TYPE || param == Short.TYPE 756 || param == Boolean.TYPE 757 || param == Character.TYPE); 758 } 759 760 try { 761 methods[i].invoke(testMethod, new Object[] { new Long(1) }); 762 assertTrue("invalid invoke with Long: " + methods[i], 763 param == Long.TYPE || param == Float.TYPE 764 || param == Double.TYPE); 765 } catch (Exception e) { 766 assertTrue("Long invalid exception: " + e, 767 e instanceof IllegalArgumentException); 768 assertTrue("Long invalid failure: " + methods[i], 769 param == Byte.TYPE || param == Short.TYPE 770 || param == Integer.TYPE 771 || param == Boolean.TYPE 772 || param == Character.TYPE); 773 } 774 775 try { 776 methods[i].invoke(testMethod, new Object[] { new Character( 777 'a') }); 778 assertTrue("invalid invoke with Character: " + methods[i], 779 param == Character.TYPE || param == Integer.TYPE 780 || param == Long.TYPE 781 || param == Float.TYPE 782 || param == Double.TYPE); 783 } catch (Exception e) { 784 assertTrue("Character invalid exception: " + e, 785 e instanceof IllegalArgumentException); 786 assertTrue("Character invalid failure: " + methods[i], 787 param == Byte.TYPE || param == Short.TYPE 788 || param == Boolean.TYPE); 789 } 790 791 try { 792 methods[i] 793 .invoke(testMethod, new Object[] { new Float(1) }); 794 assertTrue("invalid invoke with Float: " + methods[i], 795 param == Float.TYPE || param == Double.TYPE); 796 } catch (Exception e) { 797 assertTrue("Float invalid exception: " + e, 798 e instanceof IllegalArgumentException); 799 assertTrue("Float invalid failure: " + methods[i], 800 param == Byte.TYPE || param == Short.TYPE 801 || param == Integer.TYPE 802 || param == Long.TYPE 803 || param == Boolean.TYPE 804 || param == Character.TYPE); 805 } 806 807 try { 808 methods[i].invoke(testMethod, 809 new Object[] { new Double(1) }); 810 assertTrue("invalid invoke with Double: " + methods[i], 811 param == Double.TYPE); 812 } catch (Exception e) { 813 assertTrue("Double invalid exception: " + e, 814 e instanceof IllegalArgumentException); 815 assertTrue("Double invalid failure: " + methods[i], 816 param == Byte.TYPE || param == Short.TYPE 817 || param == Integer.TYPE 818 || param == Long.TYPE 819 || param == Boolean.TYPE 820 || param == Character.TYPE 821 || param == Float.TYPE); 822 } 823 824 try { 825 methods[i].invoke(testMethod, new Object[] { new Boolean( 826 true) }); 827 assertTrue("invalid invoke with Boolean: " + methods[i], 828 param == Boolean.TYPE); 829 } catch (Exception e) { 830 assertTrue("Boolean invalid exception: " + e, 831 e instanceof IllegalArgumentException); 832 assertTrue("Boolean invalid failure: " + methods[i], 833 param == Byte.TYPE || param == Short.TYPE 834 || param == Integer.TYPE 835 || param == Long.TYPE 836 || param == Character.TYPE 837 || param == Float.TYPE 838 || param == Double.TYPE); 839 } 840 } 841 } 842 } 843 844 /** 845 * java.lang.reflect.Method#toString() 846 */ test_toString()847 public void test_toString() { 848 // Test for method java.lang.String java.lang.reflect.Method.toString() 849 Method mth = null; 850 Class[] parms = { int.class, short.class, String.class, boolean.class, 851 Object.class, long.class, byte.class, char.class, double.class, 852 float.class }; 853 try { 854 855 mth = TestMethod.class.getDeclaredMethod("printTest", parms); 856 } catch (Exception e) { 857 fail("Exception during toString test : " + e.getMessage()); 858 } 859 860 assertTrue( 861 "Returned incorrect string for method: " + mth.toString(), 862 mth 863 .toString() 864 .equals( 865 "public static final void org.apache.harmony.tests.java.lang.reflect.MethodTest$TestMethod.printTest(int,short,java.lang.String,boolean,java.lang.Object,long,byte,char,double,float)")); 866 } 867 868 /** 869 * Sets up the fixture, for example, open a network connection. This method 870 * is called before a test is executed. 871 */ setUp()872 protected void setUp() { 873 } 874 875 /** 876 * Tears down the fixture, for example, close a network connection. This 877 * method is called after a test is executed. 878 */ tearDown()879 protected void tearDown() { 880 } 881 } 882