Refactoring in Java

What is refactoring?

Java refactoring is a process of system-wide code changes without affecting the system’s user interfaces and experiences.

Refactoring Steps

  1. Create working copies of selected Java files
  2. Analyze refactoring possibilities
  3. Apply the refactoring
  4. Compare working copies with original sources
  5. Persist wanted refactoring to original sources

Java Refactoring with a Tool

To help developers refactor in Java, we have developed a tool called jSparrow. jSparrow is an IDE extension that aids Java developers in refactoring system-wide Java code with a rule-based approach.

It reviews system-wide code constructs, parameters, strings, temporary files, lambda expressions, if-statements, for-loops, while-loops, forEach-loops, logging statements, and closing statements in Java projects, detects old Java language constructs, and empty variables, and suggests replacements for the identified refactorings. It also allows users to rename fields, remove unused fields, methods, and classes, and replace standard output statements with logger statements.

By installing jSparrow for free to your Eclipse IDE you can view the refactorings on your project directly.

Tool Features

Java refactoring117 granular rules, including refactorings for Lambda Logging, Loops, Testing, and more
Quick-refactorings92 quick-fixes which adapt to your code in real-time and provide refactorings
Three customizable refactorings Refactor standard output methods to Logger, Remove Unused Code, and Rename Fields to Java Conventions.
A table summarizing jSparrow’s features

To see which rules are included and other technicalities please read more about jSparrow.

Java Refactoring Examples

Java Refactoring example: Replace Nested Loops with flatMap

One of our most daily visited rules is the “Replace Nested Loops with flatMap”. This rule finds the nested invocations of Stream::forEach which are used to iterate over such data structures and replaces them with single invocations of Stream::flatMap.

Before refactoring: -> !row.isEmpty()).forEach(row -> { -> !col.isEmpty()).forEach(col -> {
   -> !cell.isEmpty()).forEach(cell -> {
       -> !element.isEmpty()).map(element -> element.substring(0, 1)).forEach(element -> {

After refactoring: -> !row.isEmpty()).flatMap(row ->
    .filter(col -> !col.isEmpty())
    .flatMap(col ->
    .filter(cell -> !cell.isEmpty())
    .flatMap(cell ->
    .filter(element -> !element.isEmpty())
    .map(element -> element.substring(0, 1))
    .forEach(element -> {

Java Refactoring example: Replace Expression Lambda with Method Reference

Another highly visited refactoring rule is the “Replace Expression Lambda with Method Reference”. This rule simplifies the expression lambdas by using method reference. Additionally, you can automate this free-forever refactoring rule through our jSparrow Trial.

Before refactoring:

Set<Person> persSet = transferElements(personList, () -> new HashSet<>());

After refactoring:

Set<Person> persSet = transferElements(personList, HashSet::new);

Get Started with jSparrow

The main goal of jSparrow is to help developers automate system-wide Java refactoring tasks. To help developers get started we offer 20 free-forever refactoring rules which you can register for any time.

Rules included to get started:

jSparrow is a brand of ©2004-2022 Splendit IT Consulting GmbH