A journey to automatic Java refactoring
Software Quality Days 2019
Martin Huter & Ardit Ymeri
A journey to automatic Java
refactoring (and jSparrow)
A journey to automatic Java refactoring
Who are we?
Slide 2
A journey to automatic Java refactoring
Agenda
▪ Refactoring
▪ Software aiding the refactoring process
▪ Creating an automatic refactoring tool
Slide 3
A journey to automatic Java refactoring
Management: Do we really need that?
Refactoring
Slide 4
A journey to automatic Java refactoring
What is Refactoring
Slide 5
▪ Changing the code and keeping functionality
▪ Reasons include
▪ Improve readability
▪ Reduce complexity
▪ Improve maintainability
▪ Improve internal architecture
▪ Improve performance
▪ Improve security
▪ Parallelization
A journey to automatic Java refactoring
Typical Problems
▪ Design and code structure
▪ Lack of Cohesion
▪ Tight Coupling
▪ Repeated Code
▪ Spaghetti Code
▪ Lack of Performance
▪ Lack of Tests
▪ Security Issues
Slide 6
A journey to automatic Java refactoring
Human Approach
▪ Define a goal
▪ Understand the code base
▪ Set up a safety net - Tests!
▪ Refactor
▪ Go with baby steps
▪ Test
▪ Does everything still work?
▪ Repeat
Slide 7
Refactor
Test
A journey to automatic Java refactoring
Preserving Functionality
▪ Testing helps - but never guarantees.
▪ Program Equivalence
▪ Do programs P and Q terminate on the same state when starting on the same
one?
▪ Reducible to the Halting Problem.
▪ Undecidable!!!
▪ All nontrivial semantic properties are undecidable 👉 Rice’s Theorem
▪ The good news: Undecidable only in general!
▪ Show correctness for specific cases
▪ Trivial properties are decidable.
Slide 8
A journey to automatic Java refactoring
Using Functional Interfaces
Slide 9
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
passwordField.requestFocusInWindow();
passwordField.requestFocus();
}
});
SwingUtilities.invokeLater(() -> {
passwordField.requestFocusInWindow();
passwordField.requestFocus();
});
A journey to automatic Java refactoring
Using Standard Outputs
Slide 10
System.err.println(e.getMessage());
logger.error(e.getMessage());
e.printStackTrace();
logger.error(e.getMessage(), e);
A journey to automatic Java refactoring
Cognitive Complexity
Slide 11
public void remapIds() {
Attribute idAttribute = getAttributes();
if (idAttribute != null) {
// very long code
…
}
}
public void remapIds() {
Attribute idAttribute = getAttributes();
if (idAttribute == null) {
return;
}
// very long code
…
}
A journey to automatic Java refactoring
Other Code Smells
Slide 12
▪ Methods should not be empty
▪ Deprecated code should be removed
▪ Sections of code should not be commented out
@Deprecated
public static void doSomething(Object changeMe) {
// throw new UnsupportedOperationException();
}
A journey to automatic Java refactoring
There is Software that might help
Help me?
Slide 13
A journey to automatic Java refactoring
Assessing Technical Debt
Slide 14
▪ Existing Solutions
▪ Integration in development process
▪ Reporting technical debt
▪ Evaluating efforts
A journey to automatic Java refactoring
Addressing Technical Debt
Common scenarios for 100k lines
▪ 10s Thousands issues
▪ Hundreds working days
Addressing by:
▪ Avoid creating more debt
▪ Continuously removing debt
▪ Automatically resolving issues
Slide 15
A journey to automatic Java refactoring
Resolving Automatically
▪ Spotting issues
▪ Generating refactored code
▪ Avoiding Conflicts
▪ Preserving Functionality
▪ Test automation
▪ Preserving Comments
Slide 16
A journey to automatic Java refactoring
From Spotting to fixing
Can I automate that?
Slide 17
A journey to automatic Java refactoring
Change source with AST
Slide 18
1
Parsing
source
to
AST
Firstyou
need
a
toolthatw
illparse
your
source
code
like
a
com
piler
2
M
anipulating
AST
Use
the
resulting
AST
to
adoptthe
code
and
apply
fixes
3
Assure
syntax
stillvalid
Verify
changes
resultin
a
valid
source
code
4
W
riting
Changes
Apply
the
changes
to
the
source
file
and
replace
it
A journey to automatic Java refactoring
What framework to use?
Slide 19
Java Grammar Java Compiler Advantages of the Framework
● Open source
● Integrated within Eclipse IDE
1 Eclipse JDT
● Open source
● Integrated within Intellij IDE
2 Javaparser.org
● Mostly independent
● Building a lot from scratch
3 Antlr 4
A journey to automatic Java refactoring
jSparrow
Slide 20
Eclipse JDT
Eclipse Plugin
GitHub App
Maven Plugin
A journey to automatic Java refactoring
Automatic Refactoring
● Pros
+ Save time
+ Reduce debt
+ Set standards
+ Integration in development process
+ Improve coding habits
● Cons
- Technical restrictions
Slide 21
A journey to automatic Java refactoring
jSparrow Features
▪ 53 Rules and growing
▪ Custom rule profiles
▪ Portable between platforms
▪ Built-in review functionality
Slide 22
A journey to automatic Java refactoring
The end
▪ Thanks for your time, we now got some time left to discuss
questions.
▪ You can also visit us at our booth at number 13
Slide 23

Automatic Java Refactoring

  • 1.
    A journey toautomatic Java refactoring Software Quality Days 2019 Martin Huter & Ardit Ymeri A journey to automatic Java refactoring (and jSparrow)
  • 2.
    A journey toautomatic Java refactoring Who are we? Slide 2
  • 3.
    A journey toautomatic Java refactoring Agenda ▪ Refactoring ▪ Software aiding the refactoring process ▪ Creating an automatic refactoring tool Slide 3
  • 4.
    A journey toautomatic Java refactoring Management: Do we really need that? Refactoring Slide 4
  • 5.
    A journey toautomatic Java refactoring What is Refactoring Slide 5 ▪ Changing the code and keeping functionality ▪ Reasons include ▪ Improve readability ▪ Reduce complexity ▪ Improve maintainability ▪ Improve internal architecture ▪ Improve performance ▪ Improve security ▪ Parallelization
  • 6.
    A journey toautomatic Java refactoring Typical Problems ▪ Design and code structure ▪ Lack of Cohesion ▪ Tight Coupling ▪ Repeated Code ▪ Spaghetti Code ▪ Lack of Performance ▪ Lack of Tests ▪ Security Issues Slide 6
  • 7.
    A journey toautomatic Java refactoring Human Approach ▪ Define a goal ▪ Understand the code base ▪ Set up a safety net - Tests! ▪ Refactor ▪ Go with baby steps ▪ Test ▪ Does everything still work? ▪ Repeat Slide 7 Refactor Test
  • 8.
    A journey toautomatic Java refactoring Preserving Functionality ▪ Testing helps - but never guarantees. ▪ Program Equivalence ▪ Do programs P and Q terminate on the same state when starting on the same one? ▪ Reducible to the Halting Problem. ▪ Undecidable!!! ▪ All nontrivial semantic properties are undecidable 👉 Rice’s Theorem ▪ The good news: Undecidable only in general! ▪ Show correctness for specific cases ▪ Trivial properties are decidable. Slide 8
  • 9.
    A journey toautomatic Java refactoring Using Functional Interfaces Slide 9 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { passwordField.requestFocusInWindow(); passwordField.requestFocus(); } }); SwingUtilities.invokeLater(() -> { passwordField.requestFocusInWindow(); passwordField.requestFocus(); });
  • 10.
    A journey toautomatic Java refactoring Using Standard Outputs Slide 10 System.err.println(e.getMessage()); logger.error(e.getMessage()); e.printStackTrace(); logger.error(e.getMessage(), e);
  • 11.
    A journey toautomatic Java refactoring Cognitive Complexity Slide 11 public void remapIds() { Attribute idAttribute = getAttributes(); if (idAttribute != null) { // very long code … } } public void remapIds() { Attribute idAttribute = getAttributes(); if (idAttribute == null) { return; } // very long code … }
  • 12.
    A journey toautomatic Java refactoring Other Code Smells Slide 12 ▪ Methods should not be empty ▪ Deprecated code should be removed ▪ Sections of code should not be commented out @Deprecated public static void doSomething(Object changeMe) { // throw new UnsupportedOperationException(); }
  • 13.
    A journey toautomatic Java refactoring There is Software that might help Help me? Slide 13
  • 14.
    A journey toautomatic Java refactoring Assessing Technical Debt Slide 14 ▪ Existing Solutions ▪ Integration in development process ▪ Reporting technical debt ▪ Evaluating efforts
  • 15.
    A journey toautomatic Java refactoring Addressing Technical Debt Common scenarios for 100k lines ▪ 10s Thousands issues ▪ Hundreds working days Addressing by: ▪ Avoid creating more debt ▪ Continuously removing debt ▪ Automatically resolving issues Slide 15
  • 16.
    A journey toautomatic Java refactoring Resolving Automatically ▪ Spotting issues ▪ Generating refactored code ▪ Avoiding Conflicts ▪ Preserving Functionality ▪ Test automation ▪ Preserving Comments Slide 16
  • 17.
    A journey toautomatic Java refactoring From Spotting to fixing Can I automate that? Slide 17
  • 18.
    A journey toautomatic Java refactoring Change source with AST Slide 18 1 Parsing source to AST Firstyou need a toolthatw illparse your source code like a com piler 2 M anipulating AST Use the resulting AST to adoptthe code and apply fixes 3 Assure syntax stillvalid Verify changes resultin a valid source code 4 W riting Changes Apply the changes to the source file and replace it
  • 19.
    A journey toautomatic Java refactoring What framework to use? Slide 19 Java Grammar Java Compiler Advantages of the Framework ● Open source ● Integrated within Eclipse IDE 1 Eclipse JDT ● Open source ● Integrated within Intellij IDE 2 Javaparser.org ● Mostly independent ● Building a lot from scratch 3 Antlr 4
  • 20.
    A journey toautomatic Java refactoring jSparrow Slide 20 Eclipse JDT Eclipse Plugin GitHub App Maven Plugin
  • 21.
    A journey toautomatic Java refactoring Automatic Refactoring ● Pros + Save time + Reduce debt + Set standards + Integration in development process + Improve coding habits ● Cons - Technical restrictions Slide 21
  • 22.
    A journey toautomatic Java refactoring jSparrow Features ▪ 53 Rules and growing ▪ Custom rule profiles ▪ Portable between platforms ▪ Built-in review functionality Slide 22
  • 23.
    A journey toautomatic Java refactoring The end ▪ Thanks for your time, we now got some time left to discuss questions. ▪ You can also visit us at our booth at number 13 Slide 23