Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automatic Java Refactoring

Daniela Baumgartner
January 16, 2019
48

Automatic Java Refactoring

The relevance of code refactoring and our journey to develop a tool for automatic Java refactoring, jSparrow.

Daniela Baumgartner

January 16, 2019
Tweet

Transcript

  1. A journey to automatic Java refactoring Software Quality Days 2019

    Martin Huter & Ardit Ymeri A journey to automatic Java refactoring (and jSparrow)
  2. A journey to automatic Java refactoring Agenda ▪ Refactoring ▪

    Software aiding the refactoring process ▪ Creating an automatic refactoring tool Slide 3
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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(); });
  8. 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);
  9. 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 … }
  10. 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(); }
  11. A journey to automatic Java refactoring Assessing Technical Debt Slide

    14 ▪ Existing Solutions ▪ Integration in development process ▪ Reporting technical debt ▪ Evaluating efforts
  12. 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
  13. A journey to automatic Java refactoring Resolving Automatically ▪ Spotting

    issues ▪ Generating refactored code ▪ Avoiding Conflicts ▪ Preserving Functionality ▪ Test automation ▪ Preserving Comments Slide 16
  14. A journey to automatic Java refactoring Change source with AST

    Slide 18 1 Parsing source to AST First you need a tool that w ill parse your source code like a com piler 2 M anipulating AST Use the resulting AST to adopt the code and apply fixes 3 Assure syntax still valid Verify changes result in a valid source code 4 W riting Changes Apply the changes to the source file and replace it
  15. 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
  16. A journey to automatic Java refactoring jSparrow Slide 20 Eclipse

    JDT Eclipse Plugin GitHub App Maven Plugin
  17. 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
  18. A journey to automatic Java refactoring jSparrow Features ▪ 53

    Rules and growing ▪ Custom rule profiles ▪ Portable between platforms ▪ Built-in review functionality Slide 22
  19. 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