Search in sources :

Example 36 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class JavaTypeHierarchy method setRange.

private void setRange(Member member, IMember iMember) throws JavaModelException {
    ISourceRange nameRange = iMember.getNameRange();
    if (iMember.isBinary()) {
        nameRange = iMember.getSourceRange();
    }
    if (nameRange == null) {
        return;
    }
    member.setFileRegion(convertToRegionDTO(iMember.getSourceRange()));
}
Also used : ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 37 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class StubUtility2 method getNodeToInsertBefore.

/**
	 * Evaluates the insertion position of a new node.
	 *
	 * @param listRewrite The list rewriter to which the new node will be added
	 * @param sibling The Java element before which the new element should be added.
	 * @return the AST node of the list to insert before or null to insert as last.
	 * @throws JavaModelException thrown if accessing the Java element failed
	 */
public static ASTNode getNodeToInsertBefore(ListRewrite listRewrite, IJavaElement sibling) throws JavaModelException {
    if (sibling instanceof IMember) {
        ISourceRange sourceRange = ((IMember) sibling).getSourceRange();
        if (sourceRange == null) {
            return null;
        }
        int insertPos = sourceRange.getOffset();
        List<? extends ASTNode> members = listRewrite.getOriginalList();
        for (int i = 0; i < members.size(); i++) {
            ASTNode curr = members.get(i);
            if (curr.getStartPosition() >= insertPos) {
                return curr;
            }
        }
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMember(org.eclipse.jdt.core.IMember) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 38 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class Util method getUnresolvedJavaElement.

/**
     * Returns the IInitializer that contains the given local variable in the given type
     */
public static JavaElement getUnresolvedJavaElement(int localSourceStart, int localSourceEnd, JavaElement type) {
    try {
        if (!(type instanceof IType))
            return null;
        IInitializer[] initializers = ((IType) type).getInitializers();
        for (int i = 0; i < initializers.length; i++) {
            IInitializer initializer = initializers[i];
            ISourceRange sourceRange = initializer.getSourceRange();
            if (sourceRange != null) {
                int initializerStart = sourceRange.getOffset();
                int initializerEnd = initializerStart + sourceRange.getLength();
                if (initializerStart <= localSourceStart && localSourceEnd <= initializerEnd) {
                    return (JavaElement) initializer;
                }
            }
        }
        return null;
    } catch (JavaModelException e) {
        return null;
    }
}
Also used : JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IInitializer(org.eclipse.jdt.core.IInitializer) IType(org.eclipse.jdt.core.IType) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 39 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class JavadocContentAccess method internalGetContentReader.

/**
     * Gets a reader for an IMember's Javadoc comment content from the source attachment.
     * The content does contain only the text from the comment without the Javadoc leading star characters.
     * Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
     *
     * @param member
     *         The member to get the Javadoc of.
     * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
     * does not contain a Javadoc comment or if no source is available
     * @throws org.eclipse.jdt.core.JavaModelException
     *         is thrown when the elements javadoc can not be accessed
     * @since 3.4
     */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
    IBuffer buf = member.getOpenable().getBuffer();
    if (buf == null) {
        // no source attachment found
        return null;
    }
    ISourceRange javadocRange = member.getJavadocRange();
    if (javadocRange != null) {
        JavaDocCommentReader reader = new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
        if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
            reader.reset();
            return reader;
        }
    }
    return null;
}
Also used : IBuffer(org.eclipse.jdt.core.IBuffer) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 40 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class JavadocContentAccess2 method handleConstantValue.

private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
    String text = null;
    ISourceRange nameRange = field.getNameRange();
    if (SourceRange.isAvailable(nameRange)) {
        CompilationUnit cuNode = ASTProvider.createAST(field.getTypeRoot(), null);
        if (cuNode != null) {
            ASTNode nameNode = NodeFinder.perform(cuNode, nameRange);
            if (nameNode instanceof SimpleName) {
                IBinding binding = ((SimpleName) nameNode).resolveBinding();
                if (binding instanceof IVariableBinding) {
                    IVariableBinding variableBinding = (IVariableBinding) binding;
                    Object constantValue = variableBinding.getConstantValue();
                    if (constantValue != null) {
                        if (constantValue instanceof String) {
                            text = ASTNodes.getEscapedStringLiteral((String) constantValue);
                        } else {
                            // Javadoc tool is even worse for chars...
                            text = constantValue.toString();
                        }
                    }
                }
            }
        }
    }
    if (text == null) {
        Object constant = field.getConstant();
        if (constant != null) {
            text = constant.toString();
        }
    }
    if (text != null) {
        text = HTMLPrinter.convertToHTMLContentWithWhitespace(text);
        if (link) {
            String uri;
            try {
                uri = JavaElementLinks.createURI(urlPrefix, field);
                fBuf.append(JavaElementLinks.createLink(uri, text));
            } catch (URISyntaxException e) {
                LOG.error(e.getMessage(), e);
                return false;
            }
        } else {
            handleText(text);
        }
        return true;
    }
    return false;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) URISyntaxException(java.net.URISyntaxException) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceRange (org.eclipse.jdt.core.ISourceRange)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 JavaModelException (org.eclipse.jdt.core.JavaModelException)13 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ISourceReference (org.eclipse.jdt.core.ISourceReference)9 IType (org.eclipse.jdt.core.IType)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 SourceRange (org.eclipse.jdt.core.SourceRange)6 IBuffer (org.eclipse.jdt.core.IBuffer)5 IMethod (org.eclipse.jdt.core.IMethod)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ArrayList (java.util.ArrayList)4 IField (org.eclipse.jdt.core.IField)4 IMember (org.eclipse.jdt.core.IMember)4 CoreException (org.eclipse.core.runtime.CoreException)3 IClassFile (org.eclipse.jdt.core.IClassFile)3