Tuesday, August 3, 2010

API evolution with RefactoringNG

See my article API evolution with RefactoringNG on dzone.

Friday, June 18, 2010


RefactoringNG is a flexible and powerful Java refactoring tool, implemented as NetBeans module. Refactoring rules are described as transformations of source abstract syntax trees to destination abstract syntax trees. For example, the rule that rewrites x = x + 1 to x++ is as follows:

 // x = x + 1 -> x++
 Assignment {
    Identifier [name: "x"],
    Binary [kind: PLUS] {
       Identifier [name: "x"],
       Literal [kind: INT_LITERAL, value: 1]
 } ->
 Unary [kind: POSTFIX_INCREMENT] {
    Identifier [name: "x"]
The names and structure of abstract syntax trees are the same as in Sun Java compiler. The tool uses Compiler Tree API (com.sun.*), the formal language model in the JDK API (javax.language.model.*), and NetBeans infrastructure. For example, if you have ever needed to replace the constructor call with a factory method or add an argument to each method call, you may appreciate how easily such tasks can be done with RefactoringNG:

Factory method

 // new Position(<args>) -> Position.create(<args>)
 NewClass {
    Identifier [name: "Position"],
    List<Expression> [id: args],
 } ->
 MethodInvocation {
    List<Tree> { },
    MemberSelect [identifier: "create"] {
       Identifier [name: "Position"]
    List<Expression> [ref: args]

Another argument

 // plus (<expr1>, <expr2>) -> plus (<expr1>, <expr2>, 5)
 MethodInvocation {
    List [size: 0],
    Identifier [name: "plus"],
    List<Expression> [id: args, size: 2]
 } ->
 MethodInvocation {
    List<Tree> [size: 0],
    Identifier [name: "plus"],
    List<Expression> {
       ListItems [ref: args],
       Literal [kind: INT_LITERAL, value: 5]
For more information, go to http://kenai.com/projects/refactoringng. For download, go to http://kenai.com/projects/refactoringng/downloads