Dynamic Casting again with array

Discussion in 'Java' started by u.Shanker, Aug 25, 2004.

  1. u.Shanker

    u.Shanker Guest

    Hello !
    Ok. I did my homework, by readin around, but not able to make it generic.
    Problem:
    I have to call ExternalClass.myMethod(InputClass[]);
    Now depending on the cases InputClass and myMethods are different.
    So I want to reflection.

    Solution:
    ExternalClass external = ...
    InputClass rr = .....
    InputClass[] array = new InputClass[] { rr };

    GenerateProtocolObject generator = new GenerateProtocolObject(); // does the
    Class.forName(classname)
    Object o =generator.getObjectInstance("InputClass");

    Class cls = Class.forName("InputClass");
    Object arr = Array.newInstance(cls, 1); // just trying with 1 element in
    array
    Array.set(arr, 0, o);
    Class c = external.getClass();
    Class[] parameterTypes = new Class[] {array}; // <-- this works

    Method concatMethod;
    Object[] arguments = new Object[] { };
    try {
    Method[] mm = c.getMethods();
    concatMethod = c.getMethod("myMethod", parameterTypes);

    --
    but I want something like
    Class[] parameterTypes = new Class[] {o.getClass()};
    Class[] parameterTypes = new Class[] {o[]};
    Class[] parameterTypes = new Class[] {arr};

    thanks for any comments
    uma
     
    u.Shanker, Aug 25, 2004
    #1
    1. Advertising

  2. u.Shanker wrote:

    > Hello !
    > Ok. I did my homework, by readin around, but not able to make it generic.
    > Problem:
    > I have to call ExternalClass.myMethod(InputClass[]);
    > Now depending on the cases InputClass and myMethods are different.
    > So I want to reflection.


    No, you don't. Really. It's the number one rule of Java reflection:
    you don't want to use it. If you find yourself _needing_ to use it then
    you should be at least grumpy about it, and preferably downright disturbed.

    > Solution:
    > ExternalClass external = ...
    > InputClass rr = .....
    > InputClass[] array = new InputClass[] { rr };


    Fortunately for you, ExternalClass is apparently known at compile time,
    so you don't, in fact, need reflection. You can create your own method
    (or methods) on one of your own classes that accepts the argument and
    evaluates the cases sufficiently to dispatch a normal method call to the
    correct method of ExternalClass. This will be easier to write and
    maintain, and chances are that it will perform better (reflection these
    days is fairly fast, but you still have to do extra work in choosing the
    correct Method object, setting up the arguments to the method
    invocation, etc.). This technique is an implementation of the Class
    Adapter design pattern.


    John Bollinger
     
    John C. Bollinger, Aug 26, 2004
    #2
    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. Mike Nishizawa

    Dynamic class casting (yes, again...)

    Mike Nishizawa, Mar 3, 2004, in forum: Java
    Replies:
    4
    Views:
    3,740
    S Manohar
    Mar 5, 2004
  2. kevin
    Replies:
    11
    Views:
    5,811
    Andrew McDonagh
    Jan 8, 2005
  3. James DeClerk
    Replies:
    4
    Views:
    260
    James DeClerk
    Aug 3, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    530
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    568
    James Kanze
    Dec 20, 2009
Loading...

Share This Page