0

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);
1
  • If you want to use the IDebugger interface, directly, you do have to import it and implement it. Otherwise, you have to use reflection to explicitly access the methods which you (as programmer) know are there because you (as programmer) know that the object implements IDebugger. Commented Apr 1, 2014 at 19:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That would be good, but I dont have the IDebuger in my project, it is in external jar which I can only load by URLClassLoader

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.