Search code examples
javaeclipseintellij-ideaautomated-refactoring

Automatic Refactoring of Monolith


We have a rather large-ish monolithic software we would like to refactor at a larger scale. First step will be to derive several artefacts, which can be compiled independently. Given the size of the application we would like to automate that as much as possible.

An example:

+ package1
|    |
|    + Service1
|
+ package2
|    |
|    + Service2
|
+ interfacepackage

Assuming, Service1 is only used from within package1, it should not be touched. Assuming Service2 is used from Service1 I would like to automatically generate a minimal interface for Service2, put that interface in the package interfacepackage and change the dependency within Service1 to the interface.

Doing this manually would be no trouble at all. Both Idea and Eclipse provide semi-automatic refactorings, but we would like to formulate them as meta-rules. I had hopes, that either eclipse or intellij have a programmatic interface to define such rules, but I have not been able to find them yet.

I have even found the eclipse refactoring scripts but these seem to be restricted to refactorings of named classes, so if I knew all services which should be refactored, eclipse refactoring scripts would help but not if I want to define conditions on classes to be refactored.

Where should I look for a solution?

Clarification: Comment: So what's your problem?

We have a high 3 digit number of services which make up this monolith. These are in approx. 20 different packages. The whole software is approaching 1 million lines of code. My problem is simply the size. Doing refactorings manually could take months, we might miss something doing it manually. Also, de-tangeling the services is the first step only. So we are expecting a lot of similar refactorings applied down the road.


Solution

  • IntelliJ IDEA has an "open api" that can be used for plugin development.

    The advantage is that IntelliJ parses the java code, and the "meta model" is available to you as a plugin author.

    In IntelliJ, the "AST" model refers to the "Abstract Syntax Tree". This structure is invaluable for plugins that do refactorings.

    You can easily see the package structure, class names, code, and so on.

    https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html

    Note! Java functionality for plugin development has been externalised as a plugin.

    https://blog.jetbrains.com/platform/2019/06/java-functionality-extracted-as-a-plugin/

    Please also have a look at my own plugins on github, where I have posted the source code.

    https://github.com/Steve-Murphy/unencapsulate-plugin