public class Test1<Type>
{
public Type getCompositeMessage(Type... strings)
{
Type val = (Type) "";
for (Type str : strings) {
val = (Type) ((String)val + (String)str);
}
return val;
}
}
Retrieving method:
try
{
Class<?> c = Class.forName("test1.Test1");
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
String mname = m.getName();
System.out.println(mname);
}
Method m = c.getMethod("getCompositeMessage");
m.setAccessible(true);
Object o = m.invoke(c, "777777777777777777777777");
System.out.println(m);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
getCompositeMessage
java.lang.NoSuchMethodException: test1.Test1.getCompositeMessage()
at java.lang.Class.getMethod(Unknown Source)
at test1.Main.main(Main.java:25)
But name of method is exactly the same! Why I receive NoSuchMethodException ?
thanks.
After you fixed the misspelling, you are still looking for the wrong method:
The method is defined as:
but you are looking for
without parameters.
You need to use:
The next problem will be the call to invoke(), you are passing the class references in stead of the object on which the method should be called.
The next bug is that you are not passing the correct arguments to the function:
And the next problem is that you want output the result of the method instead of the method-object in the following line: