Refactoring with Eclipse

Similar documents
Tutorial 02: Writing Source Code

Overview of Eclipse Lectures. Module Road Map

Software Design COSC 4353/6353 D R. R A J S I N G H

Lab Exercise Refactoring using Eclipse

Carrera: Analista de Sistemas/Licenciatura en Sistemas. Asignatura: Programación Orientada a Objetos

Introduction to Eclipse

Objectives: On completion of this project the student should be able to:

Refactoring. Refactoring Techniques

News in RSA-RTE 10.2 updated for sprint Mattias Mohlin, May 2018

Chapter 6 Introduction to Defining Classes

What is Eclipse? A free copy can be downloaded at:

Using Eclipse Europa - A Tutorial

Eclipse Tips and Tricks (JDT)

Understading Refactorings

Chapter 5 Object-Oriented Programming

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, November 2017

CHAPTER 3. Using Java Development Tools

SpringSource Tool Suite 2.7.1

Just Enough Eclipse What is Eclipse(TM)? Why is it important? What is this tutorial about?

Noopur Gupta Eclipse JDT/UI Committer IBM India

This cheat sheet is aimed at people with some experience in eclipse but who may not be aware of many of its features.

How We Refactor, and How We Know It

GETTING STARTED WITH ECLIPSE Caitrin Armstrong

DVT Eclipse IDE. My First Verilog/SystemVerilog Project

6.170 Laboratory in Software Engineering Eclipse Reference for 6.170

Managing Your Website with Convert Community. My MU Health and My MU Health Nursing

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

Adapting JDT to the Cloud. Alex Boyko Pivotal Jay Arthanareeswaran - IBM John Arthorne - IBM

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, January 2018

SpringSource Tool Suite M2

2.4 Structuring programs

Software Development. Modular Design and Algorithm Analysis

DAT159 Refactoring (Introduction)

Advancing Clangd. Bringing persisted indexing to Clang tooling. Marc-André Laperle, Ericsson

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Use this document to quickly review the steps needed to get the most out of your Gmail account.

Moving to Altium Designer from Protel 99 SE. Contents

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Java Object Oriented Design. CSC207 Fall 2014

Programming overview

Refactoring. Paul Jackson. School of Informatics University of Edinburgh

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Automating Big Refactorings for Componentization and the Move to SOA

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Compaq Interview Questions And Answers

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, July 2017

Implementing evolution: Refactoring

Course December Adrian Iftene

The$credit$for$crea-ng$these$slides$belongs$to$ Fall$2014$CS$521/621$students.$$Student$names$ have$been$removed$per$ferpa$regula-ons.

Implementing evolution: Refactoring

CMSC 132: Object-Oriented Programming II

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

CPS 506 Comparative Programming Languages. Programming Language

Refactorings. Refactoring. Refactoring Strategy. Demonstration: Refactoring and Reverse Engineering. Conclusion

Homework #10 due Monday, April 16, 10:00 PM

void printowing(double amount) { printbanner(); printdetails(); void printdetails(double amount) {

Basic Keywords Practice Session

Chapter 4 Java Language Fundamentals

What are the characteristics of Object Oriented programming language?

Recap: Class Diagrams

News in RSA-RTE 10.2 updated for sprint Mattias Mohlin, January 2018

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

A Catalog and Classification of Fortran Refactorings

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, April 2017

Index. Symbols. /**, symbol, 73 >> symbol, 21

Getting Started with Eclipse for Java

WORLD FIRST. In our first ever Flash MX 2004 tutorial, we take a look at the new ease-of-use features that can turn anyone into a Flash guru.

Laboratory Assignment #3 Eclipse CDT

Index. Bitwise operations, 131. Cloud, 88, 101

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Module Road Map. 7. Version Control with Subversion Introduction Terminology

Programming II (CS300)

CHAPTER 6 ACTIONS, METHODS, REFACTORING

Inheritance and Substitution (Budd chapter 8, 10)

Game Design Unity Workshop

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

COURSE 11 DESIGN PATTERNS

User Guide Zend Studio for Eclipse V6.1

SpringSource Tool Suites M3

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Using Eclipse for Java. Using Eclipse for Java 1 / 1

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Refactoring Practice: How it is and How it Should be Supported

HPE Security Fortify Plugins for Eclipse Software Version: Installation and Usage Guide

Rational Functional Tester - Tips and Tricks

Struts Tools Reference Guide. Version: beta1

PROGRAMMING LANGUAGE 2

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

Installation and Upgrade Guide Zend Studio 7.0

12 Duplicate Clips and Virtual Clips

JSF Tools Reference Guide. Version: M5

Getting Started with Eclipse for Java

Introduction to Object-Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Refactoring Practice: How it is and How it Should be Supported An Eclipse Case Study Abstract Introduction

The kdissert handbook

Transcription:

Refactoring with Eclipse Seng 371 Lab 8 By Bassam Sayed Based on IBM article Explore refactoring functions in Eclipse JDT by Prashant Deva

Code Refactoring Code refactoring is a disciplined way to restructure code. [wikipedia] Refactoring process consists of a series of refactorings each of which is (usually) a tiny change in the source code that doesn t modify its functional requirements. [wikipedia] Usually refactoring is motivated by noticing a code problem (smell). For example, very long functions or a near duplicate of another method.

Refactoring Benefits Maintainability: It is easier to fix bugs when the source code is easy to read and understand. Extensibility: It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed [Kerievsky, Joshua (2004). Refactoring to Patterns] It is recommended that before starting the refactoring process a set of unit tests get performed on the source code to demonstrate the correctness of the module. Then this process is repeated with every small change.

Refactoring Techniques Some examples of refactoring techniques: Techniques that allow for more abstraction such as Field Encapsulation (forcing the code to access the fields with setters and getters) Techniques for breaking code apart into more logical pieces such as extracting methods and extracting classes. Techniques for improving names and location of code such as moving methods and fields, renaming methods and fields. Pull Up and Push Down in OOP programming.

JDT Rename: It allows renaming of variables, classes, methods, packages, folders, and almost any Java identifiers. When you rename an identifier, all references to that identifier are also renamed. Shortcut Alt+Shift +R. Move: You can use Move to move a class from one package to another. It physically moves the class to the folder corresponding to the package and also changes all references to the class to refer to the new package. You can drag and drop a class to a new package in the Package Explorer view, and the refactoring will take place automatically.

JDT (Cont.) Extract Local Variable: Allows assigning the result of a java expression to a new local variable. Useful when writing a complex expression by quickly dividing it into multiple lines. To use it press Ctrl+1 and select assign statement to a local variable. Convert local variable to field: takes a local variable and converts it to a private field of the class. All references to the local variable after this refer to the field. To use it press Ctrl+1 and select convert local variable to field

JDT (Cont.) Convert Anonymous Class to Nested: takes an anonymous class and converts it to a nested class of the method that originally contained the anonymous class. To use select Refactor > Convert Anonymous Class to Nested while the cursor is inside the anonymous class.

Convert Anonymous Class to Nested Example void createpool() { threadpool = Executors.newFixedThreadPool(1, new ThreadFactory() { @Override public Thread newthread(runnable r) { Thread t = new Thread(r); t.setname("worker thread"); t.setpriority(thread.min_priority); t.setdaemon(true); return t; } }); }

Convert Anonymous Class to Nested Example private final class MyThreadFactory implements ThreadFactory{ @Override public Thread newthread(runnable r) { Thread t = new Thread(r); t.setname("worker thread"); t.setpriority(thread.min_priority); t.setdaemon(true); return t; } } void createpool(){ threadpool = Executors.newFixedThreadPool(1, new MyThreadFactory()); }

JDT (Cont.) Convert Member Type to Top Level: takes a nested class and converts it to a top-level class with its own Java file. To use place cursor inside the nested class and select Refactor > Convert Member Type to Top Level. If the nested class is a static class, a box showing a preview of the refactoring displays right away. If it is not a static class, you first need to declare the name of the field that will hold the reference to the parent class of the nested class before you get to the preview box.

JDT (Cont.) Extract Interface: makes an interface out of the methods defined in a class. To use this refactoring, select Refactor > Extract Interface from the menu. A dialog box displays, requesting the name of the new interface. You can check the methods from the class that will be declared in the interface.

JDT (Cont.) Extract Superclass: It is similar to the Extract Interface refactoring described earlier. However, the Extract Superclass extracts a superclass instead of an interface. If the class already uses a superclass, the newly generated superclass will have that class as its superclass, maintaining the class hierarchy. To use this refactoring, make sure your cursor is on one of the method declarations or fields of the class and select Refactor > Extract Superclass. A huge difference between extracting a superclass and extracting an interface is that the methods put in the superclass are actually moved there. So, if any of those methods contain references to any fields in the original class, you get a compiler error.

JDT (Cont.) Extract Method: The Extract Method refactoring allows you to select a block of code and convert it to a method. Eclipse automatically infers the method arguments and return types. This is useful when a method is too big and you want to subdivide blocks of it into different methods. It is also useful if you have a piece of code that is reused across many methods. When you select one of those blocks of code and do a refactoring, Eclipse finds other occurrences of that block of code and replaces it with a call to the new method.

JDT (Cont.) Extract Method: To use this refactoring, select a block of code in the editor and press Alt+Shift+M. A dialog box displays, requesting the name and visibility (public, private, protected, or default) of the new method. You can even change the parameters and return types.

Extract Method Example public Object get(object key) { TimedKey timedkey = new TimedKey(System.currentTimeMillis(), key); Object object = map.get(timedkey); if (object!= null) { /** * if this was removed after the 'get' call by the worker thread * put it back in */ map.put(timedkey, object); return object; } } return null;

Extract Method Example public Object get(object key) { TimedKey timedkey = new TimedKey(System.currentTimeMillis(), key); Object object = map.get(timedkey); } return putifnotnull(timedkey, object); private Object putifnotnull(timedkey timedkey, Object object) { if (object!= null) { /** * if this was removed after the 'get' call by the worker thread * put it back in */ map.put(timedkey, object); return object; } } return null;

JDT (Cont.) Inline: The Inline refactoring can inline a reference to a variable or method. When used, it replaces the reference to the variable or method with the value assigned to the variable or the implementation of the method, respectively. This can be useful for cleaning up your code in the following situations: When a method is called only once by another method, and it makes more sense as a block of code. When an expression looks cleaner on one line, rather than split into multiple lines by assigning values to different variables.

Inline Example //Before Inline refactoring public Object put(object key, Object value) { TimedKey timedkey = new TimedKey(System.currentTimeMillis(), key); return map.put(timedkey, value); } //After Inline refactoring public Object put(object key, Object value) { return map.put(new TimedKey(System.currentTimeMillis(), key), value); }

JDT (Cont.) Change Method Signature: The Change Method Signature refactoring allows you to change the signature of a method. It modifies all calls to that method to use the new signature. To use this refactoring, select Refactor > Change Method Signature. The dialog box shown in the next Figure, allowing you to change everything about the method, including adding or removing parameters, changing the order of the parameters, changing the return value type, adding exceptions to the declaration of the method, and even changing the name of the method.

JDT (Cont.) Infer Generic Type Arguments: The Infer Generic Type Arguments refactoring automatically tries to guess the appropriate generic types for classes used in their raw form. This refactoring is generally used to convert pre-java 5 code to Java 5 and later code. This refactoring can even be invoked from the Package Explorer. Simply right-click on any project, package, or class in the Package Explorer and select Refactor > Infer Generic Type Arguments.

Infer Generic Type Arguments Example //Before refactoring private final ConcurrentHashMap map = new ConcurrentHashMap(); //After refactoring private final ConcurrentHashMap<TimedKey, Object> map = new ConcurrentHashMap<TimedKey, Object>();

JDT (Cont.) Migrate JAR File: The Migrate JAR File refactoring allows you to easily upgrade Java Archive (JAR) files on a project's build path. The Migrate JAR File refactoring allows you to do this in one step. To invoke the refactoring, select Refactor > Migrate Jars. In the dialog box that displays, select the location of the new JAR file. In the tree below, select the JAR from the project that will be upgraded to the new version. If you select the Replace Jar file contents but preserve existing filename checkbox, the new JAR file is renamed to match the name of the old JAR file, thus not breaking any build scripts that refer to the JAR file by that name. In any case, when you click Finish, the previous JAR file is deleted, and the new JAR file is copied to its location and automatically added to the project's build path, making your project use the new JAR file.

JDT (Cont.) Refactoring scripts: Refactoring scripts allow you to export and share refactoring actions. This is extremely useful when you are about to distribute a new version of a library that can cause errors for people using the older version. To create a refactoring script, go to Refactor > Create Script. The window shown in the next Figure, showing the history of all the refactorings that have been performed in the workspace. Select the ones you need, specify a location for the script to be generated, then click Create to generate the script.

JDT (Cont.) Refactoring Script: To apply an existing refactoring script to your workspace, select Refactor > Apply Script. In the dialog box that displays, select the location of the script. Click Next to see the refactorings that this script will perform, then click Finish to apply the refactorings.