Package org.eclipse.jdt.core

Examples of org.eclipse.jdt.core.IBuffer


      // Write crosscut and format it
      IField field = fCrosscutPage.writeCrosscut(aspect, fCrosscutTypeDialogField.getSelectionIndex(), imports,
          new SubProgressMonitor(monitor, 2), lineDelimiter);
      ISourceRange range = field.getSourceRange();
      IBuffer buf = cu.getBuffer();
      String originalContent = buf.getText(range.getOffset(), range.getLength());
      String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, 1, null,
          lineDelimiter, field.getJavaProject());
      buf.replace(range.getOffset(), range.getLength(), formattedContent);
      if (!cu.isWorkingCopy())
        buf.save(null, false);

      imports.create(needsSave, new SubProgressMonitor(monitor, 1));
      removeUnusedImports(cu, needsSave);
      if (needsCommit)
        cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
View Full Code Here


            }
            if (source == null) {
                // this could be the case for class files which are not on the class path
                // buffer should be already opened and created in our editor->doOpenBuffer
                // method
                IBuffer buffer = BufferManager.getDefaultBufferManager().getBuffer(classFile);
                if (buffer != null) {
                    source = buffer.getContents();
                }
            }
            document.set(source);
            return true;
        }
View Full Code Here

    }

    private String getAttachedJavaSource(IClassFile cf, boolean force) {
        String origSrc = null;
        if (force) {
            IBuffer buffer = BytecodeBufferManager.getBuffer(cf);
            if (buffer != null) {
                BytecodeBufferManager.removeBuffer(buffer);
            }
        }
        try {
View Full Code Here

        }
        return origSrc;
    }

    private void changeBufferContent(IClassFile cf, char[] src) {
        IBuffer buffer = BytecodeBufferManager.getBuffer(cf);

        // i'm not sure if we need to create buffer each time -
        // couldn't we reuse existing one (if any)?
        // - seems that without "create" some listener didn't get notifications about
        // changed content
        boolean addBuffer = false;

        // if(buffer == null) {
        buffer = BytecodeBufferManager.createBuffer(cf);
        addBuffer = true;
        // }
        if (src == null) {
            src = new char[]{'\n', '/', '/', 'E', 'r', 'r', 'o', 'r'};
        }
        buffer.setContents(src);
        if (addBuffer) {
            BytecodeBufferManager.addBuffer(buffer);
            buffer.addBufferChangedListener((IBufferChangedListener) cf);
        }
    }
View Full Code Here

                needsSave = true;
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now a (primary) working copy
                connectedCU = parentCU;

                IBuffer buffer = parentCU.getBuffer();

                String simpleTypeStub = constructSimpleTypeStub();
                String cuContent = constructCUContent(parentCU, simpleTypeStub, lineDelimiter);
                buffer.setContents(cuContent);

                CompilationUnit astRoot = createASTForImports(parentCU);
                existingImports = getExistingImports(astRoot);

                imports = new ImportsManager(astRoot);
                // add an import that will be removed again. Having this import solves 14661
                imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));

                String typeContent = constructTypeStub(parentCU, imports, lineDelimiter);

                int index = cuContent.lastIndexOf(simpleTypeStub);
                if (index == -1) {
                    AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0);
                    int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition();
                    int end = typeNode.getStartPosition() + typeNode.getLength();
                    buffer.replace(start, end - start, typeContent);
                } else {
                    buffer.replace(index, simpleTypeStub.length(), typeContent);
                }

                createdType = parentCU.getType(typeName);
            } else {
                IType enclosingType = getEnclosingType();

                ICompilationUnit parentCU = enclosingType.getCompilationUnit();

                needsSave = !parentCU.isWorkingCopy();
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now for sure (primary) a working copy
                connectedCU = parentCU;

                CompilationUnit astRoot = createASTForImports(parentCU);
                imports = new ImportsManager(astRoot);
                existingImports = getExistingImports(astRoot);

                // add imports that will be removed again. Having the imports solves 14661
                IType[] topLevelTypes = parentCU.getTypes();
                for (int i = 0; i < topLevelTypes.length; i++) {
                    imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
                }

                lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
                StringBuffer content = new StringBuffer();

                String comment = getTypeComment(parentCU, lineDelimiter);
                if (comment != null) {
                    content.append(comment);
                    content.append(lineDelimiter);
                }

                content.append(constructTypeStub(parentCU, imports, lineDelimiter));
                IJavaElement sibling = null;
                if (enclosingType.isEnum()) {
                    IField[] fields = enclosingType.getFields();
                    if (fields.length > 0) {
                        for (int i = 0, max = fields.length; i < max; i++) {
                            if (!fields[i].isEnumConstant()) {
                                sibling = fields[i];
                                break;
                            }
                        }
                    }
                } else {
                    IJavaElement[] elems = enclosingType.getChildren();
                    sibling = elems.length > 0 ? elems[0] : null;
                }

                createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 2));

                indent = StubUtility.getIndentUsed(enclosingType) + 1;
            }
            if (monitor.isCanceled()) {
                throw new InterruptedException();
            }

            // add imports for superclass/interfaces, so types can be resolved correctly

            ICompilationUnit cu = createdType.getCompilationUnit();

            imports.create(false, new SubProgressMonitor(monitor, 1));

            JavaModelUtil.reconcile(cu);

            if (monitor.isCanceled()) {
                throw new InterruptedException();
            }

            // set up again
            CompilationUnit astRoot = createASTForImports(imports.getCompilationUnit());
            imports = new ImportsManager(astRoot);

            createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1));

            // add imports
            imports.create(false, new SubProgressMonitor(monitor, 1));

            removeUnusedImports(cu, existingImports, false);

            JavaModelUtil.reconcile(cu);

            ISourceRange range = createdType.getSourceRange();

            IBuffer buf = cu.getBuffer();
            String originalContent = buf.getText(range.getOffset(), range.getLength());

            String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, lineDelimiter, pack.getJavaProject());
            formattedContent = Strings.trimLeadingTabsAndSpaces(formattedContent);
            buf.replace(range.getOffset(), range.getLength(), formattedContent);
            if (!isInnerClass) {
                String fileComment = getFileComment(cu);
                if (fileComment != null && fileComment.length() > 0) {
                    buf.replace(0, 0, fileComment + lineDelimiter);
                }
            }
            fCreatedType = createdType;

            if (needsSave) {
View Full Code Here

                needsSave = true;
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now a (primary) working copy
                connectedCU = parentCU;

                IBuffer buffer = parentCU.getBuffer();

                String simpleTypeStub = constructSimpleTypeStub();
                String cuContent = constructCUContent(parentCU, simpleTypeStub, lineDelimiter);
                buffer.setContents(cuContent);

                CompilationUnit astRoot = createASTForImports(parentCU);
                existingImports = getExistingImports(astRoot);

                imports = new ImportsManager(astRoot);
                // add an import that will be removed again. Having this import solves 14661
                imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));

                String typeContent = constructTypeStub(parentCU, imports, lineDelimiter);

                int index = cuContent.lastIndexOf(simpleTypeStub);
                if (index == -1) {
                    AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0);
                    int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition();
                    int end = typeNode.getStartPosition() + typeNode.getLength();
                    buffer.replace(start, end - start, typeContent);
                } else {
                    buffer.replace(index, simpleTypeStub.length(), typeContent);
                }

                createdType = parentCU.getType(typeName);
            } else {
                IType enclosingType = getEnclosingType();

                ICompilationUnit parentCU = enclosingType.getCompilationUnit();

                needsSave = !parentCU.isWorkingCopy();
                parentCU.becomeWorkingCopy(new SubProgressMonitor(monitorInternal, 1)); // cu is now for sure (primary) a working copy
                connectedCU = parentCU;

                CompilationUnit astRoot = createASTForImports(parentCU);
                imports = new ImportsManager(astRoot);
                existingImports = getExistingImports(astRoot);

                // add imports that will be removed again. Having the imports solves 14661
                IType[] topLevelTypes = parentCU.getTypes();
                for (int i = 0; i < topLevelTypes.length; i++) {
                    imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
                }

                lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
                StringBuffer content = new StringBuffer();

                String comment = getTypeComment(parentCU, lineDelimiter);
                if (comment != null) {
                    content.append(comment);
                    content.append(lineDelimiter);
                }

                content.append(constructTypeStub(parentCU, imports, lineDelimiter));
                IJavaElement sibling = null;
                if (enclosingType.isEnum()) {
                    IField[] fields = enclosingType.getFields();
                    if (fields.length > 0) {
                        for (int i = 0, max = fields.length; i < max; i++) {
                            if (!fields[i].isEnumConstant()) {
                                sibling = fields[i];
                                break;
                            }
                        }
                    }
                } else {
                    IJavaElement[] elems = enclosingType.getChildren();
                    sibling = elems.length > 0 ? elems[0] : null;
                }

                createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitorInternal, 2));

                indent = StubUtility.getIndentUsed(enclosingType) + 1;
            }
            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // add imports for superclass/interfaces, so types can be resolved correctly

            ICompilationUnit cu = createdType.getCompilationUnit();

            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            JavaModelUtil.reconcile(cu);

            if (monitorInternal.isCanceled()) {
                throw new InterruptedException();
            }

            // set up again
            CompilationUnit astRoot = createASTForImports(imports.getCompilationUnit());
            imports = new ImportsManager(astRoot);

            createTypeMembers(createdType, imports, new SubProgressMonitor(monitorInternal, 1));

            // add imports
            imports.create(false, new SubProgressMonitor(monitorInternal, 1));

            removeUnusedImports(cu, existingImports, false);

            JavaModelUtil.reconcile(cu);

            ISourceRange range = createdType.getSourceRange();

            IBuffer buf = cu.getBuffer();
            String originalContent = buf.getText(range.getOffset(), range.getLength());

            String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, indent, lineDelimiter, pack.getJavaProject());
            formattedContent = Strings.trimLeadingTabsAndSpaces(formattedContent);
            buf.replace(range.getOffset(), range.getLength(), formattedContent);
            if (!isInnerClass) {
                String fileComment = getFileComment(cu);
                if (fileComment != null && fileComment.length() > 0) {
                    buf.replace(0, 0, fileComment + lineDelimiter);
                }
            }
            fCreatedType = createdType;

            if (needsSave) {
View Full Code Here

    public void inCompilationUnit(ICompilationUnit compilationUnit, int position, int length, String newText) throws JavaModelException {
        try {
            WorkingCopyOwner owner = DefaultWorkingCopyOwner.PRIMARY;
            ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(owner, null);
            IBuffer buffer = workingCopy.getBuffer();
            buffer.replace(position, length, newText);
            workingCopy.reconcile(ICompilationUnit.NO_AST, true, owner, null);
            compilationUnit.commitWorkingCopy(true, null);
        } finally {
            compilationUnit.discardWorkingCopy();
        }
View Full Code Here

public class BuilderGenerator implements Generator {

  public void generate(ICompilationUnit cu, boolean createBuilderConstructor, boolean formatSource, List<IField> fields) {
    try {
      IBuffer buffer = cu.getBuffer();
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      pw.println();
      pw.println("public static class Builder {");

      IType clazz = cu.getTypes()[0];

      int pos = clazz.getSourceRange().getOffset() + clazz.getSourceRange().getLength() - 1;

      createFieldDeclarations(pw, fields);

      createBuilderMethods(pw, fields);
      if (createBuilderConstructor) {
        createPrivateBuilderConstructor(pw, clazz, fields);
        pw.println("}");
      } else {
        createClassBuilderConstructor(pw, clazz, fields);
        pw.println("}");
        createClassConstructor(pw, clazz, fields);
      }
     
      if (formatSource) {
        buffer.replace(pos, 0, sw.toString());
        String builderSource = buffer.getContents();
     
        TextEdit text = ToolFactory.createCodeFormatter(null).format(CodeFormatter.K_COMPILATION_UNIT, builderSource, 0, builderSource.length(), 0, "\n");
        // text is null if source cannot be formatted
        if (text != null) {
          Document simpleDocument = new Document(builderSource);
          text.apply(simpleDocument);
          buffer.setContents(simpleDocument.get());
        }
      } else {
        buffer.replace(pos, 0, sw.toString())
      }
    } catch (JavaModelException e) {
      e.printStackTrace();
    } catch (MalformedTreeException e) {
      e.printStackTrace();
View Full Code Here

          return overridden;
        }
        IOpenable openable = member.getOpenable();
        if (openable == null)
          return InheritDocVisitor.CONTINUE;
        IBuffer buf= openable.getBuffer();
        if (buf == null) {
          // no source attachment found. This method maybe the one. Stop.
          return InheritDocVisitor.STOP_BRANCH;
        }

        ISourceRange javadocRange= member.getJavadocRange();
        if (javadocRange == null)
          return InheritDocVisitor.CONTINUE;  // this method doesn't have javadoc, continue to look.
        String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
        if (rawJavadoc != null) {
          return overridden;
        }
        return InheritDocVisitor.CONTINUE;
      }
View Full Code Here

    try {
      int importsStart=  this.replaceRange.getOffset();
      int importsLen= this.replaceRange.getLength();

      String lineDelim= this.compilationUnit.findRecommendedLineSeparator();
      IBuffer buffer= this.compilationUnit.getBuffer();

      int currPos= importsStart;
      MultiTextEdit resEdit= new MultiTextEdit();

      if ((this.flags & F_NEEDS_LEADING_DELIM) != 0) {
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.IBuffer

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.