Is there a way to autogenerate wrappers in Eclipse?

2022-09-02 00:32:34

I have to create several proxies, to add, for example, logging. Something like that:

interface IMath {
    public int add(a, b);
}

class Math implements IMath {
    public int add(a, b) { return a + b; }
}

class MathWithLogs implements IMath {
    private IMath realMath;
    public int add(a, b) {
        Log.d("tag", "valueable info");
        return realMath.add(a, b);
    }
}

Everything is fine as long as these interfaces aren't 20 methods and I have to add something to just one.

My question is, is there a way to autogenerate wrapper classes with some plugin for eclipse?

Or maybe there is a way to do something with annotations to invoke methods from realMath unless stated otherwise (like @Override)?


答案 1

Right click in any source file (.java) and navigate to source -> Override/Implement Methods/Generate Delegate Methods.

The first will paste the body of all methods of your immediate interface. the second will do the same for all the hierarchy up to Object(I guess, not sure). Hope this helps.


答案 2

Yes, there is a source generator called "Generate Delegate Methods" which will do exactly what you want.


推荐