Examples of ASTRewrite


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

            ICompilationUnit cu = newFrag.getCompilationUnit(resourceName);
            if (Util.isExcluded(cu.getPath(), inclusionPatterns, exclusionPatterns, false/*not a folder*/)) continue;
            this.parser.setSource(cu);
            CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
            AST ast = astCU.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            updatePackageStatement(astCU, newFragName, rewrite, cu);
            TextEdit edits = rewrite.rewriteAST();
            applyTextEdit(cu, edits);
            cu.save(null, false);
          }
        }
      }
View Full Code Here

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

      // ensure cu is consistent (noop if already consistent)
      cu.makeConsistent(this.progressMonitor);
      this.parser.setSource(cu);
      CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
      AST ast = astCU.getAST();
      ASTRewrite rewrite = ASTRewrite.create(ast);
      updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
      updatePackageStatement(astCU, destPackageName, rewrite, cu);
      return rewrite.rewriteAST();
    }
  }
View Full Code Here

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

      // Modify the newly created enum type.
      final IType newEnumType = page.getCreatedType();
      final CompilationUnit node = (CompilationUnit) Util.getASTNode(
          newEnumType, monitor);

      final ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
      final ImportRewrite importRewrite = ImportRewrite
          .create(node, true);

      final EnumDeclaration oldEnumDeclaration = (EnumDeclaration) node
          .types().get(0);

      // Add imports for annotations to the enum constants.
      for (final Iterator eit = newEnumDeclaration.enumConstants()
          .iterator(); eit.hasNext();) {
        final Object obj = eit.next();
        final EnumConstantDeclaration ecd = (EnumConstantDeclaration) obj;
        for (final Iterator emit = ecd.modifiers().iterator(); emit
            .hasNext();) {
          final Object o = emit.next();
          if (o instanceof Annotation) {
            final Annotation anno = (Annotation) o;
            final String newName = importRewrite
                .addImport((String) annotationToQualifiedNameMap
                    .get(anno));
            anno.setTypeName(ast.newName(newName));
          }
        }
      }
      /*
       * TODO: Need to remove resulting unused imports, but I am unsure of
       * how to do that.
       */

      astRewrite.replace(oldEnumDeclaration, newEnumDeclaration, null);
      this.rewriteAST(newEnumType.getCompilationUnit(), astRewrite,
          importRewrite);
    }

    return status;
View Full Code Here

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

  }

  protected void rewriteCompilationUnit(ICompilationUnit unit,
      Collection matches, CompilationUnit node, RefactoringStatus status,
      IProgressMonitor monitor) throws CoreException {
    final ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
    final ImportRewrite importRewrite = ImportRewrite.create(node, true);

    for (final Iterator it = matches.iterator(); it.hasNext();) {
      final SearchMatch match = (SearchMatch) it.next();
      if (match.getAccuracy() == SearchMatch.A_ACCURATE
View Full Code Here

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

    else group = model;
    // 2°) Second step, we build a rewriter from the compilation unit
    AST ast = u.getAST();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] {"java", "util", "Set"}));
    ASTRewrite rewriter = ASTRewrite.create(ast);
    // 3°) Third step, we can process the type declaration
    if(t.isInterface()) {
      result = new InterfaceService(t, group, rewriter, u);
    }
    else {
View Full Code Here

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

        }

        Document doc = new Document(originalUnit.getBuffer().getContents());
        CompilationUnit workingUnit = createWorkingCopy(originalUnit);

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

        try {
            repairBug(rewrite, workingUnit, bug);
            rewriteCompilationUnit(rewrite, doc, originalUnit);
View Full Code Here

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

    try {
      beginTask(Messages.operation_sortelements, getMainAmountOfWork());

      ICompilationUnit cu= (ICompilationUnit)this.elementsToProcess[0];
      String content= cu.getBuffer().getContents();
      ASTRewrite rewrite= sortCompilationUnit(unit, group);
      if (rewrite == null) {
        return null;
      }

      Document document= new Document(content);
      return rewrite.rewriteAST(document, cu.getJavaProject().getOptions(true));
    } finally {
      done();
    }
  }
View Full Code Here

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

    parser.setSource(source);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setResolveBindings(false);
    org.eclipse.jdt.core.dom.CompilationUnit ast = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);

    ASTRewrite rewriter= sortCompilationUnit(ast, null);
    if (rewriter == null)
      return document.get();

    TextEdit edits = rewriter.rewriteAST(document, unit.getJavaProject().getOptions(true));

    RangeMarker[] markers = null;
    if (this.positions != null) {
      markers = new RangeMarker[this.positions.length];
      for (int i = 0, max = this.positions.length; i < max; i++) {
View Full Code Here

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

        }
        return true;
      }
    });

    final ASTRewrite rewriter= ASTRewrite.create(ast.getAST());
    final boolean[] hasChanges= new boolean[] {false};

    ast.accept(new ASTVisitor() {

      private void sortElements(List elements, ListRewrite listRewrite) {
        if (elements.size() == 0)
          return;

        final List myCopy = new ArrayList();
        myCopy.addAll(elements);
        Collections.sort(myCopy, SortElementsOperation.this.comparator);

        for (int i = 0; i < elements.size(); i++) {
          ASTNode oldNode= (ASTNode) elements.get(i);
          ASTNode newNode= (ASTNode) myCopy.get(i);
          if (oldNode != newNode) {
            listRewrite.replace(oldNode, rewriter.createMoveTarget(newNode), group);
            hasChanges[0]= true;
          }
        }
      }

      public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) {
        if (checkMalformedNodes(compilationUnit)) {
          return true; // abort sorting of current element
        }

        sortElements(compilationUnit.types(), rewriter.getListRewrite(compilationUnit, org.eclipse.jdt.core.dom.CompilationUnit.TYPES_PROPERTY));
        return true;
      }

      public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) {
        if (checkMalformedNodes(annotationTypeDeclaration)) {
          return true; // abort sorting of current element
        }

        sortElements(annotationTypeDeclaration.bodyDeclarations(), rewriter.getListRewrite(annotationTypeDeclaration, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY));
        return true;
      }

      public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) {
        if (checkMalformedNodes(anonymousClassDeclaration)) {
          return true; // abort sorting of current element
        }

        sortElements(anonymousClassDeclaration.bodyDeclarations(), rewriter.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY));
        return true;
      }

      public boolean visit(TypeDeclaration typeDeclaration) {
        if (checkMalformedNodes(typeDeclaration)) {
          return true; // abort sorting of current element
        }

        sortElements(typeDeclaration.bodyDeclarations(), rewriter.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY));
        return true;
      }

      public boolean visit(EnumDeclaration enumDeclaration) {
        if (checkMalformedNodes(enumDeclaration)) {
          return true; // abort sorting of current element
        }

        sortElements(enumDeclaration.bodyDeclarations(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.BODY_DECLARATIONS_PROPERTY));
        sortElements(enumDeclaration.enumConstants(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.ENUM_CONSTANTS_PROPERTY));
        return true;
      }
    });

    if (!hasChanges[0])
View Full Code Here

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

            ICompilationUnit cu = newFrag.getCompilationUnit(resourceName);
            if (Util.isExcluded(cu.getPath(), inclusionPatterns, exclusionPatterns, false/*not a folder*/)) continue;
            this.parser.setSource(cu);
            CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
            AST ast = astCU.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            updatePackageStatement(astCU, newFragName, rewrite, cu);
            TextEdit edits = rewrite.rewriteAST();
            applyTextEdit(cu, edits);
            cu.save(null, false);
          }
        }
      }
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.