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

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


    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);

    AST ast = normalAnnotation.getAST();

    ASTRewrite rewrite = ASTRewrite.create(ast);

    maintainValuesProperty(normalAnnotation, memberValues, ast, rewrite);
   
    Map options = compilationUnit.getJavaProject().getOptions(true);
    TextEdit edits = rewrite.rewriteAST(document, options);
   
    // computation of the new source code
    edits.apply(document);
    String newSource = document.get();
   
View Full Code Here


    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(compilationUnit);

    AST ast = normalAnnotation.getAST();

    ASTRewrite rewrite = ASTRewrite.create(ast);

    maintainValuesProperty(normalAnnotation, memberValues, ast, rewrite);
   
    Map options = compilationUnit.getJavaProject().getOptions(true);
    TextEdit edits = rewrite.rewriteAST(document, options);
   
    // computation of the new source code
    edits.apply(document);
    String newSource = document.get();
   
View Full Code Here

        imports.addStaticImport("org.junit.Assert", "*", false);
        imports.addImport(JUnitCorePlugin.JUNIT4_ANNOTATION_NAME);
        edit.addChild(imports.rewriteImports(null));

        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){
        throw new RuntimeException(e);
      }
View Full Code Here

    CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
    ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
    if (node == null)
      Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
    AST ast = astCU.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    rewriter.remove(node, null);
     TextEdit edits = rewriter.rewriteAST();
     applyTextEdit(cu, edits);
  }
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

   */
  protected void generateNewCompilationUnitAST(ICompilationUnit cu) throws JavaModelException {
    this.cuAST = parse(cu);

    AST ast = this.cuAST.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    ASTNode child = generateElementAST(rewriter, cu);
    if (child != null) {
      ASTNode parent = ((JavaElement) getParentElement()).findNode(this.cuAST);
      if (parent == null)
        parent = this.cuAST;
      insertASTNode(rewriter, parent, child);
      TextEdit edits = rewriter.rewriteAST();
      applyTextEdit(cu, edits);
    }
    worked(1);
  }
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

        }
        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

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.