Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Java reflection with primitive types

Thread view: 
Zach - 08 Jun 2005 01:25 GMT
I want to call a class function through invoke(...) in class Method. If
this function's arguments are different primitive types, can I call it
with invoke(...)? For example, how can I call foo() in class A in
following example?

public class A
{
   public A() {}

   public int foo(int a, boolean b)
   {
       ...
   }
}

Is it doable?

Thanks a lot!
Zach
Bjorn Abelli - 08 Jun 2005 02:45 GMT
"Zach" wrote...

> I want to call a class function through invoke(...) in class
> Method. If this function's arguments are different primitive
[quoted text clipped - 12 lines]
>
> Is it doable?

Yes, it is.

Just use the corresponding wrapper for primitive types.

int --> Integer
boolean --> Boolean
etc..

// Bjorn A
John Currier - 08 Jun 2005 03:21 GMT
 Class[] paramTypes = new Class[] {int.class, boolean.class};
 Method foo = A.class.getMethod("foo", paramTypes);
 Object[] params = new Object[] {new Integer(55), Boolean.TRUE};
 int rc = ((Integer)foo.invoke(new A(), params)).intValue();
 System.out.println("foo returned " + rc);

John


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.