• Post Reply Bookmark Topic Watch Topic
  • New Topic

reflection problem  RSS feed

 
kelly goedert
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a NoSuchMethodException in a situation like this:

public class Orbit extends Thread implements IOrbit{
public void run(){
URLClassLoader loader = new URLClassLoader(urls);
Class mainClass = loader.loadClass(satellitePOJO.getMainClass());
Class[] executeParams = new Class[]{HashMap.class};
Object satelliteInstance = mainClass.newInstance();
Method[] allMethods = satelliteInstance.getClass().getMethods();
Method executeMethod = mainClass.getMethod("execute", executeParams);
Object[] args = new Object[]{this.args};
executeMethod.invoke(satelliteInstance, args);
Class[] setUpParams = new Class[]{IOrbit.class};
Method setUpMethod = mainClass.getMethod("setUp", setUpParams);
setUpMethod.invoke(satelliteInstance, this);
}
}

In the last line I get the exception. The IOrbit.class just like this:

public interface IOrbit{}

The class on which the methods are invoked is like this:

public class G1IntegrationSatellite extends AbstractSatellite
{
private HashMap<String, String> argumentMap;
public void execute(HashMap<String, String> argumentMap) throws Exception {
//method implementation
}
}

and the AbstractSatellite class is like this:

public abstract class AbstractSatellite
{
private IOrbit orbit;
public abstract void execute(HashMap<String,String> argumentMap) throws Exception;

public void setUp(IOrbit orbit)
{
this.orbit = orbit;
}
}

Why do I get NoSuchMethod exception when trying to execute setup method if it is implemented in the parent class?

Thanks for any help.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd put in a System.out.println(mainClass.getName()) just to make sure that you are workign with the class that you think that you are.

Failing that, I'd loop through all of the methods in the allMethods array that you have to see if the method you're looking for really does exist.

I took your code and ran it here with no problems, but I'm using java 1.4.2; unless it's a problem with 1.5 however, it should work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!