01: /**
02: *
03: */package refactor;
04:
05: import org.eclipse.jdt.core.dom.AST;
06: import org.eclipse.jdt.core.dom.ASTVisitor;
07: import org.eclipse.jdt.core.dom.CompilationUnit;
08: import org.eclipse.jdt.core.dom.ImportDeclaration;
09: import org.eclipse.jdt.core.dom.Name;
10:
11: /**
12: * @author sh
13: *
14: */
15: public class PackageFragmentVisitor extends ASTVisitor {
16: private String oldName = null;
17: private String newName = null;
18:
19: /**
20: * Looks for import declarations.
21: * For every occurence matching the renamed type
22: * the import statement would be updated.
23: *
24: * @param node the node to visit
25: */
26: @Override
27: public boolean visit(ImportDeclaration node) {
28: String fqn = node.getName().getFullyQualifiedName();
29: if (fqn.contains(oldName)) {
30: AST ast = node.getAST();
31: Name newName = ast.newName(fqn.replace(this .oldName,
32: this .newName));
33: node.setName(newName);
34: }
35: return super .visit(node);
36: }
37:
38: /**
39: * Starts the process.
40: *
41: * @param unit the AST root node. Bindings have to been resolved.
42: */
43: public void process(CompilationUnit unit, String oldName,
44: String newName) {
45: this.oldName = oldName;
46: this.newName = newName;
47: unit.accept(this);
48: }
49: }
|