/**
*
*/
package core;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.SimpleType;
/**
* @author sh
*
*/
public class TypeVisitor extends ASTVisitor {
// the full qualified name of the old package
private String oldPackage;
// the full qualified name of the new package
private String newPackage;
// the old type name
private String oldType;
// the new type name
private String newType;
/**
* Looks for import declarations.
* For every occurence matching the renamed type
* the import statement will be updated.
*
* @param node the node to visit
*/
@Override
public boolean visit(ImportDeclaration node) {
String fqn = node.getName().getFullyQualifiedName();
// replace old import declaration
if (fqn.toString().equals(this.oldPackage)) {
AST ast = node.getAST();
node.setName(ast.newName(this.newPackage));
}
return super.visit(node);
}
/**
* Looks for simple types.
* For every occurence mathing the renamed type
* the type will be updated.
*
* @param node the node to visit
*/
@Override
public boolean visit(SimpleType node) {
if (node.toString().equals(this.oldType))
node.setName(node.getAST().newName(this.newType));
return super.visit(node);
}
/**
* Starts the process.
*
* @param unit the AST root node. Bindings have to been resolved.
*/
public void process(CompilationUnit unit, ModelInfo info) {
this.oldPackage = info.getOldImplementation();
this.newPackage = info.getNewImplementation();
this.oldType = Util.classname(info.getOldImplementation());
this.newType = Util.classname(info.getNewImplementation());
unit.accept(this);
}
}
|