Link to home
Start Free TrialLog in
Avatar of HuangJo
HuangJo

asked on

Dynamic Methods Selection

Hi friends,

I would like to write a class that allow user to enter key no.  to activate the methods selected, do you think it is possible?
For eg:

I declare an ArrayList containing names string of methods: "method1", "method2", "method3".
ArrayList aList = new ArrayList() ;
aList.add("method1");
aList.add("method2");
aList.add("method3");

All the three methods got same interface, but different function.For eg.
public byte[] method1(int data)
{}

Can I select the method by this way:

public byte[] selectMethod(int ID, int data)
{
     byte[] result = ((aList.get(ID)(data)) ;
     
     return result ;

}

I got error when compiling, ')' expected.

Do you know ways to dynamically select the functions, apart from using if-else or switch-cast? As the list of methods, I hope to make it dynamic, that is the no. of methods might be changed in future. and might be downloaded from the server etc.

Please advise...Thank you very much...

regards HuangJo
Avatar of vivexp
vivexp

Hi

In the statement  byte[] result = ((aList.get(ID)(data)) ;


The syntax of public Object get(int index);

U cant pass data here that could be the reason it is giving ')' required.

check...
check the java.lang.reflect.* package. (and any book talking about Java Reflection)
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,
   You can go for Reflection,which is a technique using that we can find the information about the class at run time.Once you loaded the class file,you can find out what are all the methods available,constructors available,base class of that class,etc.And you can get the available methods and you can execute the particular method.
   In the book "Thinking in Java" by Bruceekkel,one topic "RunTime Type Identification" is there.You can find more information Reflection.

Here is another example from Sun...

import java.lang.reflect.*;

public class DumpMethods {
  public static void main(String args[])
  {
    try {
      Class c = Class.forName(args[0]);
      Method m[] = c.getDeclaredMethods();
      for (int i = 0; i < m.length; i++)
        System.out.println(m[i].toString());              
    }
               
    catch (Throwable e) {
      System.err.println(e);
    }
  }
}


Invoke this program with a class name like this:

java DumpMethods java.util.Stack
Avatar of HuangJo

ASKER

Sorry Jod, I am not sure why the chart show only 10pts, I did awarded 100pts and it was deducted from my account too. Is it something a technical error? The last time the same thing happen too, is it normal?
Avatar of HuangJo

ASKER

Thank you very much to all the expert friends who answered this questions, and for all the advices that I have received.

regards Jo
It's always a pleasure to help out someone who appreciates it HuangJo. We all learn a little every day.

Once the question is answered the points are awarded as you specified. The points shown then becomes the amount of points someone has to pay to get the answer if they are not myself or you for example.

If you look at all the previosly answered questions (PAQs) you will see that you cannot see the answer until you pay the appropriate number of points. To ask someone to pay 100 points for a question that has already been answered would be too much so the price changes in this case from 100 to 10.

Make sense?
Avatar of HuangJo

ASKER

Thank you very much, Jod. Now I understand...