JAVA Reflection

I

imran

Hi,

I have a little problem on reflection (how to pass many types of
data), can someone help me please. Actually i am still beginner in
JAVA programming, and i try to design fault injection. Below is the
source code( Kit.java and Tst1.java) i modify from xito web site :

http://xito.sourceforge.net/documentation/javadoc/reflectKit/index.html

------------------------------------------------------
public class Tst1
{
private int x;
public Tst1()
{
x = 10;
}
public int sampleMethod(int ms)
{
System.out.println(ms);
return x;
}
}

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

public class Kit
{
public static void main (String args[])
{
Reflection kit = Reflection.getToolKit();
try
{
Class cls = kit.findClass ("Tst1");
Object my = kit.newInstance(cls);
Object a = kit.call(my,"sampleMethod",20); //my problem here
}
catch (ClassNotFoundException err1)
{
}
catch (java.lang.reflect.InvocationTargetException err1)
{
}
catch (org.xito.reflect.reflectException err1)
{
}

}
}
 
H

Heiner Kücker

imran wrote
Hi,

I have a little problem on reflection (how to pass many types of
data), can someone help me please. Actually i am still beginner in
JAVA programming, and i try to design fault injection. Below is the
source code( Kit.java and Tst1.java) i modify from xito web site :

http://xito.sourceforge.net/documentation/javadoc/reflectKit/index.html

------------------------------------------------------
public class Tst1
{
private int x;
public Tst1()
{
x = 10;
}
public int sampleMethod(int ms)
{
System.out.println(ms);
return x;
}
}

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

public class Kit
{
public static void main (String args[])
{
Reflection kit = Reflection.getToolKit();
try
{
Class cls = kit.findClass ("Tst1");
Object my = kit.newInstance(cls);
Object a = kit.call(my,"sampleMethod",20); //my problem here
}
catch (ClassNotFoundException err1)
{
}
catch (java.lang.reflect.InvocationTargetException err1)
{
}
catch (org.xito.reflect.reflectException err1)
{
}

}
}
---------------------------------------------------------------------------
my problem is how can i modify Kit.java to make sure it can pass many
types of data ( integer, string, char ). In this sourcecode "Object a
= kit.call(my,"sampleMethod",20);", it just pass one integer. How can
i pass many types of data....

Try the Method#invoke method

http://java.sun.com/j2se/1.4.2/docs...#invoke(java.lang.Object, java.lang.Object[])


Heiner Kuecker
Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
 
J

John C. Bollinger

imran said:
I have a little problem on reflection (how to pass many types of
data), can someone help me please. Actually i am still beginner in
JAVA programming, and i try to design fault injection. Below is the
source code( Kit.java and Tst1.java) i modify from xito web site :

http://xito.sourceforge.net/documentation/javadoc/reflectKit/index.html

Have you considered using a unit testing framework instead? JUnit
(http://www.junit.org/) is the de facto standard. Unless you're looking
to randomly or systematically throw arguments at methods, you're going
to have to employ knowledge of the methods to choose arguments. Once
you decide to do that, reflective method invocation only has liabilities
relative to normal Java method invocation.

Note also, Java reflection is a bit arcane and has considerable
disadvantages in general relative to normal Java object usage. There
are tasks for which reflection is indispensable, but they are uncommon,
and those that involve reflective method invocation even more so. I
would in no case recommend that a Java newbie work with reflection.


John Bollinger
(e-mail address removed)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top