fork download
  1. import java.lang.reflect.*;
  2.  
  3. class Example {
  4.  
  5. public void testArrayParams(String first, String second, String... strings) {
  6.  
  7. System.out.println("First: " + first);
  8. System.out.println("Second: " + second);
  9.  
  10. System.out.println("Size of String... strings: " + strings.length);
  11. for(int i = 0; i < strings.length; i ++) {
  12. System.out.println(i + ": " + strings[i]);
  13. }
  14.  
  15. }
  16.  
  17. public static void main(String[] args) {
  18. //Dummy dummy = new Dummy();
  19. //dummy.testArrayParams("Hello", "World", "This is ", "passed in ", "format String... strings.");
  20.  
  21. Object[] params = {
  22. "Hello",
  23. "World",
  24. new String[] {
  25. "This is ",
  26. "passed in ",
  27. "format String... strings."
  28. }
  29. };
  30. String method = "testArrayParams";
  31.  
  32. try {
  33. Class<?> clazz = Example.class;//Class.forName("com.bt.testafix.utils.Dummy");
  34. Method[] methods = clazz.getMethods();
  35.  
  36. for (Method m : methods) {
  37. if (m.getName().equalsIgnoreCase(method)) {
  38. //m.invoke(clazz, (Object[]) params);
  39. m.invoke(clazz.newInstance(), params);
  40. break;
  41. }
  42. }
  43.  
  44. } catch (Exception e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
First: Hello
Second: World
Size of String... strings: 3
0: This is 
1: passed in 
2: format String... strings.