Skip to content
Advertisement

Automatically delegating all methods of a java class

Say I have a class with many of public methods:

JavaScript

Now I would like to create a wrapper class which would delegate all the methods to wrapped instance (delegate):

JavaScript

Now if MyClass has a lot of methods I would need to override each of them which is more or less the same code which just “delegates”. I was wondering if it is possible to do some magic to automatically call a method in Java (so the Wrapper class would need to say “Hey if you call a method on me just go to delegate object and call this method on it).

BTW: I can not use inheritance because the delegate is not under my control.I just get its instance from elsewhere (another case would be if MyClass was final).

NOTE: I do not want IDE generation. I know I can do it with help of IntelliJ/Eclipse, but I’m curious if this can be done in code.

Any suggestions how to achieve something like this? (NOTE: I would probably be able to do it in some scripting languages like php where I could use php magic functions to intercept the call).

Advertisement

Answer

Perhaps the dynamic Proxy of java can help you. It only works if you consequently use interfaces. In this case, I will call the interface MyInterface and set up a default implementation:

JavaScript

The wrapper class implementation would look like:

JavaScript

Note that this class:

  • extends MyClass, to inherit a default implementation (any other would do)
  • implements Invocationhandler, to allow the proxy to do reflection
  • optionally implement MyInterface (to satisfy the decorator pattern)

This solution allows you to override special methods, but to delegate all others. This will even work with sub classes of Wrapper class.

Note that the method findMethod does not yet capture the special cases.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement