Java Refactoring

Refactoring in Java

blue Java icon

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 refactoring118 granular rules, including refactorings for Lambda Logging, Loops, Testing, and more
Quick-refactorings94 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:

    matrix3.stream().filter(row -> !row.isEmpty()).forEach(row -> {
        row.stream().filter(col -> !col.isEmpty()).forEach(col -> {
            col.stream().filter(cell -> !cell.isEmpty()).forEach(cell -> {
                cell.stream().filter(element -> !element.isEmpty()).map(element -> element.substring(0, 1)).forEach(element -> {
                    System.out.print(element);
                });
            });
        });
    });

After refactoring:

matrix3.stream().filter(row -> !row.isEmpty()).flatMap(row -> row.stream())
    .filter(col -> !col.isEmpty())
    .flatMap(col -> col.stream())
    .filter(cell -> !cell.isEmpty())
    .flatMap(cell -> cell.stream())
    .filter(element -> !element.isEmpty())
    .map(element -> element.substring(0, 1))
    .forEach(element -> {
        System.out.print(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 Splendit IT Consulting GmbH