MartinZ
MartinZ

Reputation: 65

Java class implement reflected interface

Is there some way to tell java to think that Object passed to reflected method implements interface of input attribute of method?

public class Debuger /*implements IDebuger*/{
...
}

and this Debuger I need to use in reflected method someDocument.attachDebuger(IDebuger).

I know the structure of IDebuger interface, but I cant just simply write implements IDebuger since it is not in my project. I want to be able to call something similar

Debuger dbg = new Debuger();
Class theClassINeedToImplement = ...;
Object document = ...;
Class docClass = document.GetClass();
/*
HERE call something like 
Object Idbg = theClassINeedToImplement.ForceImplementInterface(dbg);
*/
Method m = docClass.getMethod("attachDebuger", theClassINeedToImplement);
m.invoke(document, Idbg);

Upvotes: 0

Views: 121

Answers (1)

Straw1239
Straw1239

Reputation: 689

I think you need to import IDebuger to your project and then implement it with Debuger. Otherwise the method won't know it is being given an IDebuger, and that will cause a compile error.

Even if its in an external jar, you can import it and use the interface. In Eclipse, right click the project, then select build path, then add external archives. I hope that works.

Upvotes: 1

Related Questions