Package org.eclipse.jdt.core.dom.rewrite

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


        .getVariableDeclarationFragment();
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment
        .getParent();
    // add a remove command to the protocol
    rewrite.remove(fragment, null);
    ListRewrite fragmentsListRewrite = rewrite.getListRewrite(statement,
        VariableDeclarationStatement.FRAGMENTS_PROPERTY);
    if (fragmentsListRewrite.getRewrittenList().size() == 0) {
      // add a remove command to the protocol
      rewrite.remove(statement, null);
    }
  }
View Full Code Here


    VariableDeclarationStatement statement = createNewVariableDeclarationStatement(
        manager, ast);
    int firstReferenceIndex = getFirstReferenceListIndex(manager);
    Block block = Helper.getParentBlock(manager.getFirstReference());
    // get the list rewriter for the statments list
    ListRewrite statementsListRewrite = rewrite.getListRewrite(block,
        Block.STATEMENTS_PROPERTY);
    // add insert-at command to the protocol
    statementsListRewrite.insertAt(statement, firstReferenceIndex, null);
  }
View Full Code Here

    for(Object o:cu.types()){
      AbstractTypeDeclaration dec = (AbstractTypeDeclaration) o;
      List bodyDeclarations = dec.bodyDeclarations();
      for(Object body:bodyDeclarations){
        MethodDeclaration methodDec = (MethodDeclaration) body;
        ListRewrite listRewrite = rewriter.getListRewrite(methodDec.getJavadoc(), Javadoc.TAGS_PROPERTY);
        List originalList = listRewrite.getOriginalList();
        ASTNode node = createNode(cu.getAST());
        listRewrite.insertLast(node, null);
        System.out.println(originalList);
        System.out.println(listRewrite.getRewrittenList());
      }
    }
    TextEdit rewriteAst = rewriter.rewriteAST(doc, null);
    rewriteAst.apply(doc);
    System.out.println(doc.get());
View Full Code Here

 
    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());

    ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();

    ListRewrite listRewrite = rewrite.getListRewrite(declaration, modifiersProperty);
    listRewrite.insertFirst(annotation, null);
   
    TextEdit importEdits = null;
    if (importDeclaration != null) {
      ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
      importRewrite.addImport(importDeclaration.getName().getFullyQualifiedName());
View Full Code Here

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);
 
    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());

    ListRewrite listRewrite = rewrite.getListRewrite(declaration, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
    listRewrite.insertFirst(annotation, null);
   
    TextEdit importEdits = null;
    if (importDeclaration != null) {
      ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
      importRewrite.addImport(importDeclaration.getName().getFullyQualifiedName());
View Full Code Here

   
    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());
   
    ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();
   
    ListRewrite listRewrite = rewrite.getListRewrite(declaration, modifiersProperty);
    listRewrite.insertFirst(normalAnnotation, null);

    maintainValuesProperty(normalAnnotation, memberValues, ast, rewrite);

    TextEdit importEdits = null;
    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
View Full Code Here

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);
   
    ASTRewrite rewrite = ASTRewrite.create(declaration.getAST());
   
    ListRewrite listRewrite = rewrite.getListRewrite(declaration, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
    listRewrite.insertFirst(normalAnnotation, null);

    maintainValuesProperty(normalAnnotation, memberValues, ast, rewrite);

    TextEdit importEdits = null;
    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
View Full Code Here

   * @param rewrite
   */
  private void maintainValuesProperty(
      NormalAnnotation normalAnnotation, Map<String, Object> newMemberValues,
      AST ast, ASTRewrite rewrite) {
    ListRewrite listRewrite = rewrite.getListRewrite(normalAnnotation, NormalAnnotation.VALUES_PROPERTY);
        LinkedHashMap<String, Object> existingMemberValues = ASTTools.memberValues(normalAnnotation);
       
        Set<String> existingKeySet = new HashSet<String>(existingMemberValues.keySet());
        Set<String> newKeySet = new HashSet<String>(newMemberValues.keySet());
       
        Set<String> commonMembers = new HashSet<String>(existingKeySet);
        commonMembers.retainAll(newKeySet);
       
        Set<String> newMembers = new HashSet<String>(newKeySet);
        newMembers.removeAll(existingKeySet);
       
        Set<String> removedMembers = new HashSet<String>(existingKeySet);
        removedMembers.removeAll(newKeySet);
       
    for(String memberName: commonMembers) {
      Object value = newMemberValues.get(memberName);
      MemberValuePair memberValuePair =
        ASTTools.memberValuePair(normalAnnotation, memberName);
      if (value != null) {
        MemberValuePair newValuePair = createMemberValuePair(ast, memberName, value);
        listRewrite.replace(memberValuePair, newValuePair, null);
      } else {
        listRewrite.remove(memberValuePair, null);
      }
    }

        for(String memberName: newMembers) {
            Object value = newMemberValues.get(memberName);
            if (value != null) {
                MemberValuePair newValuePair = createMemberValuePair(ast, memberName, value);
                // TODO: should attempt to preserve order
                listRewrite.insertLast(newValuePair, null);
            } else {
                // nothing to do
            }
        }

        for(String memberName: removedMembers) {
            MemberValuePair memberValuePair =
                ASTTools.memberValuePair(normalAnnotation, memberName);
            listRewrite.remove(memberValuePair, null);
        }

        // TODO: earlier implementation; to remove
//        for(String memberName: commonMembers) {
//            Object value = newMemberValues.get(memberName);
View Full Code Here

        AST ast = cu.getAST();
        ASTRewrite astRewrite = ASTRewrite.create(ast);
        ASTNode node = cu.findDeclaringNode(typeBinding);
        ChildListPropertyDescriptor property = ((AbstractTypeDeclaration)node)
          .getBodyDeclarationsProperty();
        ListRewrite memberRewriter = astRewrite.getListRewrite(node, property);
        HashSet<String> added = new HashSet<String>();
        for (IMethodBinding binding : methodBindings){
          String name = binding.getName();
          if (added.contains(name)){
            continue;
          }
          added.add(name);

          MethodDeclaration stub = ast.newMethodDeclaration();
          stub.setConstructor(false);
          stub.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));

          Annotation marker = ast.newMarkerAnnotation();
          marker.setTypeName(ast.newSimpleName("Test"));
          astRewrite
            .getListRewrite(stub, MethodDeclaration.MODIFIERS2_PROPERTY)
            .insertFirst(marker, null);

          stub.setName(ast.newSimpleName(name));

          Block body = ast.newBlock();
          stub.setBody(body);

          String todoTask = "";
          String todoTaskTag = JUnitStubUtility.getTodoTaskTag(javaProject);
          if (todoTaskTag != null) {
            todoTask = " // " + todoTaskTag;
          }
          String message = WizardMessages
            .NewTestCaseWizardPageOne_not_yet_implemented_string;
          body.statements().add(astRewrite.createStringPlaceholder(
                todoTask,
                ASTNode.RETURN_STATEMENT));
          body.statements().add(astRewrite.createStringPlaceholder(
                Messages.format("fail(\"{0}\");", message),
                ASTNode.RETURN_STATEMENT));

          memberRewriter.insertLast(stub, null);
        }
        edit.addChild(astRewrite.rewriteAST());

        JavaModelUtil.applyEdit(src, edit, true, null);
      }catch(Exception e){
View Full Code Here

   */
  protected void insertASTNode(ASTRewrite rewriter, ASTNode parent, ASTNode child) throws JavaModelException {
    StructuralPropertyDescriptor propertyDescriptor = getChildPropertyDescriptor(parent);
    if (propertyDescriptor instanceof ChildListPropertyDescriptor) {
      ChildListPropertyDescriptor childListPropertyDescriptor = (ChildListPropertyDescriptor) propertyDescriptor;
       ListRewrite rewrite = rewriter.getListRewrite(parent, childListPropertyDescriptor);
       switch (this.insertionPolicy) {
         case INSERT_BEFORE:
           ASTNode element = ((JavaElement) this.anchorElement).findNode(this.cuAST);
           if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass()))
             rewrite.insertBefore(child, element, null);
           else
             // case of an empty import list: the anchor element is the top level type and cannot be used in insertBefore as it is not the same type
             rewrite.insertLast(child, null);
           break;
         case INSERT_AFTER:
           element = ((JavaElement) this.anchorElement).findNode(this.cuAST);
           if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass()))
             rewrite.insertAfter(child, element, null);
           else
             // case of an empty import list: the anchor element is the top level type and cannot be used in insertAfter as it is not the same type
             rewrite.insertLast(child, null);
           break;
         case INSERT_LAST:
           rewrite.insertLast(child, null);
           break;
       }
    } else {
      rewriter.set(parent, propertyDescriptor, child, null);
    }
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.dom.rewrite.ListRewrite

Copyright © 2018 www.massapicom. 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.