コード例 #1
0
  private void rewriteImports() {
    if (fImportRewrite == null) return;

    if (isReadOnly()) return;

    ICompilationUnit cu = getCompilationUnit();
    if (cu == null) return;

    try {
      Position position = new Position(getCompletionOffset(), 0);
      IDocument document = getDocument();
      final String category =
          "__template_position_importer" + System.currentTimeMillis(); // $NON-NLS-1$
      IPositionUpdater updater = new DefaultPositionUpdater(category);
      document.addPositionCategory(category);
      document.addPositionUpdater(updater);
      document.addPosition(position);

      try {
        JavaModelUtil.applyEdit(cu, fImportRewrite.rewriteImports(null), false, null);

        setCompletionOffset(position.getOffset());
      } catch (CoreException e) {
        handleException(null, e);
      } finally {
        document.removePosition(position);
        document.removePositionUpdater(updater);
        document.removePositionCategory(category);
      }
    } catch (BadLocationException e) {
      handleException(null, e);
    } catch (BadPositionCategoryException e) {
      handleException(null, e);
    }
  }
コード例 #2
0
  @Override
  public void run(IProgressMonitor monitor) throws CoreException {
    if (monitor == null) monitor = new NullProgressMonitor();
    try {
      monitor.beginTask("", 1); // $NON-NLS-1$
      monitor.setTaskName(CodeGenerationMessages.GenerateToStringOperation_description);

      AbstractTypeDeclaration declaration =
          (AbstractTypeDeclaration)
              ASTNodes.findDeclaration(fContext.getTypeBinding(), fRewrite.getRoot());
      ListRewrite rewriter =
          fRewrite
              .getASTRewrite()
              .getListRewrite(declaration, declaration.getBodyDeclarationsProperty());
      if (fContext.getTypeBinding() != null && rewriter != null) {

        MethodDeclaration toStringMethod = fGenerator.generateToStringMethod();

        List<BodyDeclaration> list = declaration.bodyDeclarations();
        BodyDeclaration replace = findMethodToReplace(list, toStringMethod);
        if (replace == null
            || ((Boolean)
                    toStringMethod.getProperty(AbstractToStringGenerator.OVERWRITE_METHOD_PROPERTY))
                .booleanValue()) insertMethod(toStringMethod, rewriter, replace);

        List<MethodDeclaration> helperMethods = fGenerator.generateHelperMethods();
        for (Iterator<MethodDeclaration> iterator = helperMethods.iterator();
            iterator.hasNext(); ) {
          MethodDeclaration method = iterator.next();
          replace = findMethodToReplace(list, method);
          if (replace == null
              || ((Boolean) method.getProperty(AbstractToStringGenerator.OVERWRITE_METHOD_PROPERTY))
                  .booleanValue()) {
            insertMethod(method, rewriter, replace);
          }
        }

        JavaModelUtil.applyEdit(
            (ICompilationUnit) fUnit.getJavaElement(),
            fRewrite.createChange(true).getEdit(),
            false,
            monitor);
      }

    } finally {
      monitor.done();
    }
  }