JAVA Reflection

Discussion in 'Java' started by imran, Jan 18, 2005.

  1. imran

    imran Guest

    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....
    imran, Jan 18, 2005
    #1
    1. Advertising

  2. 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...tml#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
    Heiner Kücker, Jan 18, 2005
    #2
    1. Advertising

  3. 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/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
    John C. Bollinger, Jan 18, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A. Wiebenga

    Reflection in Java - Please help..

    A. Wiebenga, Feb 6, 2004, in forum: Java
    Replies:
    2
    Views:
    490
    Tony Morris
    Feb 8, 2004
  2. Replies:
    23
    Views:
    5,549
  3. Benjamin Johnston
    Replies:
    1
    Views:
    474
    Mike Schilling
    Jan 6, 2005
  4. imran

    JAVA reflection

    imran, Jan 15, 2005, in forum: Java
    Replies:
    1
    Views:
    322
    Mike Schilling
    Jan 15, 2005
  5. pek
    Replies:
    2
    Views:
    1,050
Loading...

Share This Page