Example usage for org.eclipse.jdt.core IOpenable getBuffer

List of usage examples for org.eclipse.jdt.core IOpenable getBuffer

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IOpenable getBuffer.

Prototype

public IBuffer getBuffer() throws JavaModelException;

Source Link

Document

Returns the buffer opened for this element, or null if this element does not have a buffer.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private Object findMethodWithAttachedDocInHierarchy(final MethodBinding method) throws JavaModelException {
    ReferenceBinding type = method.declaringClass;
    final SelectionRequestor requestor1 = (SelectionRequestor) this.requestor;
    return new InheritDocVisitor() {
        public Object visit(ReferenceBinding currType) throws JavaModelException {
            MethodBinding overridden = findOverriddenMethodInType(currType, method);
            if (overridden == null)
                return InheritDocVisitor.CONTINUE;
            TypeBinding args[] = overridden.parameters;
            String names[] = new String[args.length];
            for (int i = 0; i < args.length; i++) {
                names[i] = Signature.createTypeSignature(args[i].sourceName(), false);
            }/*from w w w .  j  a  v  a 2 s  . co m*/
            IMember member = (IMember) requestor1.findMethodFromBinding(overridden, names,
                    overridden.declaringClass);
            if (member == null)
                return InheritDocVisitor.CONTINUE;
            if (member.getAttachedJavadoc(null) != null) {
                // for binary methods with attached javadoc and no source attached
                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;
        }
    }.visitInheritDoc(type);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.LocalVariable.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ISourceReference
 *//*w  w  w.ja v  a 2 s  . c o  m*/
public String getSource() throws JavaModelException {
    IOpenable openable = this.parent.getOpenableParent();
    IBuffer buffer = openable.getBuffer();
    if (buffer == null) {
        return null;
    }
    ISourceRange range = getSourceRange();
    int offset = range.getOffset();
    int length = range.getLength();
    if (offset == -1 || length == 0) {
        return null;
    }
    try {
        return buffer.getText(offset, length);
    } catch (RuntimeException e) {
        return null;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceRefElement.java

License:Open Source License

/**
 * @see ISourceReference/* w w w  .ja  va2  s  .  co  m*/
 */
public String getSource() throws JavaModelException {
    IOpenable openable = getOpenableParent();
    IBuffer buffer = openable.getBuffer();
    if (buffer == null) {
        return null;
    }
    ISourceRange range = getSourceRange();
    int offset = range.getOffset();
    int length = range.getLength();
    if (offset == -1 || length == 0) {
        return null;
    }
    try {
        return buffer.getText(offset, length);
    } catch (RuntimeException e) {
        return null;
    }
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaDocLocations.java

License:Open Source License

/**
 * Returns the reason for why the Javadoc of the Java element could not be retrieved.
 *
 * @param element whose Javadoc could not be retrieved
 * @param root the root of the Java element
 * @return the String message for why the Javadoc could not be retrieved for the Java element or
 *         <code>null</code> if the Java element is from a source container
 * @since 3.9//  w w w .  ja v  a  2 s .co  m
 */
public static String getExplanationForMissingJavadoc(IJavaElement element, IPackageFragmentRoot root) {
    String message = null;
    try {
        boolean isBinary = (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY);
        if (isBinary) {
            boolean hasAttachedJavadoc = JavaDocLocations.getJavadocBaseLocation(element) != null;
            boolean hasAttachedSource = root.getSourceAttachmentPath() != null;
            IOpenable openable = element.getOpenable();
            boolean hasSource = openable.getBuffer() != null;

            // Provide hint why there's no Java doc
            if (!hasAttachedSource && !hasAttachedJavadoc)
                message = CorextMessages.JavaDocLocations_noAttachments;
            else if (!hasAttachedJavadoc && !hasSource)
                message = CorextMessages.JavaDocLocations_noAttachedJavadoc;
            else if (!hasAttachedSource)
                message = CorextMessages.JavaDocLocations_noAttachedSource;
            else if (!hasSource)
                message = CorextMessages.JavaDocLocations_noInformation;

        }
    } catch (JavaModelException e) {
        message = CorextMessages.JavaDocLocations_error_gettingJavadoc;
        LOG.error(message, e);
    }
    return message;
}

From source file:com.microsoft.javapkgsrv.JavaParser.java

License:MIT License

public List<FindDefinitionResponse.JavaElement> ProcessFindDefinintionRequest(String fileParseContents,
        String typeRootId, int cursorPosition) throws Exception {
    if (ActiveTypeRoots.containsKey(typeRootId)) {
        ITypeRoot cu = ActiveTypeRoots.get(typeRootId);
        cu.getBuffer().setContents(fileParseContents.toCharArray());
        IJavaElement[] elements = cu.codeSelect(cursorPosition, 0);

        List<FindDefinitionResponse.JavaElement> ret = new ArrayList<FindDefinitionResponse.JavaElement>();
        for (IJavaElement element : elements) {
            String definition = element.toString();
            String path = element.getResource() != null ? element.getResource().getLocation().toOSString()
                    : element.getPath().toOSString();
            //String path = element.getPath().makeAbsolute().toOSString(); // element.getPath().toString();

            boolean isAvailable = false;
            int posStart = -1;
            int posLength = 0;
            String contents = null;
            String classFileName = null;
            IClassFile classFileObj = null;

            ISourceReference srcRef = (ISourceReference) element;
            if (srcRef != null) {
                ISourceRange range = srcRef.getSourceRange();
                if (SourceRange.isAvailable(range)) {
                    isAvailable = true;//from  ww w .  ja v  a2s  . c  o  m
                    posStart = range.getOffset();
                    posLength = range.getLength();

                    //if (path.endsWith(".jar"))
                    //{
                    IOpenable op = element.getOpenable();
                    if (op != null && op instanceof IClassFile) {
                        IBuffer buff = op.getBuffer();
                        classFileObj = (IClassFile) op;
                        classFileName = classFileObj.getElementName();
                        contents = buff.getContents();
                    }
                    //}
                }
            }

            FindDefinitionResponse.JavaElement.Builder retItem = FindDefinitionResponse.JavaElement.newBuilder()
                    .setDefinition(definition).setFilePath(path).setHasSource(isAvailable)
                    .setPositionStart(posStart).setPositionLength(posLength);

            if (contents != null) {
                //int hashCode = classFileObj.hashCode();
                String handle = classFileObj.getHandleIdentifier();
                ActiveTypeRoots.put(handle, classFileObj);

                retItem.setFileName(classFileName);
                retItem.setTypeRootIdentifier(TypeRootIdentifier.newBuilder().setHandle(handle).build());
            }
            System.out.println(retItem.toString());
            if (contents != null) {
                retItem.setFileContents(contents);
            }
            ret.add(retItem.build());
        }
        return ret;
    }
    return null;
}

From source file:org.eclipse.ajdt.core.javaelements.AJCodeElement.java

License:Open Source License

public void initializeLocations() {
    // try the easy way:
    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
            .javaElementToProgramElement(this);
    ISourceLocation sloc = ipe.getSourceLocation();
    if (sloc != null) {
        startLine = sloc.getLine();//  w w w  .j a  v a  2s.  c  o m

        nameStart = sloc.getOffset();
        if (sloc instanceof EclipseSourceLocation) {
            EclipseSourceLocation esloc = (EclipseSourceLocation) sloc;
            nameEnd = esloc.getEndPos();
        }
    }

    // sometimes the start and end values are not set...so do it the hard way
    // so calculate it from the line
    if (nameStart <= 0 || nameEnd <= 0) {
        try {
            IOpenable openable = this.parent.getOpenableParent();
            IBuffer buffer;
            if (openable instanceof AJCompilationUnit) {
                AJCompilationUnit ajCompUnit = (AJCompilationUnit) openable;
                ajCompUnit.requestOriginalContentMode();
                buffer = openable.getBuffer();
                ajCompUnit.discardOriginalContentMode();
            } else {
                buffer = openable.getBuffer();
            }
            String source = buffer.getContents();

            int lines = 0;
            for (int i = 0; i < source.length(); i++) {
                if (source.charAt(i) == '\n') {
                    lines++;
                    if (lines == startLine - 1) {
                        // starting remove white space
                        i++;
                        while (i < source.length()
                                && (Character.isWhitespace(source.charAt(i)) && source.charAt(i) != '\n')) {
                            i++;
                        }
                        nameStart = i;
                        break;
                    }
                }
            }

            for (int i = nameStart + 1; i < source.length(); i++) {
                if (source.charAt(i) == '\n' || source.charAt(i) == ';') {
                    nameEnd = i - 1;
                    break;
                }
            }

            nameStart = Math.min(nameStart, nameEnd);
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.ajdt.internal.core.search.AJDTSearchProvider.java

License:Open Source License

public char[] findSource(IOpenable elt) {
    if (elt instanceof AJCompilationUnit) {
        try {//  w  w  w  . ja  va  2s . co m
            IBuffer buf = elt.getBuffer();
            if (buf instanceof JavaCompatibleBuffer) {
                JavaCompatibleBuffer convertingBuf = (JavaCompatibleBuffer) buf;
                ConversionOptions orig = convertingBuf.getConversionOptions();
                convertingBuf.setConversionOptions(ConversionOptions.CONSTANT_SIZE);
                char[] contents = convertingBuf.getCharacters();
                convertingBuf.setConversionOptions(orig);
                return contents;
            }
        } catch (JavaModelException e) {
        }

        // couldn't get the buffer for some reason, but we should still 
        // return the converted contents as this is better than returning the original contents
        return ((AJCompilationUnit) elt).getContents();
    } else {
        // should return null here, so contents will be gotten in the normal way.
        return null;
    }
}

From source file:org.eclipse.jem.internal.adapters.jdom.JavaFieldJDOMAdaptor.java

License:Open Source License

/**
 * Return a String for the source starting after the field's name to the end of
 * the source range.  This will be the source after the name which could include comments.
 *///from ww w  .jav a2  s .  c  om
protected String getFieldInitializerSource() {
    IOpenable openable = getSourceField().getOpenable();
    try {
        ISourceRange nameRange, sourceRange;
        int start = -1, length = 0;
        IBuffer buffer = openable.getBuffer();
        if (buffer == null) {
            return ""; //$NON-NLS-1$
        }
        nameRange = getSourceField().getNameRange();
        start = nameRange.getOffset() + nameRange.getLength();
        if (start != -1) {
            sourceRange = getSourceField().getSourceRange();
            if (sourceRange.getOffset() != -1)
                length = sourceRange.getOffset() + sourceRange.getLength() - start;
            return buffer.getText(start, length);
        }
        return null;
    } catch (JavaModelException e) {
        return ""; //$NON-NLS-1$
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.MethodMapping.java

License:Open Source License

/**
 * @see ISourceReference//from w  w  w .j a va  2 s.  c  o  m
 */
public String getSource() throws JavaModelException {
    IOpenable openable = getOpenableParent();
    IBuffer buffer = openable.getBuffer();
    if (buffer == null) {
        return null;
    }
    ISourceRange range = getSourceRange();
    int offset = range.getOffset();
    int length = range.getLength();
    if (offset == -1 || length == 0) {
        return null;
    }
    try {
        return buffer.getText(offset, length);
    } catch (RuntimeException ex) {
        return null;
    }
}

From source file:org.jboss.tools.jst.jsp.jspeditor.info.JavaStringELInfoHover.java

License:Open Source License

/**
 * Adds full information to the hover/*from  ww  w . j a va 2s .co  m*/
 * Returns base URL if exists
 * 
 * @param buffer
 * @param element
 * @return
 */
private static String addFullInfo(StringBuffer buffer, IJavaElement element, boolean useFullHTML) {
    String base = null;

    if (element instanceof IMember) {
        IMember member = (IMember) element;
        HTMLPrinter.addSmallHeader(buffer, getInfoText(member, true, useFullHTML));
        Reader reader;
        try {
            String content = JavadocContentAccess2.getHTMLContent(member, true);
            reader = content == null ? null : new StringReader(content);

            // Provide hint why there's no Javadoc
            if (reader == null && member.isBinary()) {
                boolean hasAttachedJavadoc = JavaDocLocations.getJavadocBaseLocation(member) != null;
                IPackageFragmentRoot root = (IPackageFragmentRoot) member
                        .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                boolean hasAttachedSource = root != null && root.getSourceAttachmentPath() != null;
                IOpenable openable = member.getOpenable();
                boolean hasSource = openable.getBuffer() != null;

                if (!hasAttachedSource && !hasAttachedJavadoc)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachments);
                else if (!hasAttachedJavadoc && !hasSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachedJavadoc);
                else if (!hasAttachedSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noAttachedJavaSource);
                else if (!hasSource)
                    reader = new StringReader(ELInfoHoverMessages.ELInfoHover_noInformation);

            } else {
                base = JavaDocLocations.getBaseURL(member);
            }

        } catch (JavaModelException ex) {
            reader = new StringReader(ELInfoHoverMessages.ELInfoHover_error_gettingJavadoc);
            JavaPlugin.log(ex);
        }

        if (reader != null) {
            HTMLPrinter.addParagraph(buffer, reader);
        }

    } else if (element.getElementType() == IJavaElement.LOCAL_VARIABLE
            || element.getElementType() == IJavaElement.TYPE_PARAMETER) {
        HTMLPrinter.addSmallHeader(buffer, getInfoText(element, true, useFullHTML));
    }
    return base;
}