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
HuangJoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vivexpCommented:
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...
0
heyhey_Commented:
check the java.lang.reflect.* package. (and any book talking about Java Reflection)
0
JodCommented:
You can identify methods from their names and from the parameters they take using reflection.

For example, if you want to find methods in an object myObject and you know the string name of the object then you can do this...

String s = "myMethod";

//this will get you all the methods in the class
Method[] theMethods = myObject.getClass().getMethods();

for (i=0; i < theMethods.length; i++) {
  if ( theMethods[i].getName().equals(s) ) {
    System.out.println("Found a matching method for: " + s);
    Class[] c = theMethods[i].getParameterTypes();
    for (j=0; j < c.length; j++) {
      System.out.println("Parameters: " + c[i].toString() );
    }
  }
}

The above will find a matching method for a given name if you only have one method with the specified name. Otherwise you will have to use the getMethod method and pass in the parameters to give a full signature to the JVM as to which method you want.

Once you have identified your method object you can invoke it and run it like this...

theMethod.invoke(myObject, new Object[] {parameters to method} );

So, to invoke a method like this:

myObject.met("String");

you would do:

String s = "String";
theMethod.invoke(myObject,new Object[] { (Object)s } )

Or something similar. Trying to remember this off hand is not that easy...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

g_senthilkumarCommented:
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.

0
JodCommented:
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
0
HuangJoAuthor Commented:
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?
0
HuangJoAuthor Commented:
Thank you very much to all the expert friends who answered this questions, and for all the advices that I have received.

regards Jo
0
JodCommented:
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?
0
HuangJoAuthor Commented:
Thank you very much, Jod. Now I understand...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.