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

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}