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

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


      CompilationUnit unit = getRecoveredAST(document, offset, recoveredDocument);
      initContext(offset, importRw, unit);

      ASTNode node = NodeFinder.perform(unit, offset, 1);
      AST ast = unit.getAST();
      ASTRewrite rewrite = ASTRewrite.create(ast);

      CodeGenerationSettings settings = getCodeGenSettings();

      MethodDeclaration stub = createMockMethodStub(ast, rewrite, settings);
View Full Code Here


      TypeDeclaration dropTypeDec = (TypeDeclaration) ASTNode
          .copySubtree(astRoot.getAST(), typeDeclaration);

      // creation of ASTRewrite
      AST ast = astRoot.getAST();
      ASTRewrite rewrite = ASTRewrite.create(ast);

      // description of the change
      for (Object typeObj : astRoot.types()) {
        if (typeObj instanceof TypeDeclaration) {
          TypeDeclaration typeDec = (TypeDeclaration) typeObj;
          if (typeDec.getName().toString()
              .equals(dropTypeDec.getName().toString())) {
            logger.debug("Replace existing type declaration");
            if (Activator
                .getDefault()
                .getPreferenceStore()
                .getString(
                    PreferenceConstants.P_OVERWRITE_ON_INSERT)
                .equals("true")) {
              rewrite.remove(typeDec, null);
            }
          }
        }
      }

      ASTNode a = ASTNode.copySubtree(ast, dropTypeDec);
      @SuppressWarnings("unchecked")
      List<TypeDeclaration> types = astRoot.types();
      ArrayList<TypeDeclaration> newTypes = new ArrayList<TypeDeclaration>();
      newTypes.add((TypeDeclaration) a);
      if (types.addAll(newTypes)) {
        if (!target.getElementName().equals(
            dropTypeDec.getName().toString() + ".java")) {
          List<?> modifiers = dropTypeDec.modifiers();
          for (Object mod : modifiers) {
            if (mod instanceof Modifier) {
              Modifier modifier = (Modifier) mod;
              if (modifier.getKeyword().toString()
                  .equals("public")) {
                dropTypeDec.modifiers().remove(modifier);
                break;
              }
            }
          }
        }
        String preambule = createPreambule(typeDeclaration) + "\r\n";
        icu.createType(preambule + dropTypeDec.toString(), null, true,
            null);
        logger.debug("New type added successfully");
      }

      String source = icu.getSource();
      Document document = new Document(source);

      // computation of the text edits
      TextEdit edits = rewrite.rewriteAST(document, icu.getJavaProject()
          .getOptions(true));

      // computation of the new source code
      if (edits != null) {
        edits.apply(document);
View Full Code Here

            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

      // 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

      // 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

  }

  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

    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

        }

        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

    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

    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

TOP

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

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.