-->

Can Eclipse auto-generate an interface of a 3rd pa

2020-08-20 05:57发布

问题:

I'm working with Apache's FTPClient class in the Apache commons net library. Sadly it doesn't implement an interface for most of the functionality which makes testing classes which use it tricky. So, I thought I'd create my own class which wrappers this one and implements an interface. Anyway that's the background. My question is, is it possible in Eclipse to generate an Interface (similiar to Refactor->Extract Interface) but for 3rd party code sitting in a jar file?

Just to clarify, I'm not looking for FTPClient to now implement the new interface, but to create an interface which mimics the same public API as FTPClient. I can then create my own class which implements this interface and wrappers the FTPClient.

回答1:

Hm. Why not start with an empty class, like

class MyWrapper {

    private Referent client;
}

Then, I'd do "Source -> Generate Delegate Methods", populating the empty class with delegating calls to the underlying original object as I need them. From that class, you can now "Refactor -> Extract interface"... As you need the wrapper for production anyway, this will solve both problems (wrapper generation + interface generation) at once.