Examples of ASTRewrite


Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

            }
        }

        // now add all the new methods and fields to the existing
        // CompilationUnit with a ListRewrite
        ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
        ListRewrite listRewrite = rewrite.getListRewrite(topLevelType,
                TypeDeclaration.BODY_DECLARATIONS_PROPERTY);

        Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
        int i = 0;
        while (astIter.hasNext()) {
            ASTNode node = astIter.next();

            if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
                String name = ((TypeDeclaration) node).getName()
                        .getFullyQualifiedName();
                if (visitor.containsInnerClass(name)) {
                    continue;
                }
            } else if (node instanceof FieldDeclaration) {
                addExistsAnnotations((BodyDeclaration) node,
                        visitor.getFieldAnnotations((FieldDeclaration) node));
            } else if (node instanceof MethodDeclaration) {
                addExistsAnnotations((BodyDeclaration) node,
                        visitor.getMethodAnnotations((MethodDeclaration) node));
            }

            listRewrite.insertAt(node, i++, null);
        }

        textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
        try {
            textEdit.apply(document);
        } catch (BadLocationException e) {
            throw new ShellException(
                    "BadLocationException adding new fields and methods");
View Full Code Here

Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

  public ImportAssertRewriter(String node) {
    m_assert = node;
  }

  public ASTRewrite createRewriter(CompilationUnit astRoot, AST ast) {
    final ASTRewrite result = ASTRewrite.create(astRoot.getAST());
    TestNGVisitor visitor = new TestNGVisitor();
    astRoot.accept(visitor);

    //
    // Add a static import for this method
    //
    ListRewrite lr = result.getListRewrite(astRoot, CompilationUnit.IMPORTS_PROPERTY);
    ImportDeclaration id = ast.newImportDeclaration();
    id.setStatic(true);
    id.setName(ast.newName("org.testng.AssertJUnit." + m_assert));
    lr.insertFirst(id, null);
View Full Code Here

Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

* @author Cedric Beust <cedric@beust.com>
*/
public class PullTestRewriter implements IRewriteProvider {

  public ASTRewrite createRewriter(CompilationUnit astRoot, AST ast) {
    final ASTRewrite result = ASTRewrite.create(astRoot.getAST());
    TestNGVisitor visitor = new TestNGVisitor();
    astRoot.accept(visitor);

    //
    // Remove all the @Test annotations
    //
    for (Annotation a: visitor.getTestMethods().values()) {
      result.remove(a, null);
    }

    //
    // Add @Test at the class level
    //
    MarkerAnnotation test = ast.newMarkerAnnotation();
    test.setTypeName(ast.newName("Test"));
    ListRewrite lr = result.getListRewrite(visitor.getType(), TypeDeclaration.MODIFIERS2_PROPERTY);
    lr.insertFirst(test, null);

    return result;
  }
View Full Code Here

Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

        String node = findAssertInContext(context);
        if (node != null) providers.add(new ImportAssertRewriter(node));
      }

      for (IRewriteProvider provider : providers) {
        ASTRewrite rewriter = provider.createRewriter(m_astRoot, m_ast);
        vResult.add(new JUnitRewriteCorrectionProposal(
            provider.getName(), m_compilationUnit, rewriter, 1));
      }
    }
   
View Full Code Here

Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

      IRewriteProvider[] providers = new IRewriteProvider[] {
          new AnnotationRewriter(),
      };

      for (int i = 0; i < providers.length; i++) {
        ASTRewrite rewriter = providers[i].createRewriter(astRoot, ast);
        vResult.add(new JUnitRewriteCorrectionProposal(
            providers[i].getName(), cu, rewriter, 1));
      }
    }
   
View Full Code Here

Examples of org.eclipse.jdt.core.dom.rewrite.ASTRewrite

    return change;
  }

  protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
//    super.addEdits(document, editRoot);
    ASTRewrite rewrite = m_rewriter;
    if (rewrite != null) {
      try {
        TextEdit edit= rewrite.rewriteAST();
        editRoot.addChild(edit);
      } catch (IllegalArgumentException e) {
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
      }
    }
View Full Code Here

Examples of org.eclipse.php.internal.core.ast.rewrite.ASTRewrite

       
    FunctionDeclaration functionDec = ast.newFunctionDeclaration(ast.newIdentifier(fMethodName), computeArguments(ast), extractedMethodBody, false);
    method.setModifier(fModifierAccessFlag);
    method.setFunction(functionDec);
   
    ASTRewrite rewriter = ASTRewrite.create(ast);
   
    ListRewrite classListRewrite = rewriter.getListRewrite( fCoveringDeclarationFinder.getCoveringClassDeclaration().getBody(), Block.STATEMENTS_PROPERTY);
    VariableBase dispatcher = ast.newVariable(THIS_VARIABLE_NAME);
    FunctionInvocation calledExtractedMethod = ast.newFunctionInvocation(ast.newFunctionName(ast.newIdentifier(fMethodName)), computeParameters(ast));
    MethodInvocation inlineMethodCall = ast.newMethodInvocation(dispatcher, calledExtractedMethod);

    List<List<ASTNode>> Occurences = new ArrayList<List<ASTNode>>();
   
    if(fReplaceDuplicates) {
      for(Match replace : fDuplicates) {
        Occurences.add(Arrays.asList(replace.getNodes()));
      }
    } else {
      Occurences.add(fSelectedNodesFinder.getNodes());
    }
   
    boolean createdMethodBody = false;
   
    TextEditGroup inlineReplacementEditGroup = inlineReplacementEdit;
   
    for(List<ASTNode> selectedNodeOccurence : Occurences) {
   
      // this is also an indicator, whether this loop was already gone through
      if(createdMethodBody) {
        inlineReplacementEditGroup = additionalInlineReplacementEdit;
      }
     
      ASTNode parent = selectedNodeOccurence.get(0).getParent();
     
      inlineMethodCall = ASTNode.copySubtree(ast, inlineMethodCall);
     
      ListRewrite lrw;
           
      if(parent instanceof Block) {
       
        if(!createdMethodBody) {
          extractedMethodBody.statements().addAll(ASTNode.copySubtrees(ast, selectedNodeOccurence));
          addReturnStatement(ast, extractedMethodBody, fReturnStatement);
          createdMethodBody = true;
        }
       
        lrw = rewriter.getListRewrite(parent, Block.STATEMENTS_PROPERTY);
       
        ExpressionStatement inlineReplacement;
        if (fReturnStatement != null) {
          inlineReplacement = ast.newExpressionStatement(ast.newAssignment(
              ast.newVariable(fReturnStatement.getParameterName()),
              Assignment.OP_EQUAL, inlineMethodCall));
        } else {
          inlineReplacement = ast.newExpressionStatement(inlineMethodCall);
        }
       
        lrw.replace(selectedNodeOccurence.get(0),inlineReplacement, inlineReplacementEditGroup);
 
        for (int i = 1; i < selectedNodeOccurence.size(); ++i) {
          lrw.remove(selectedNodeOccurence.get(i), inlineReplacementEditGroup);
        }
       
      } else {
        if(!createdMethodBody) {
          addReturnStatement(ast, extractedMethodBody, ASTNode.copySubtree(ast, selectedNodeOccurence.get(0)));
          createdMethodBody = true;
        }
        rewriter.replace( selectedNodeOccurence.get(0), inlineMethodCall, inlineReplacementEditGroup);
      }
    }
   

    classListRewrite.insertAfter(method, fCoveringDeclarationFinder.getCoveringMethodDeclaration(), newMethodEdit);
   
    TextEdit fullDocumentEdit = rewriter.rewriteAST(document, null);

    anotherChange.addEdit(fullDocumentEdit);
   
    return anotherChange;
  }
View Full Code Here

Examples of org.eclipse.php.internal.core.ast.rewrite.ASTRewrite

 
    ExpressionStatement statement = getStatement(context);
    Expression expression = statement.getExpression();
   
    AST ast = statement.getAST();
    ASTRewrite astRewrite = ASTRewrite.create(ast);
    String[] names = possibleNames(expression);
    for (int i = 0; i < names.length; i++) {
      linkedModel.getPositionGroup(KEY_NAME, true).addProposal(names[0], null, 10);
    }
    Variable variable = ast.newVariable(names[0]);
   
    Assignment assign = ast.newAssignment(variable, Assignment.OP_EQUAL, (Expression) astRewrite.createCopyTarget(expression));
    astRewrite.replace(expression, assign, editGroup);

    linkedModel.getPositionGroup(KEY_NAME, true).addPosition(astRewrite.track(variable.getName()), true);
    linkedModel.setEndPosition(astRewrite.track(statement));
   
    return astRewrite;
  }
View Full Code Here

Examples of org.eclipse.php.internal.core.ast.rewrite.ASTRewrite

    initialize(str);

    List<Block> blocks = getAllOfType(program, Block.class);
    Assert.assertTrue("Unexpected list size.", blocks.size() == 2);
    AST astRoot = program.getAST();
    ASTRewrite rewrite = ASTRewrite.create(astRoot);
    Block block = blocks.get(0);
    ListRewrite listRewrite = rewrite.getListRewrite(block,
        Block.STATEMENTS_PROPERTY);
    ASTNode placeHolder = rewrite.createStringPlaceholder("//mycomment",
        ASTNode.COMMENT);
    listRewrite.insertFirst(placeHolder, null);

    TextEdit textEdits = rewrite.rewriteAST(document, null);
    textEdits.apply(document);
    checkResult("<?php\n class A { \n  //mycomment\n  public function foo(int $a){}\n }?> ");
  }
View Full Code Here

Examples of org.eclipse.php.internal.core.ast.rewrite.ASTRewrite

  public void testException() throws Exception {
    String str = "<?php\n function A() { throw new Exception(); }";
    initialize(str);
    List<ThrowStatement> allthrows = getAllOfType(program,
        ThrowStatement.class);
    ASTRewrite rewrite = ASTRewrite.create(program.getAST());

    rewrite.replace(
        allthrows.get(0),
        rewrite.createGroupNode(new ASTNode[] { allthrows.get(0),
            program.getAST().newBreakStatement() }), null);
    rewrite.rewriteAST(document, null).apply(document);

    checkResult("<?php\n function A() { throw new Exception(); break; }");

  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.