Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit:</strong> This answer is incorrect. See below for a counterexample.</p> <p>You've formatted your argument to <code>Method.invoke()</code> wrong. See the documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...%29" rel="nofollow">here</a>. Instead of passing a single Object[] array of all arguments, you just pass multiple arguments to invoke. That's what the <code>Object... args</code> notation means: the method accepts any number of Objects.</p> <p>For your example, changing</p> <pre><code>m.invoke(c.newInstance(), new Object[] { new com.syncoorp.ftpsyncx.commons.SyncFile("bla") }); </code></pre> <p>to</p> <pre><code>m.invoke(c.newInstance(), new com.syncoorp.ftpsyncx.commons.SyncFile("bla")); </code></pre> <p>should fix the problem.</p> <hr> <p>Counterexample:</p> <p>Refl.java:</p> <pre><code>package com.drfloob.so; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; public class Refl { public static void main(String[] args) { try { Class c = Class.forName("com.drfloob.so.Refl2", true, ClassLoader.getSystemClassLoader()); for (Method m : c.getDeclaredMethods()) { System.out.println("Method: " + m.getName()); for (Type t : m.getGenericParameterTypes()) { System.out.println(" - type: " + t.toString()); } m.invoke(c.newInstance(), "test 1"); m.invoke(c.newInstance(), new Object[] {"test 2"}); break; } } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>Refl2.java:</p> <pre><code>package com.drfloob.so; public class Refl2 { public Refl2() { System.out.println(Refl2.class); } public void doStuff(String str) { System.out.println(str); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload