Giter Site home page Giter Site logo

reextractor's Introduction

Table of Contents

General info

ReExtractor is a library/API written in Java that can detect refactorings applied between two successive versions of a Java project.

Currently, it supports the detection of the following refactorings:

supported by ReExtractor 1.0 and newer versions

  1. Rename Method
  2. Rename Attribute
  3. Rename Class
  4. Move Class
  5. Extract Class
  6. Extract Method
  7. Inline Method
  8. Change Return Type
  9. Change Attribute Type

supported by ReExtractor 2.0 and newer versions

  1. Move Method
  2. Move Attribute
  3. Pull Up Method
  4. Pull Up Attribute
  5. Push Down Method
  6. Push Down Attribute
  7. Extract Superclass
  8. Extract Interface
  9. Extract and Move Method
  10. Move and Rename Class
  11. Extract Subclass
  12. Extract Variable
  13. Inline Variable
  14. Rename Variable
  15. Rename Parameter
  16. Move and Rename Attribute
  17. Change Variable Type
  18. Change Parameter Type
  19. Move and Rename Method
  20. Move and Inline Method
  21. Add Method Annotation
  22. Remove Method Annotation
  23. Modify Method Annotation
  24. Add Attribute Annotation
  25. Remove Attribute Annotation
  26. Modify Attribute Annotation
  27. Add Class Annotation
  28. Remove Class Annotation
  29. Modify Class Annotation
  30. Add Parameter Annotation
  31. Remove Parameter Annotation
  32. Modify Parameter Annotation
  33. Add Variable Annotation
  34. Remove Variable Annotation
  35. Modify Variable Annotation
  36. Add Parameter
  37. Remove Parameter
  38. Reorder Parameter
  39. Add Thrown Exception Type
  40. Remove Thrown Exception Type
  41. Change Thrown Exception Type
  42. Change Method Access Modifier
  43. Change Attribute Access Modifier
  44. Add Method Modifier (final, static, abstract, synchronized)
  45. Remove Method Modifier (final, static, abstract, synchronized)
  46. Add Attribute Modifier (final, static, transient, volatile)
  47. Remove Attribute Modifier (final, static, transient, volatile)
  48. Add Variable Modifier (final)
  49. Add Parameter Modifier (final)
  50. Remove Variable Modifier (final)
  51. Remove Parameter Modifier (final)
  52. Change Class Access Modifier
  53. Add Class Modifier (final, static, abstract)
  54. Remove Class Modifier (final, static, abstract)
  55. Change Type Declaration Kind (class, interface, enum)
  56. Replace Loop with Pipeline
  57. Replace Anonymous with Lambda
  58. Replace Pipeline with Loop
  59. Split Conditional
  60. Invert Condition
  61. Merge Conditional
  62. Change Loop Type *
  63. Merge Declaration and Assignment *
  64. Replace if with Ternary Operator *
  65. Loop Interchange *

  * not supported by other refactoring detection tools

Requirements

Java 17 or newer

Apache Maven 3.8 or newer

API usage guidelines

ReExtractor can automatically detect refactorings between two successive versions of a git repository.

In the code snippet below we demonstrate how to print all refactorings performed at a specific commit in the toy project https://github.com/danilofes/refactoring-toy-example.git. The commit is identified by its SHA key, such as in the example below:

GitService gitService = new GitServiceImpl();
try (Repository repo = gitService.openRepository("E:/refactoring-toy-example")) {
  RefactoringExtractorService extractor = new RefactoringExtractorServiceImpl();
  extractor.detectAtCommit(repo, "d4bce13a443cf12da40a77c16c1e591f4f985b47", new RefactoringHandler() {
    @Override
    public void handle(String commitId, List<Refactoring> refactorings) {
      System.out.println("Refactorings at " + commitId);
      for (Refactoring ref : refactorings) {
        System.out.println(ref.toString());
      }
    }
      
    @Override
    public void handleException(String commit, Exception e) {
      System.err.println("Error processing commit " + commit);
      e.printStackTrace(System.err);
    }
  });
}

Location information

All classes implementing the Refactoring interface include refactoring-specific location information. For example, ExtractOperationRefactoring offers the following methods:

  1. getSourceOperationBeforeExtraction() : Returns the code entity associated with the source method in the parent commit
  2. getSourceOperationAfterExtraction() : Returns the code entity associated with the source method in the child commit
  3. getExtractedOperation() : Returns the code entity associated with the extracted method in the child commit

Each code entity offers the LocationInfo getLocation() method to return a LocationInfo object including the following properties:

String filePath
int startLine
int endLine
int startColumn
int endColumn

Alternatively, you can use the methods LocationInfo leftSide() and LocationInfo rightSide() to get the LocationInfo objects for the left side (i.e., parent commit) and the right side (i.e., child commit) of the refactoring, respectively.

How to build and run

Command line

  1. Clone repository

    git clone https://github.com/lyoubo/ReExtractor.git

  2. Cd in the locally cloned repository folder

    cd ReExtractor

  3. Build ReExtractor

    mvn install

  4. Run the API usage example shown in README

    mvn compile exec:java -Dexec.mainClass="org.reextractor.ReExtractor" -Dexec.args="-c refactoring-toy-example d4bce13a443cf12da40a77c16c1e591f4f985b47"

IntelliJ IDEA

  1. Clone repository

    git clone https://github.com/lyoubo/ReExtractor.git

  2. Import project

    Go to File -> Open...

    Browse to the root directory of project ReExtractor

    Click OK

    The project will be built automatically.

  3. Run the API usage examples shown in README

    From the Project tab navigate to org.reextractor.ReExtractor

    Right-click on the file and select Run ReExtractor.main()

You can add the -json <path-to-json-file> command arguments to save the JSON output in a file. The results are appended to the file after each processed commit.

In both cases, you will get the output in JSON format:

{
  "results": [
    {
      "repository": "https://github.com/danilofes/refactoring-toy-example.git",
      "sha1": "d4bce13a443cf12da40a77c16c1e591f4f985b47",
      "url": "https://github.com/danilofes/refactoring-toy-example/commit/d4bce13a443cf12da40a77c16c1e591f4f985b47",
      "refactorings": [
        {
          "type": "MOVE_OPERATION",
          "description": "Move Method	public barkBark(manager DogManager) : void from class org.animals.Dog to public barkBark(dog Dog) : void from class org.DogManager",
          "leftSideLocation": [
            {
              "filePath": "src/org/animals/Dog.java",
              "startLine": 14,
              "endLine": 21,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "original method declaration",
              "codeElement": "public barkBark(manager DogManager) : void"
            }
          ],
          "rightSideLocation": [
            {
              "filePath": "src/org/DogManager.java",
              "startLine": 25,
              "endLine": 32,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "moved method declaration",
              "codeElement": "public barkBark(dog Dog) : void"
            }
          ]
        },
        {
          "type": "RENAME_PARAMETER",
          "description": "Rename Parameter	manager : DogManager to dog : Dog in method public barkBark(dog Dog) : void from class org.DogManager",
          "leftSideLocation": [
            {
              "filePath": "src/org/animals/Dog.java",
              "startLine": 14,
              "endLine": 14,
              "startColumn": 23,
              "endColumn": 41,
              "codeElementType": "SINGLE_VARIABLE_DECLARATION",
              "description": "original variable declaration",
              "codeElement": "manager : DogManager"
            },
            {
              "filePath": "src/org/animals/Dog.java",
              "startLine": 14,
              "endLine": 21,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "original method declaration",
              "codeElement": "public barkBark(manager DogManager) : void"
            }
          ],
          "rightSideLocation": [
            {
              "filePath": "src/org/DogManager.java",
              "startLine": 25,
              "endLine": 25,
              "startColumn": 23,
              "endColumn": 30,
              "codeElementType": "SINGLE_VARIABLE_DECLARATION",
              "description": "renamed variable declaration",
              "codeElement": "dog : Dog"
            },
            {
              "filePath": "src/org/DogManager.java",
              "startLine": 25,
              "endLine": 32,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "method declaration with renamed variable",
              "codeElement": "public barkBark(dog Dog) : void"
            }
          ]
        },
        {
          "type": "CHANGE_PARAMETER_TYPE",
          "description": "Change Parameter Type	manager : DogManager to dog : Dog in method public barkBark(dog Dog) : void from class org.DogManager",
          "leftSideLocation": [
            {
              "filePath": "src/org/animals/Dog.java",
              "startLine": 14,
              "endLine": 14,
              "startColumn": 23,
              "endColumn": 41,
              "codeElementType": "SINGLE_VARIABLE_DECLARATION",
              "description": "original variable declaration",
              "codeElement": "manager : DogManager"
            },
            {
              "filePath": "src/org/animals/Dog.java",
              "startLine": 14,
              "endLine": 21,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "original method declaration",
              "codeElement": "public barkBark(manager DogManager) : void"
            }
          ],
          "rightSideLocation": [
            {
              "filePath": "src/org/DogManager.java",
              "startLine": 25,
              "endLine": 25,
              "startColumn": 23,
              "endColumn": 30,
              "codeElementType": "SINGLE_VARIABLE_DECLARATION",
              "description": "changed-type variable declaration",
              "codeElement": "dog : Dog"
            },
            {
              "filePath": "src/org/DogManager.java",
              "startLine": 25,
              "endLine": 32,
              "startColumn": 2,
              "endColumn": 3,
              "codeElementType": "METHOD_DECLARATION",
              "description": "method declaration with changed variable type",
              "codeElement": "public barkBark(dog Dog) : void"
            }
          ]
        }
      ]
    }
  ]
}

How to add as a maven dependency

Maven Central

To add ReExtractor as a maven dependency in your project, add the following snippet to your project's pom.xml:

<dependency>
  <groupId>io.github.lyoubo</groupId>
  <artifactId>refactoring-extractor</artifactId>
  <version>2.3.0</version>
</dependency>

Tool

To get refactoring information when inspecting a commit of a Java project, you can use our RefactoringExtractor tool.

  • Git Repository: Local or cloned Java project
  • Commit SHA1: Git commit ID
  • JSON File Path: JSON output file

clone repository

reextractor's People

Contributors

lyoubo avatar

Stargazers

Taketoday avatar HUI LIU avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.