Results 1 to 9 of 9
- 08-23-2010, 05:14 PM #1
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
how to use java.reflection with parameter(s)
how to use java.reflection with parameter(s)
Java Code:public class MyClass1{ Class1 cls1 = new Class1(); public void myMethod(String s) { cls1.msgBox(null, s, Class1.MESSAGE_INFO); } } //============= String fcn = "learn38.MyClass1"; Class c1 = Class.forName(fcn); Method meth1 = c1.getMethod("myMethod", "hello world"); Object theObject1 = c1.newInstance(); meth1.invoke(theObject1);
- 08-24-2010, 09:03 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Maybe by, I don't know, reading the API for Method.invoke()?
- 08-24-2010, 05:04 PM #3
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
Just want to share..
String fcn = "learn38.MyClass1";
Class c1 = Class.forName(fcn);
Object theObject1 = c1.newInstance();
Method meth1 = c1.getDeclaredMethod("myMethod", new Class[]{String.class});
String argu = "This is String to pass";
meth1.invoke(theObject1, new Object[]{argu});
- 08-24-2010, 05:41 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Don't actually need the array (unless you are using an earlier version of Java of course).
Just list the parameters.
- 08-24-2010, 06:00 PM #5
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
hi.. thanks .. but could you please more detail?
- 08-25-2010, 08:41 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
This is the method:
invoke(Object obj, Object... args)
The "..." means you can pass in a list of arguments, eg:
or, in your case:Java Code:method.invoke(object, param1, param2, param3);
Java Code:meth1.invoke(theObject1, argu);
- 08-25-2010, 08:46 AM #7
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
so you meant:
Default
Just want to share..
String fcn = "learn38.MyClass1";
Class c1 = Class.forName(fcn);
Object theObject1 = c1.newInstance();
Method meth1 = c1.getDeclaredMethod("myMethod", String.class, String.class);
String argu1 = "This is String 1 to pass";
String argu2 = "This is String 2 to pass";
meth1.invoke(theObject1, argu1, argu2);Last edited by newbiejava; 08-25-2010 at 09:21 AM.
- 08-25-2010, 09:01 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Yes.
And that getDeclaredMethod Strings should be String.class (as you had in the original one), but the compiler would catch that for you.
I suspect (though I haven't looked) that the compiler wraps those up in an array in any case, but it means you don't have to clutter up your code with extra "new Object[]{}" fluff.
- 08-25-2010, 09:21 AM #9
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
Similar Threads
-
Reflection - How to determine the method parameter class
By bazong in forum Advanced JavaReplies: 4Last Post: 06-18-2010, 10:08 AM -
java.sql.SQLException: Missing IN or OUT parameter at index:: 1
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-08-2010, 08:45 PM -
Java reflection: find serializable classes in package
By andrew222 in forum Advanced JavaReplies: 2Last Post: 02-27-2010, 07:36 AM -
java INVALID COPYRIGHT PARAMETER daily fx free charts
By pete in forum New To JavaReplies: 1Last Post: 11-12-2009, 08:53 PM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks