Вопрос

Are there any good Java refactoring tools that could handle large-scale refactoring projects such as the one below. Obviously I would prefer free if possible.

I am working with a legacy project, most of which was built during the early days of Java. There are many, perhaps even hundreds of classes that use the old style command processing pattern like:

public interface IFrom {
  public static final String COMMAND1 = "Command1";
  public static final String COMMAND2 = "Command2";
}

public class From {
  public void doSomething(String command) {
    if (command.equals(IFrom.COMMAND1)) {
      doSomething1();
    } else if (command.equals(IFrom.COMMAND2)) {
      doSomething2();
    }
  }

  private void doSomething1() {
    System.out.println("Something-1");
  }

  private void doSomething2() {
    System.out.println("Something-2");
  }
}

into something that is much more manageable like:

interface DoSomething {
  public void doSomething();
}

enum ToCommands implements DoSomething {
  Command1 {
    @Override
    public void doSomething() {
      System.out.println("Something-1");
    }
  },
  Command2 {
    @Override
    public void doSomething() {
      System.out.println("Something-2");
    }
  };
}

public class To {
  public void doSomething(String command) {
    ToCommands.valueOf(command).doSomething();
  }
}

I might add that not only are there perhaps several hundred classes that follow this pattern but some of them implement several hundred commands.

Это было полезно?

Решение

Large-scale refactoring usually include custom transformation, e.g., changes to code that are not easily expressed as compositions of classic refactoring operations. If they are, doing this using interactive tools is hard; in essence you have mouse your way through hundreds of changes and you are unreliable as a repeatable process.

Finally, figuring out the chain of actions required is hard; you don't want to do this by hand and get it wrong. You need to the ability to apply the changes mechanically and verify the results over several trials before a production application attempt.

You most likely want to use a programmable refactoring tool. For massive changes, you probably want to consider use of program transformation tools. Such tools allow you to write program transformations as pattern-directed rewriting rules, e.g., "if you see this code, replace it by that code"; normally such rewrites allow pattern variables, too. These are not string replacements; rather they match the program structure (e.g., the parse trees) and can with more sophisticated systems include information about the types of symbols and where information is coming from or going to (dataflow). Because they match program structure and can use deep semantic analysis, they can make reliable changes. Program transformation tools have been used to make massive changes to code bases over the last 20 years. (See my bio if you want to see a specific program transformation tool I consider generally capable of handling complex refactoring tasks).

You'll hear about "model-based" transformation. Usually this means "convert your code to UML class models and then transform them". This approach usually loses the details of the code itself, and it is why the program transformation approach, that operates on code, is much more successful at massive changes.

Другие советы

IntelliJ has extract interface /superclass etc. That will suite you most. See more at http://www.jetbrains.com/idea/features/refactoring.html. I use commercial licence, but I guess refactoring is part of community edition as well.

(For writing a program for refactoring, you could start with eclipse JDT tools. I never used it in large scale)

IntelliJ and other similar refactoring tools produced by JetBrains (e.g. the ReSharper plugin to Visual Studio) have a somewhat little-known but very powerful feature called Structural Search And Replace which can be very useful for this sort of change. You can basically say "Whenever you see code that is structured like THIS, change it so that it looks like THAT." This is different from a textual search-and-replace in that it actually looks at the program structure and parse tree to do its searching and replacement. You can keep lists of such transformations and invoke them as you like. I have found this very useful when transforming old code.

The Eclipse MoDisco project aims at such refactorings. It helps you to convert your Java projects to EMF model instances. Then you can manipulate these models (for example applying the refactoring you mention). There also exist tools for helping you with that task - ATL for example. Then you can use a code generator (Xpand, Acceleo, JET) to convert the EMF model instance back to Java source.

It is some effort to get into all this. But you can get fast support at the eclipse forums if you might get stuck, as far as my experience goes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top