title: Get enclosing IJavaElement
body: <p>I am working with the selections within the Eclipse source code editor. I defined a key-binding that gets me the "text" at the current caret position. I managed to parse the current caret position to an IJavaElement</p>
  
  <pre><code>private void processEditorSelection(IEditorPart part, ITextSelection selection) {
            IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
              final ITypeRoot root = (ITypeRoot) JavaUI.getEditorInputJavaElement(editorInput);
              if (root != null) {
                 int offset = (selection).getOffset();
                  IJavaElement[] codeSelect = root.codeSelect(offset, 0);
                  if (codeSelect.length &gt; 0) {
                      //codeSelect[0...n] are my resolved elements at the current caret pos.
         }
  </code></pre>
  
  <p>What I did not managed to figure out yet is, how to resolve the enclosing type. for instance:</p>
  
  <pre><code>public void do() {
         System.out.println("it"); 
  }
  </code></pre>
  
  <p>If the caret is on the System.out, I obtain an JavaElement of the system out call, but I am actually interested in the enclosing method. Does anyone know how to ask Eclipse for the enclosing type? Like what is the compilation unit or method my current carret position is enclosed in?</p>
  
  <p>Thanks for an answer</p>