Newbie on Reflection

K

Kamal Zamli

Hi all

I need help on the following code...I have a user defined class
called Kucing. I need to invoke a method called add using reflection.
Now, I can make the code works if kuci is a single object.

How can I change the code to make use an array of kuci...and be able
to invoke the method "add" through reflection
....
public String add (Kucing kuci[],String b)
......

partypes [0] = Kucing.class; // Dont know how to specify an array ????


----------------------------------------------------------
package tip2;

import java.lang.reflect.*;

public class method2
{
public String add (Kucing kuci, String b)
{
return kuci.get()+" = "+ b;
}

public static void main (String args[])
{
try
{
Class cls = Class.forName("tip.method2");
Class partypes[] = new Class [2];
partypes [0] = Kucing.class;
partypes [1] = String.class; //
Method meth = cls.getMethod ("add", partypes);
method2 methodobj = new method2 ();
Object arglist[] = new Object[2];
arglist [0] = new Kucing ();
arglist [1] = new String ("Kamal Test");
Object retobj = meth.invoke(methodobj, arglist);
System.out.println ((String) retobj);
}
catch (Throwable e)
{
System.err.println(e);
}
}
}
 
A

andrew_c_81

First, your example does not quite comply with the java code standards.
From the array definitions and others i assume you have a C background,
and are not to familiar with java(yes , i know this is probably just an
example but,...).
Now about reflexion : one should start using reflection either because
the class/method is not known at compile time and/or to prevent
ClassNotFoundException for optional libraries.
And reflexion should not be used untill you get a good grip on java.
Anyway here is how you sould do it for arrays:
partypes[ 0 ] = Kucing[].class; // this is for the method parameter
type
and when you invoke the method use ,
arglist[ 0 ] = new Kucing[]{new Kucing(),new Kucing(),new Kucing()}; //
as many times you want

hope it helps.

Regards,
Andrei
 
T

Tor Iver Wilhelmsen

partypes [0] = Kucing.class; // Dont know how to specify an array ????

If you don't already have an array available:

partypes[0] = new Kucing[0].getClass();

Otherwise, just call getClass() on the array you want to pass to
invoke() later on.
 
K

Kamal Zamli

Tor Iver Wilhelmsen said:
partypes [0] = Kucing.class; // Dont know how to specify an array ????

If you don't already have an array available:

partypes[0] = new Kucing[0].getClass();

Otherwise, just call getClass() on the array you want to pass to
invoke() later on.


Thanks all for the reply..Having digested the suggestion, am I correct
to say that if Kucing had been a basic data type, then I would do the
following to to specify partypes....


partypes [0]=Integer[].TYPE;
....

then:
arglist [0] = ... How can I initialize their values then....?

Thanks
 
A

andrew_c_81

Kamal said:
Thanks all for the reply..Having digested the suggestion, am I
correct
to say that if Kucing had been a basic data type, then I would do the
following to to specify partypes....


partypes [0]=Integer[].TYPE;
...
yes,
partypes [0]= Integer[].class;
then:
arglist [0] = ... How can I initialize their values then....?
arglist[0] = new Integer[]{ new Integer(1), new Integer(2)};

Regards,
Andrei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top