I have a server object we are calling via soap. That server object has an XML cfg file associated with it (to be built). That XML contains a "Class" and "Method" name in it. I am trying to figure out how to use reflection to create a class and call a method on that class dynamically.
This way, we can add new action classes, without actually changing our code.
Here are some code notes I have currently, but I was wondering if you have done this before:
//* Load the XML config for the obj name.
//cfg.load(obj.getName());
//* Then we need to determine the AO class file associated with that (obj).
//Class ao = Class.forName(cfg.get("obj.getName()", "className"));
Class aoc = Class.forName("AccountAO");
//* Get an object from the AO Object Class.
//Object aoo = aoc.getInstance();
//* Get the actual method
//Method aom = ao.getMethod(cfg.get("obj.getName()", "className"));
Method aom = ao.getMethod(methodName);
Object returnObj = aom.invoke(aoc);
-
Invoking Methods dynamically (2 messages)
- Posted by: Mick Knutson
- Posted on: November 29 2001 11:32 EST
Threaded Messages (2)
- Invoking Methods dynamically by Jason Weiss on November 29 2001 23:57 EST
- Invoking Methods dynamically by Leonard Gurevich on December 12 2001 14:27 EST
-
Invoking Methods dynamically[ Go to top ]
- Posted by: Jason Weiss
- Posted on: November 29 2001 23:57 EST
- in response to Mick Knutson
Take a look at the invoke() method on org.apache.soap.server.RPCRouter servlet for some strong examples.
hth,
Jason Weiss
Manager, Software Engineering
eBusiness Systems Group -
Invoking Methods dynamically[ Go to top ]
- Posted by: Leonard Gurevich
- Posted on: December 12 2001 14:27 EST
- in response to Mick Knutson
//*******************************************************
//This allow to call many methods with one call to EJB
//*******************************************************
Client code:
HashMap hm = new HashMap();
ArrayList args = new AttayList();
args.add(new Integer(1), "SomeString", new Double(3.6));
args.add(...);
args.add(...);
args.put("yourMethodName", args)
......................
HashMap result = ejbServices.invokeMethod(hm);
//********************************************************
// This class can be used as superclass for EJB
// and allow to call any methods on subclass EJB
//********************************************************
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import java.lang.reflect.Method;
public class EjbServices {
public EjbServices() {}
public HashMap invokeMethod(HashMap value){
String key = null;
HashMap result = new HashMap();
try{
Set keys = parms.keySet();
Iterator iterator = keys.iterator();
while (iterator.hasNext()){
key = (String)iterator.next();
ArrayList argsList = (ArrayList)value){.get(key);
int argsCount = argsList.size();
Object[] args = new Object[argsCount];
Class[] argsClass = new Class[argsCount];
for (int i = 0; i < argsCount;i++){
argsClass[i] = argsList.get(i).getClass();
args[i] = argsList.get(i);
}
Method m = this.getClass().getDeclaredMethod(key, argsClass);
Object ob = m.invoke(this, args);
result.put(key, ob);
}
}catch(Throwable e){
// error checking...
e.printStackTrace();
}
return result;
}
}