Generate all setXXX calls of a POJO in Eclipse?

前端 未结 5 1444
感动是毒
感动是毒 2021-01-31 03:36

Im currently doing a lot of testing with JPA entities, where i have to keep calling the setter methods on the entity that looks something like this :

myEntity.se         


        
相关标签:
5条回答
  • 2021-01-31 04:28

    Source --> Generate Getters and Setters...

    You can also get at it via the Quick Fix command (Ctrl+1) when the cursor is on a property.


    EDIT

    If you are simply looking for a faster way to copy properties from one object to another I suggest that you look at using reflection. I think this path would be much easier long term then generating the same-looking code over-and-over.

    Commons BeanUtils can take away some of the pain in writing pure reflection code. For example, copyProperties takes a destination bean and either another bean or a Map as the source.

    0 讨论(0)
  • 2021-01-31 04:33

    I like @Oscar's answer. It does lead to some cleanup though.

    When I paste from the clipboard, I get something that looks like this:

    setOne(int)  
    setTwo(String)  
    

    In order to clean this up, I first add semicolons with this search/replace regexp:

    search = (.)$
    replace = \1;
    

    Then I add the getter calls (assuming incoming data object is named "data"):

    search = s(et.*)\(.*  
    replace = s\1(data.g\1());  
    

    This doesn't handle multiple arguments in a method call...

    0 讨论(0)
  • 2021-01-31 04:34

    you can use the outline at right side. There you can sort alphabetically or by declaration order using the toolbar button of the view.

    and then you can filter out non required this.

    From here also you can copy..all setter functions or getters functions names...

    0 讨论(0)
  • 2021-01-31 04:37

    I have found the answer (I was always searching for this thing)...

    The easiest way is to expand the class members in the "Package Explorer", sort them by name, multi-select all the setters, and then you have in the clipboard all the method names...

    ;-)

    0 讨论(0)
  • 2021-01-31 04:39

    There is eclipse plugin to do that. The name of the plugin is **

    FastCode

    **. There are so many templates. Among those there is template to generate code for create object of the class and all setters method.

    0 讨论(0)
提交回复
热议问题