001: /**
002: *
003: */package refactor;
004:
005: import org.eclipse.jdt.core.dom.AST;
006: import org.eclipse.jdt.core.dom.ASTVisitor;
007: import org.eclipse.jdt.core.dom.CompilationUnit;
008: import org.eclipse.jdt.core.dom.ImportDeclaration;
009: import org.eclipse.jdt.core.dom.Name;
010: import org.eclipse.jdt.core.dom.SimpleType;
011:
012: /**
013: * @author sh
014: *
015: */
016: public class CompilationUnitVisitor extends ASTVisitor {
017: private String oldName = null;
018: private String newName = null;
019:
020: /**
021: * Looks for variable declarations in parameter lists.
022: * For every occurence of a variable declaration mathing the
023: * the renamed type the type name would be updated.
024: *
025: * @param node the node to visit
026: */
027: /*
028: @Override
029: public boolean visit(SingleVariableDeclaration node) {
030: if (node.getType().toString().equals(this.oldName)) {
031: AST ast = node.getAST();
032: node.setType(ast.newSimpleType(ast.newName(this.newName)));
033: }
034:
035: return super.visit(node);
036: }
037: */
038:
039: /**
040: * Looks for method declarations.
041: * For every occurence of a methods type declaration
042: * matching the renamed type the return type would be updated.
043: *
044: * @param node the node to visit
045: */
046: /*
047: @Override
048: public boolean visit(MethodDeclaration node) {
049: if (node.getName().toString().equals(this.oldName))
050: node.setName(node.getAST().newSimpleName(this.newName));
051:
052: return super.visit(node);
053: }
054: */
055:
056: /**
057: * Looks for field declarations.
058: * For every occurence of a fields type declaration
059: * matching the renamed type the type would be updated.
060: *
061: * @param node the node to visit
062: */
063: /*
064: @Override
065: public boolean visit(FieldDeclaration node) {
066: if (node.getType().toString().equals(this.oldName)) {
067: AST ast = node.getAST();
068: node.setType(ast.newSimpleType(ast.newName(this.newName)));
069: }
070:
071: return super.visit(node);
072: }
073: */
074:
075: /**
076: * Looks for import declarations.
077: * For every occurence matching the renamed type
078: * the import statement would be updated.
079: *
080: * @param node the node to visit
081: */
082: @Override
083: public boolean visit(ImportDeclaration node) {
084: String fqn = node.getName().getFullyQualifiedName();
085: if (fqn.contains(oldName)) {
086: AST ast = node.getAST();
087: Name newName = ast.newName(fqn.replace(this .oldName,
088: this .newName));
089: node.setName(newName);
090: }
091: return super .visit(node);
092: }
093:
094: /**
095: * Looks for simple types.
096: * For every occurence mathing the renamed type
097: * the type would be updated.
098: *
099: * @param node the node to visit
100: */
101: @Override
102: public boolean visit(SimpleType node) {
103: if (node.toString().equals(this .oldName))
104: node.setName(node.getAST().newName(this .newName));
105:
106: return super .visit(node);
107: }
108:
109: /**
110: * Starts the process.
111: *
112: * @param unit the AST root node. Bindings have to been resolved.
113: */
114: public void process(CompilationUnit unit, String oldName,
115: String newName) {
116: this.oldName = oldName;
117: this.newName = newName;
118: unit.accept(this);
119: }
120: }
|