TypeVisitor.java :  » Workflow-Engines » osbl-1_0 » core » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » core » TypeVisitor.java
/**
 * 
 */
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);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.