Example usage for org.eclipse.jdt.core ICodeAssist codeSelect

List of usage examples for org.eclipse.jdt.core ICodeAssist codeSelect

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ICodeAssist codeSelect.

Prototype

IJavaElement[] codeSelect(int offset, int length) throws JavaModelException;

Source Link

Document

Returns the Java elements corresponding to the given selected text in this compilation unit.

Usage

From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java

License:Open Source License

/**
 * Gets a IJavaElement by its position.//  w  w w  .  ja  va 2  s  . c om
 *
 * @param javaProject The IJavaProject the file is in.
 * @param filename The file containing the element.
 * @param offset The offset of the element in the file.
 * @param length The lenght of the element.
 * @return The element.
 */
protected IJavaElement getElement(IJavaProject javaProject, String filename, int offset, int length)
        throws Exception {
    ICodeAssist code = null;
    try {
        code = JavaUtils.getCompilationUnit(javaProject, filename);
    } catch (IllegalArgumentException iae) {
        // source not found, try location the class file.
        code = JavaUtils.findClassFile(javaProject, filename);
    }

    if (code != null) {
        IJavaElement[] elements = code.codeSelect(offset, length);
        if (elements != null && elements.length > 0) {
            return elements[0];
        }
    }
    return null;
}

From source file:org.eclim.plugin.jdt.command.type.MethodInfoCommand.java

License:Open Source License

/**
 * Gets a IJavaElement by its position.//from www .  j a  v a2 s.  co  m
 *
 * @param javaProject The IJavaProject the file is in.
 * @param filename The file containing the element.
 * @param offset The offset of the element in the file.
 * @param length The lenght of the element.
 * @return The element.
 */
protected IJavaElement getElement(IJavaProject javaProject, String filename, int offset, int length)
        throws Exception {
    ICodeAssist code = JavaUtils.getCompilationUnit(javaProject, filename);

    if (code != null) {

        IJavaElement[] elements = code.codeSelect(offset, length);
        if (elements != null && elements.length > 0) {
            return elements[0];
        } else {
            throw new RuntimeException("elements is null");
        }
    } else {
        throw new RuntimeException("code is null");
    }
}

From source file:org.eclipse.che.jdt.JavadocFinder.java

License:Open Source License

public String findJavadoc(IJavaProject project, String fqn, int offset) throws JavaModelException {

    IMember member = null;/*from w  w  w . j  a v a  2  s . c  om*/
    IType type = project.findType(fqn);
    ICodeAssist codeAssist;
    if (type.isBinary()) {
        codeAssist = type.getClassFile();
    } else {
        codeAssist = type.getCompilationUnit();
    }

    IJavaElement[] elements = null;
    if (codeAssist != null) {
        elements = codeAssist.codeSelect(/*region.getOffset(), region.getLength()*/ offset, 0);
    }
    IJavaElement element = null;
    if (elements != null && elements.length > 0) {
        element = elements[0];
    }

    if (element != null && element instanceof IMember) {
        member = ((IMember) element);
    }
    if (member == null) {
        return null;
    }
    return getJavadoc(member);
}

From source file:org.eclipse.che.jdt.JavaTypeHierarchy.java

License:Open Source License

private IJavaElement getJavaElement(IJavaProject project, String fqn, int offset) throws JavaModelException {
    IJavaElement originalElement = null;
    IType type = project.findType(fqn);/*from  w w w.jav  a  2 s  .  c  o  m*/
    ICodeAssist codeAssist;
    if (type.isBinary()) {
        codeAssist = type.getClassFile();
    } else {
        codeAssist = type.getCompilationUnit();
    }

    IJavaElement[] elements = null;
    if (codeAssist != null) {
        elements = codeAssist.codeSelect(offset, 0);
    }

    if (elements != null && elements.length > 0) {
        originalElement = elements[0];
    }
    return originalElement;
}

From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

public OpenDeclarationDescriptor findDeclaration(IJavaProject project, String fqn, int offset)
        throws JavaModelException {
    IJavaElement originalElement = null;
    IType type = project.findType(fqn);/*www .j  a  v  a2s  . c  om*/
    ICodeAssist codeAssist;
    if (type.isBinary()) {
        codeAssist = type.getClassFile();
    } else {
        codeAssist = type.getCompilationUnit();
    }

    IJavaElement[] elements = null;
    if (codeAssist != null) {
        elements = codeAssist.codeSelect(offset, 0);
    }

    if (elements != null && elements.length > 0) {
        originalElement = elements[0];
    }
    IJavaElement element = originalElement;
    while (element != null) {
        if (element instanceof ICompilationUnit) {
            ICompilationUnit unit = ((ICompilationUnit) element).getPrimary();
            return compilationUnitNavigation(unit, originalElement);
        }

        if (element instanceof IClassFile) {
            return classFileNavigation((IClassFile) element, originalElement);
        }
        element = element.getParent();
    }
    return null;
}