Help on Java Reflection

K

Kamal Zamli

Dear all

I have the following question on Java reflection. Consider the
following code:

-----------------------------------------------------------
import java.lang.reflect.*;

public class method2
{
public String add (int a[])
{
return "done";
}

public static void main (String args[])
{
try
{
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

partypes [0] = Integer[].class;

Method meth = cls.getMethod ("add", partypes);
method2 methobj = new method2 ();
Object arglist[] = new Object[1];

arglist [0] = new Integer[]{ new Integer(25),new Integer(24)};
Object retobj = meth.invoke(methobj, arglist);
System.out.println ((String) retobj);
}
catch (Throwable e)
{
System.err.println(e);
}
}
}




Here is my question, I could not seem to be able to invoke method add
using reflection with array of integer as parameter.
(java.lang.NoSuchMethodException is thrown)...

Tried the following code:
............
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

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


Got syntax error... Any hints ?
Thanks in advance
 
L

Lee Fesperman

Kamal said:
Dear all

I have the following question on Java reflection. Consider the
following code:

-----------------------------------------------------------
import java.lang.reflect.*;

public class method2
{
public String add (int a[])
{
return "done";
}

public static void main (String args[])
{
try
{
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

partypes [0] = Integer[].class; partypes [0] = int[].class;
 
K

Kevin McMurtrie

Dear all

I have the following question on Java reflection. Consider the
following code:

-----------------------------------------------------------
import java.lang.reflect.*;

public class method2
{
public String add (int a[])
{
return "done";
}

public static void main (String args[])
{
try
{
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

partypes [0] = Integer[].class;

Method meth = cls.getMethod ("add", partypes);
method2 methobj = new method2 ();
Object arglist[] = new Object[1];

arglist [0] = new Integer[]{ new Integer(25),new Integer(24)};
Object retobj = meth.invoke(methobj, arglist);
System.out.println ((String) retobj);
}
catch (Throwable e)
{
System.err.println(e);
}
}
}




Here is my question, I could not seem to be able to invoke method add
using reflection with array of integer as parameter.
(java.lang.NoSuchMethodException is thrown)...

Tried the following code:
............
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

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


Got syntax error... Any hints ?
Thanks in advance


int[] is a subclass of Object and it has a Class.
Only a primitive needs to be wrapped.

public class Test
{
public Test ()
{
System.out.println("Hello");
}

public void arrayMethod (int ints[])
{
for (int i= 0; i < ints.length; ++i)
System.out.println(ints);
}

public static void main (String args[])
{
try
{
Class tstClass = Class.forName("Test");
Method arrayMeth= tstClass.getMethod("arrayMethod", new Class[] {int[].class});
Constructor testConst= tstClass.getConstructor(new Class[0]);
Test tstObject= (Test)testConst.newInstance(null);
arrayMeth.invoke(tstObject, new Object[]{new int[]{2,4,6,10,16}});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
 
F

Filip Larsen

Kamal Zamli wrote
I have the following question on Java reflection. Consider the
following code:

-----------------------------------------------------------
import java.lang.reflect.*;

public class method2
{
public String add (int a[])
{
return "done";
}

public static void main (String args[])
{
try
{
Class cls = Class.forName("method2");
Class partypes[] = new Class [1];

partypes [0] = Integer[].class;

Method meth = cls.getMethod ("add", partypes);
method2 methobj = new method2 ();
Object arglist[] = new Object[1];

arglist [0] = new Integer[]{ new Integer(25),new Integer(24)};
Object retobj = meth.invoke(methobj, arglist);
System.out.println ((String) retobj);
}
catch (Throwable e)
{
System.err.println(e);
}
}
}




Here is my question, I could not seem to be able to invoke method add
using reflection with array of integer as parameter.

Got syntax error... Any hints ?

I think that since you want to invoke the method you reflect for you
must have an integer array anyway to pass on to the method so you might
as well use the Class of that array:

public class ArrayReflectTest {

public int sum(int[] a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a;
}
return sum;
}

public static void main(String[] args) {
try {
Object obj = new ArrayReflectTest();
int[] x = { 1, 2, 3 };
Class[] types = new Class[]{x.getClass()};
Object[] parameters = new Object[]{x};

System.out.println(obj.getClass().getMethod("sum",types).invoke(obj,para
meters));
} catch (Exception ex) {
ex.printStackTrace();
}
}

}


Alternatively, like if you want the class of an array with a component
type only known at runtime, you can use the
java.lang.reflect.Array.newInstance methods to indirectly give you the
class:

public static Class arrayType(Class componentType) {
return java.lang.reflect.Array.newInstance(componentType,
0).getClass();
}



Regards,
 
C

Chris Smith

Filip Larsen said:
I think that since you want to invoke the method you reflect for you
must have an integer array anyway to pass on to the method so you might
as well use the Class of that array:

This is a dangerous practice in general. It will work in the case of
int[], but whenever there is any possibility of subclassing of the
parameter type, it should not be used. The class of the object being
passed may not match the type of the parameter, and if it doesn't then
the call to getMethod will fail.

Best to just use a literal for the desired type (in this case, as others
have said, it's int[].class).

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
F

Filip Larsen

I wrote

and Chris Smith answered
This is a dangerous practice in general. It will work in the case of
int[], but whenever there is any possibility of subclassing of the
parameter type, it should not be used. The class of the object being
passed may not match the type of the parameter, and if it doesn't then
the call to getMethod will fail.

Best to just use a literal for the desired type (in this case, as others
have said, it's int[].class).

You are right, it probably is too dangerous to use the class of the
parameter since it then may appear to some people that the getMethod can
retrieve methods based on the actual parameter types even when these are
true subtypes of the formal parameter types.

And if the array component type is fixed at int, and not variable as I
somehow read it from the original question, then int[].class sure is a
perfect fit.


Regards,
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top