/** @deprecated using deprecated code */
  public void testFieldAccess() throws Exception {
    IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("    function foo() {\n");
    buf.append("        foo().i= goo().i;\n");
    buf.append("    }\n");
    IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null);

    JavaScriptUnit astRoot = createAST(cu);
    ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());

    AST ast = astRoot.getAST();

    assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
    //		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
    FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo");
    Block block = methodDecl.getBody();
    List statements = block.statements();
    assertTrue("Number of statements not 1", statements.size() == 1);
    { // replace field expression, replace field name
      ExpressionStatement stmt = (ExpressionStatement) statements.get(0);
      Assignment assignment = (Assignment) stmt.getExpression();
      FieldAccess leftFieldAccess = (FieldAccess) assignment.getLeftHandSide();
      FieldAccess rightFieldAccess = (FieldAccess) assignment.getRightHandSide();

      FunctionInvocation invocation = ast.newFunctionInvocation();
      invocation.setName(ast.newSimpleName("xoo"));
      rewrite.replace(leftFieldAccess.getExpression(), invocation, null);

      SimpleName newName = ast.newSimpleName("x");
      rewrite.replace(leftFieldAccess.getName(), newName, null);

      SimpleName rightHand = ast.newSimpleName("b");
      rewrite.replace(rightFieldAccess.getExpression(), rightHand, null);
    }

    String preview = evaluateRewrite(cu, rewrite);

    buf = new StringBuffer();
    buf.append("    function foo() {\n");
    buf.append("        xoo().x= b.i;\n");
    buf.append("    }\n");
    assertEqualString(preview, buf.toString());
  }
Ejemplo n.º 2
0
 /*
  * @see ASTVisitor#visit(FieldAccess)
  */
 public boolean visit(FieldAccess node) {
   node.getExpression().accept(this);
   this.fBuffer.append("."); // $NON-NLS-1$
   node.getName().accept(this);
   return false;
 }