Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut in Eclipse for generating code for decorators?

Tags:

I was wondering if there was some functionality for generating the skeletons of a decorator from an interface similar to Generate Setters and Getters. I would like to create a class from an interface that took an instance of that interface and implemented every method as calling that same method with those same parameters on the instance. I have an ugly class from a library that I would like to decorate but it has ~50 methods and I only want to change the functionality of one of them and would like to save some typing. Is there anything like this?

Example, given the interface:

 interface FooBar{       void foo( Object o );       Object bar();  } 

produce something like the class:

 class FooBarDecorator implements FooBar{       private final FooBar fubee;        FooBarDecorator( final FooBar fb ){            this.fubee = fb;       }        public void foo( Object o ){            this.fubee.foo( o );       }        public Object bar(){            return this.fubee.bar();       }  } 
like image 832
Sled Avatar asked Apr 05 '11 15:04

Sled


People also ask

What is Ctrl Shift G in eclipse?

Search – Eclipse Shortcuts CTRL SHIFT G – Search for current cursor positioned word reference in workspace. CTRL H – Java search in workspace.

What does ctrl do in Eclipse?

If you press CTRL + I it will just format tabs/whitespaces in code and pressing CTRL + SHIFT + F format all code that is format tabs/whitespaces and also divide code lines in a way that it is visible without horizontal scroll.

How do I get the code for Eclipse?

Use the menu Search->File and in the "Containing Text" field type in "System. out. println*", in the "File name patterns" field type in "*.


1 Answers

Yes!! Right click on your fubee member, select Source -> Generate Delegate Methods -> done! Although, I think that for this to work, you will have to write

class FooBarDecorator implements FooBar { // [...] 

Letting the decorator implement the interface

like image 119
Lukas Eder Avatar answered Sep 20 '22 09:09

Lukas Eder