Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

RefactoringNG: Bulk Refactoring in Java

DZone's Guide to

RefactoringNG: Bulk Refactoring in Java

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

RefactoringNG is a NetBeans plugin for Java source code refactoring. Rather than a limited set of refactoring rules, we can define our own with RefactoringNG. The tool works with abstract syntax trees which are built in the process of source code compilation. So, we have available information from the syntax and semantic analysis which makes the tool superior to text-level oriented tools. Furthermore, we can create batches which can be performed later. Refactoring batches can be used for automatic code upgrade when a new version of library is released.

A refactoring rule is defined by a source abstract syntax tree (what we want to look for) and destination abstract syntax tree (what we want the original tree to transform to). For example, the rule that replaces p.enable() with p.setEnabled(true) where p is a variable of the Window type or any subclass, is as follows:

// p.enable() -> p.setEnabled(true)
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "enable"] {
Identifier [id: p, instanceof: "windows.Window"]
  },
List<Expression> { }
} ->
MethodInvocation {
List<Tree> { },
  MemberSelect [identifier: "setEnabled"] {
Identifier [ref: p]
  },
  List<Expression> {
  Literal [value: true]
  }
}

When we encapsulate a variable, we need to change all the variable references to getter calls. In RefactoringNG, this can be done by the following rule:

// s.color -> s.getColor()
MemberSelect [identifier: "color"] {
Identifier [id: s, instanceof: "windows.Screen"]
} ->
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "getColor"] {
  Identifier [ref: s]
},
List<Expression> { }
}

Have you ever needed to use a factory method instead of a contructor? With RefactoringNG, this is a simple transition from a constructor to a factory method:

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

For more information, see http://kenai.com/projects/refactoringng/pages/Home.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}