コード例 #1
0
 /*
  * @see ASTVisitor#visit(FunctionDeclaration)
  */
 public boolean visit(FunctionDeclaration node) {
   if (node.getJavadoc() != null) {
     node.getJavadoc().accept(this);
   }
   if (node.getAST().apiLevel() == AST.JLS2) {
     printModifiers(node.getModifiers());
   }
   if (node.getAST().apiLevel() >= AST.JLS3) {
     printModifiers(node.modifiers());
   }
   //		if (!node.isConstructor()) {
   //			if (node.getAST().apiLevel() == AST.JLS2) {
   //				node.getReturnType().accept(this);
   //			} else {
   //				if (node.getReturnType2() != null) {
   //					node.getReturnType2().accept(this);
   //				} else {
   //					// methods really ought to have a return type
   //					this.fBuffer.append("void");//$NON-NLS-1$
   //				}
   //			}
   //			this.fBuffer.append(" ");//$NON-NLS-1$
   //		}
   if (node.getName() != null) node.getName().accept(this);
   this.fBuffer.append("("); // $NON-NLS-1$
   for (Iterator it = node.parameters().iterator(); it.hasNext(); ) {
     SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
     v.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(")"); // $NON-NLS-1$
   for (int i = 0; i < node.getExtraDimensions(); i++) {
     this.fBuffer.append("[]"); // $NON-NLS-1$
   }
   if (!node.thrownExceptions().isEmpty()) {
     this.fBuffer.append(" throws "); // $NON-NLS-1$
     for (Iterator it = node.thrownExceptions().iterator(); it.hasNext(); ) {
       Name n = (Name) it.next();
       n.accept(this);
       if (it.hasNext()) {
         this.fBuffer.append(", "); // $NON-NLS-1$
       }
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   if (node.getBody() == null) {
     this.fBuffer.append(";"); // $NON-NLS-1$
   } else {
     node.getBody().accept(this);
   }
   return false;
 }
コード例 #2
0
  /** @deprecated using deprecated code */
  public void testMethodParamsRenameReorder() throws Exception {
    if (true) return;
    IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("    function m( y,  a) {\n");
    buf.append("        m(y, a);\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, "m");
    Block block = methodDecl.getBody();
    List statements = block.statements();
    assertTrue("Number of statements not 1", statements.size() == 1);
    {
      // params
      List params = methodDecl.parameters();
      SingleVariableDeclaration firstParam = (SingleVariableDeclaration) params.get(0);
      SingleVariableDeclaration secondParam = (SingleVariableDeclaration) params.get(1);

      // args
      ExpressionStatement stmt = (ExpressionStatement) statements.get(0);
      FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression();
      List arguments = invocation.arguments();
      SimpleName first = (SimpleName) arguments.get(0);
      SimpleName second = (SimpleName) arguments.get(1);

      // rename args
      SimpleName newFirstArg = methodDecl.getAST().newSimpleName("yyy");
      SimpleName newSecondArg = methodDecl.getAST().newSimpleName("bb");
      rewrite.replace(first, newFirstArg, null);
      rewrite.replace(second, newSecondArg, null);

      // rename params
      SimpleName newFirstName = methodDecl.getAST().newSimpleName("yyy");
      SimpleName newSecondName = methodDecl.getAST().newSimpleName("bb");
      rewrite.replace(firstParam.getName(), newFirstName, null);
      rewrite.replace(secondParam.getName(), newSecondName, null);

      // reoder params
      ASTNode paramplaceholder1 = rewrite.createCopyTarget(firstParam);
      ASTNode paramplaceholder2 = rewrite.createCopyTarget(secondParam);

      rewrite.replace(firstParam, paramplaceholder2, null);
      rewrite.replace(secondParam, paramplaceholder1, null);

      // reorder args
      ASTNode placeholder1 = rewrite.createCopyTarget(first);
      ASTNode placeholder2 = rewrite.createCopyTarget(second);

      rewrite.replace(first, placeholder2, null);
      rewrite.replace(second, placeholder1, null);
    }

    String preview = evaluateRewrite(cu, rewrite);

    buf = new StringBuffer();
    buf.append("    function m(bb, yyy) {\n");
    buf.append("        m(bb, yyy);\n");
    buf.append("    }\n");
    assertEqualString(preview, buf.toString());
  }