Example usage for org.eclipse.jdt.core ITypeRoot getSourceRange

List of usage examples for org.eclipse.jdt.core ITypeRoot getSourceRange

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot getSourceRange.

Prototype

ISourceRange getSourceRange() throws JavaModelException;

Source Link

Document

Returns the source range associated with this element.

Usage

From source file:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java

License:Open Source License

private static boolean isInvalidSelection(ITypeRoot root, final int offset) {
    try {/* w ww  .ja v a 2 s.c om*/
        if (!root.exists()) {
            return true;
        }
        // check whether the type root is part of an package fragment root. If not, it's an invalid selection and
        // all resolutions are likely to fail. Thus, return true (=invalid):
        IJavaElement ancestor = root.getAncestor(IJavaProject.PACKAGE_FRAGMENT_ROOT);
        if (!ancestor.exists()) {
            return true;
        }
        ISourceRange range = root.getSourceRange();
        return range == null || offset < 0 || offset > range.getLength();
    } catch (Exception e) {
        log(ERROR_EXCEPTION_WHILE_CHECKING_OFFSETS, e);
        return false;
    }
}

From source file:org.eclipse.recommenders.internal.rcp.JavaSelectionUtilsTest.java

License:Open Source License

@Test
public void testInvalidSelections02_withSourceRange() throws JavaModelException {
    ITypeRoot mock = mock(ITypeRoot.class);
    when(mock.getSourceRange()).thenReturn(new SourceRange(0, 100));
    assertEquals(absent(), JavaElementSelections.resolveJavaElementFromTypeRootInEditor(mock, -1));
    assertEquals(absent(), JavaElementSelections.resolveJavaElementFromTypeRootInEditor(mock, 0));
    assertEquals(absent(),/*from w  w  w. j  a va 2s. c o  m*/
            JavaElementSelections.resolveJavaElementFromTypeRootInEditor(mock, Integer.MAX_VALUE));
}