Demo of Software Refactoring with Eclipse Giriprasad Sridhara

  • Slides: 29
Download presentation
Demo of Software Refactoring with Eclipse Giriprasad Sridhara CISC 879 Spring 2007 May 15

Demo of Software Refactoring with Eclipse Giriprasad Sridhara CISC 879 Spring 2007 May 15 2007

Road Map • Demo with Eclipse • Demo with Visual Studio (if time permits)

Road Map • Demo with Eclipse • Demo with Visual Studio (if time permits) • Eclipse plug-in to – Do your own refactoring (if time permits) • Examples of refactorings – In real world code • Apache Struts • Free open-source framework – for creating Java web applications

Road Map • Eclipse vs IDEA - Comparison – NOT an IDE comparison! –

Road Map • Eclipse vs IDEA - Comparison – NOT an IDE comparison! – BUT a Comparison of the refactoring support – Like, Eclipse supports Refactoring A, but not IDEA – Refactoring B has a bug in Eclipse but not in IDEA – So on…

Refactoring tool • Why have a refactoring tool (? ) • Is a refactoring

Refactoring tool • Why have a refactoring tool (? ) • Is a refactoring tool redundant – If we have good unit test suite (? ) – i. e. We can easily check behaviour preservation – NO! • Refactoring by hand – Tedious – Error Prone – Time consuming • Without a tool – Developers may NOT refactor!

Job done by refactoring tool • Extract Method Refactoring (manual versus by tool) –

Job done by refactoring tool • Extract Method Refactoring (manual versus by tool) – Remember Extract Method ? • A tool does the following: – – Checks the selected code is legal to extract Computes number of parameters to the method Computes the required return value and type from the method Allows to specify • Name of the method • Order of parameter passing – Extracts the code into the new method – Creates the new method in the original class. • Compare with doing manually – Show In Eclipse (group 2. Extract. Method_etal: : Cnvrt. Temp)

Technical Requirements for a Refactoring Tool • Program database – A searchable repository –

Technical Requirements for a Refactoring Tool • Program database – A searchable repository – For example, • For Rename Method, what do we need? • We need to find all calls to the method – Lexical searches (grep) • Won’t work! • Can’t distiniguish between – a name like “temperature” which can be » A method » A variable » In the program comments

Technical Requirements of a Refactoring tool • Parse Trees (AST) – Refactorings use this

Technical Requirements of a Refactoring tool • Parse Trees (AST) – Refactorings use this – Manipulate this – For Rename Method • Rewrite the AST – Change the Method. Declaration node name – Change all the Method. Invocation node names which referred to the old name • Accuracy – The refactored code should be accurate – i. e. preserve behaviour

Practical Requirements for a Refactoring Tool • Speed – If the refactoring tool is

Practical Requirements for a Refactoring Tool • Speed – If the refactoring tool is slow (? ) – Developer may not refactor at all! – OR may do it by hand with all its associated problems • Undo a refactoring – Without this (? ) – We have to use an old version to revert • Available within the IDE

Eclipse Refactoring support • Mainly Java support via the JDT • Very poor support

Eclipse Refactoring support • Mainly Java support via the JDT • Very poor support for C++ from the CDT – Only Rename refactoring! – This is supposed to improve – With the new CDT 4. 0 release. • The Java refactoring – Available from Main menu – Context (right-click) menu – Undo/redo refactoring • • Merged with Edit->Undo/Redo Menu This is bad! Why? We have to navigate past edit actions performed after a refactoring

Eclipse Refactorings • General work flow • Choose a refactoring from main/context menu •

Eclipse Refactorings • General work flow • Choose a refactoring from main/context menu • If not applicable – For right click • it is disabled – For main menu • • A dialog saying not applicable shows up Informs what has to be selected for applicability Example, For Rename refactoring Must select Method, Variable, Class • Show in eclipse

Eclipse Refactorings • If applicable – Additional dialog for parameters – For Rename method,

Eclipse Refactorings • If applicable – Additional dialog for parameters – For Rename method, – Ask for new method name • Show preview of the refactored code – Shows potential problems – For Rename Method (? ) – If there is a name conflict • A method with the new name and same signature exists in the hierarchy, already • Once user accepts the changes, – do the changes.

Eclipse Refactorings • If there were problems shown in the preview – BUT, the

Eclipse Refactorings • If there were problems shown in the preview – BUT, the user ignored them (? ) – The refactored code will also have the same problems – i. e. it will not compile.

Eclipse Refactoring Menu Organization • Divided into 7 groups • Group 1 – Name

Eclipse Refactoring Menu Organization • Divided into 7 groups • Group 1 – Name and physical organization – Rename and Move • Group 2 – Methods – Extract Method, Change Signature, Inline, Extract Constant, Extract Local variable.

Eclipse Refactoring Menu Organization • Group 3 – Within class – Nested, anonymous classes.

Eclipse Refactoring Menu Organization • Group 3 – Within class – Nested, anonymous classes. • Group 4 – Inheritance hierarchy – Pull up, Push down • Group 5 – Miscellaneous – Indirection, Factory

Eclipse Refactoring Menu Organization • Group 6 – Generics • Group 7 – Scripts

Eclipse Refactoring Menu Organization • Group 6 – Generics • Group 7 – Scripts and Refactoring History – History Shows – Date and time of refactorings – The actual refactorings • Method Cnvrt was renamed to Convert • Additional choices made by the developer – Like, Update the name even in program comments.

Eclipse Refactoring Menu Organization • Record refactorings (Create Script) • Playback refactorings (Apply Script)

Eclipse Refactoring Menu Organization • Record refactorings (Create Script) • Playback refactorings (Apply Script) • Where would this be helpful (? ) – Library developer can do refactorings of APIs – Create script of these refactorings. – He can then distribute it to Library users – Library user can avoid manual updates to sync with the library API change – But playback the refactorings from the script

Microsoft Visual Studio refactorings • No direct support in. Net 2003 version – Believe

Microsoft Visual Studio refactorings • No direct support in. Net 2003 version – Believe there is support in VS 2005, – But I don’t have access to it. – 90 day trial version available. • So used an external trial product – Resharper (Jet. Brains) – Same company that produced Intelli. J IDEA • Resharper – Add on to Visual Studio – Similar support like Eclipse (Main and context menu) – Supports only C# langauge

Eclipse plug-in to do your own refactoring • http: //www 128. ibm. com/developerworks/opensource/ library/os-ecjdt/

Eclipse plug-in to do your own refactoring • http: //www 128. ibm. com/developerworks/opensource/ library/os-ecjdt/

Real world refactorings • Struts – Examples of Refactorings – Between version 1. 1

Real world refactorings • Struts – Examples of Refactorings – Between version 1. 1 and 1. 2 • • Rename Method Pull Up Method Change Method Signature Move Method – Detection mechanism • Refactoring Crawler (Dig et al UIUC) • https: //netfiles. uiuc. edu/dig/Refactoring. Crawler/

Eclipse vs Intellij IDEA • A quick comparison of the refactoring support offered by

Eclipse vs Intellij IDEA • A quick comparison of the refactoring support offered by – Eclipse – Intelli. J IDEA • Basic Facts – Eclipse • Open source (IBM) • Customizable to different languages like Java, C++ – Intelli. J IDEA • Commercial (Jet. Brains http: //www. jetbrains. com/idea ) • Java IDE

Eclipse vs Intellij IDEA • Intelli. J IDEA Pricing – Commercial - $499 –

Eclipse vs Intellij IDEA • Intelli. J IDEA Pricing – Commercial - $499 – Personal - $249 – Academic - $99 – Classroom – Free! – Open Source – Free!

Eclipse vs Intellij IDEA • IDEA does not appear to have – Ability to

Eclipse vs Intellij IDEA • IDEA does not appear to have – Ability to create and apply • refactoring scripts – Does not show history of refactorings clearly – It has “migrate” refactorings • But this is for packages and classes • Also the procedure described appears very tedious • http: //blogs. jetbrains. com/idea/2006/08/switchingbetween-api

Eclipse vs Intellij IDEA • IDEA’s Rename Method refactoring is smarter! – Renames Method

Eclipse vs Intellij IDEA • IDEA’s Rename Method refactoring is smarter! – Renames Method even in Java Reflection API call – i. e. – Class: : get. Method(“old. Name”…) – will be replaced with – Class: : get. Method(“new. Name”…)

Eclipse vs Intellij IDEA • Eclipse’s generify refactoring is better – In my example,

Eclipse vs Intellij IDEA • Eclipse’s generify refactoring is better – In my example, group 6. Infer. Generic. Type. Arg – It inferred generic type for List and Iterator – IDEA did only for Iterator – This is bad! • The List is the type that must be type safe • Introduce Parameter – Eclipse has a bug!! – IDEA does not have it!

Eclipse vs Intellij IDEA • IDEA supports an important refactoring – Replace Inheritance with

Eclipse vs Intellij IDEA • IDEA supports an important refactoring – Replace Inheritance with delegation – This refactoring allows to remedy • Violations of the Liskov Substitution Principle – Eclipse does not support this. • IDEA supports – Replace Temp With Query – Eclipse does not.

Eclipse vs Intellij IDEA • Extract Method – Duplicated Code – Suppose we do

Eclipse vs Intellij IDEA • Extract Method – Duplicated Code – Suppose we do Extract Method – And the same piece of code is • Duplicated at other places – IDEA detects this • Replaces the clones as well, • With the extracted method call! – Officially, Eclipse • is supposed to recognize this • and handle it, • but it does not work always!

Eclipse vs Intellij IDEA • IDEA supports Invert Boolean refactoring – Eclipse does not

Eclipse vs Intellij IDEA • IDEA supports Invert Boolean refactoring – Eclipse does not – But this is a minor refactoring • Preview of refactored code – IDEA and Eclipse show preview – But Eclipse’s preview is better – It shows both the current code and new refactored code juxtaposed for comparison – IDEA does not present such a preview • So it is hard to visualize the changes. • For many refactorings like Extract. Method • IDEA does not present previews at all.

Sources of the presentation • • Refactoring – Martin Fowler Wikipedia Eclipse online documentation

Sources of the presentation • • Refactoring – Martin Fowler Wikipedia Eclipse online documentation IBM Eclipse

Questions? Thank you.

Questions? Thank you.