Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JAVA Reflection

Reply
Thread Tools

JAVA Reflection

 
 
imran
Guest
Posts: n/a
 
      01-18-2005
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/document...Kit/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....
 
Reply With Quote
 
 
 
 
Heiner Kücker
Guest
Posts: n/a
 
      01-18-2005
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/document...Kit/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/...va.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


 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      01-18-2005
imran wrote:

> 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/document...Kit/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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining Java Reflection API with Java Annotation Types for Thread Safety pek Java 2 10-23-2007 03:00 PM
JAVA reflection imran Java 1 01-15-2005 09:35 PM
Java Reflection: Is there an inverse of Class.getComponentType() Benjamin Johnston Java 1 01-06-2005 06:36 AM
Java Reflection Exception Jungle alan@engrm.com Java 23 01-04-2005 08:18 PM
Reflection in Java - Please help.. A. Wiebenga Java 2 02-08-2004 10:11 AM



Advertisments