{"id":794,"date":"2013-11-30T08:12:30","date_gmt":"2013-11-30T06:12:30","guid":{"rendered":"http:\/\/www.pellissier.co.za\/hermien\/?p=794"},"modified":"2013-11-30T08:12:30","modified_gmt":"2013-11-30T06:12:30","slug":"top-5-java-refactoring-tools-in-the-netbeans-ide","status":"publish","type":"post","link":"https:\/\/www.pellissier.co.za\/hermien\/?p=794","title":{"rendered":"Top 5 Java Refactoring Tools in the NetBeans IDE"},"content":{"rendered":"<p>The NetBeans IDE has some great refactoring tools. It makes lots of tasks so much less error prone than doing them manually. In this article, I describe some of my favourite refactoring tools. (These tools can be accessed from the main menu bar (Refactor) or from the context menu of a Java file open in the editor.)<\/p>\n<p><strong>#1 &#8211; Rename<\/strong><\/p>\n<p>Anything that has a name can be renamed. But my favourite example is renaming a class.<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor01.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-801\" alt=\"refactor01\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor01.png\" width=\"528\" height=\"298\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor01.png 528w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor01-300x169.png 300w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>When a class is renamed, not only the source code of the class (optionally including comments) is changed, but also the name of the file where it lives. And also any usages of the class elsewhere in your open projects!<\/p>\n<p><strong>#2 &#8211; Encapsulate Fields<\/strong><\/p>\n<p>This feature allows you to generate getters, setters and property change support for fields in a class. It allows you to specify various parameters, such as the visibility of the accessors. So lets take a very simple class, and apply the <em>Encapsulate Fields<\/em> tool:<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor021.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-797\" alt=\"refactor02\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor021.png\" width=\"678\" height=\"595\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor021.png 678w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor021-300x263.png 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&#8230; and lets look at the generated code:<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor03.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-798\" alt=\"refactor03\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor03.png\" width=\"686\" height=\"419\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor03.png 686w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor03-300x183.png 300w\" sizes=\"(max-width: 686px) 100vw, 686px\" \/><\/a><\/p>\n<p><strong>\u00a0#3 &#8211; Extract Interface<\/strong><\/p>\n<p>If you have an existing class, and you want to extract some of its methods into an interface, the NetBeans IDE has just the tool for you!<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-806\" alt=\"refactor04\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04.png\" width=\"580\" height=\"601\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04.png 580w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04-289x300.png 289w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04-24x24.png 24w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor04-36x36.png 36w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Not only does the interface get generated, but your class is automatically changed to implement the new interface too!<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor05.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-807\" alt=\"refactor05\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor05.png\" width=\"680\" height=\"472\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor05.png 680w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor05-300x208.png 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n<p><strong>#4 &#8211; Introduce Method<\/strong><\/p>\n<p>Sometimes one wants to extract some lines of code into a separate method. Maybe because the initial method is becoming too long to be readable, or because that part of the functionality will be reused elsewhere. Whatever the motivation, the NetBeans IDE has just the tool for the job! So lets look at a silly example where an identical piece of code occurs twice in the same class:<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor06.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-810\" alt=\"refactor06\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor06.png\" width=\"579\" height=\"518\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor06.png 579w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor06-300x268.png 300w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/a><\/p>\n<p>And then lets look at the result of the refactoring:<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor07.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-811\" alt=\"refactor07\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor07.png\" width=\"459\" height=\"290\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor07.png 459w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor07-300x189.png 300w\" sizes=\"(max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n<p>Tip: Experiment with extracting large, complicated blocks of code &#8211; the NetBeans IDE is capable of much more than this simple example!<\/p>\n<p><strong>#5 &#8211; Change Method Parameters<\/strong><\/p>\n<p>Say we decide to now introduce a new parameter for the extractedMethod() created above. If we did this manually, it would mean changing each place where the method is called manually too. (Sometimes this is desirable, but it can be a lot of effort.) With the <em>Change Method Parameters<\/em> feature, this task becomes much easier, since it allows you to specify a default value for the new parameter.<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor08.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-814\" alt=\"refactor08\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor08.png\" width=\"644\" height=\"610\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor08.png 644w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor08-300x284.png 300w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor08-24x24.png 24w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><\/a><\/p>\n<p>Now our class looks like this:<\/p>\n<p><a href=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor09.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-815\" alt=\"refactor09\" src=\"http:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor09.png\" width=\"532\" height=\"288\" srcset=\"https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor09.png 532w, https:\/\/www.pellissier.co.za\/hermien\/wp-content\/uploads\/sites\/2\/2013\/11\/refactor09-300x162.png 300w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>Tip: Also try out the <em>Create New Method and Delegate from Existing Method<\/em> option!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The NetBeans IDE has some great refactoring tools. It makes lots of tasks so much less error prone than doing them manually. In this article, I describe some of my favourite refactoring tools. (These tools can be accessed from the main menu bar (Refactor) or from the context menu of a Java file open in &#8230; <a title=\"Top 5 Java Refactoring Tools in the NetBeans IDE\" class=\"read-more\" href=\"https:\/\/www.pellissier.co.za\/hermien\/?p=794\" aria-label=\"More on Top 5 Java Refactoring Tools in the NetBeans IDE\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[3,4],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1v8WL-cO","_links":{"self":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/794"}],"collection":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=794"}],"version-history":[{"count":13,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":818,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/794\/revisions\/818"}],"wp:attachment":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}