Null checks, class match checks and exception handling omitted:

 JTextComponent editor = Registry.getMostActiveComponent();
BaseDocument baseDoc = (BaseDocument) ed.getDocument();
int offset = ed.getCaret().getDot();
JMIUtils utils = JMIUtils.get(baseDoc);
Resource resource = utils.getResource();
Element el = resource.getElementByOffset(offset);

To iterate backwards and find, say, the method the caret is in (if any), use a while loop ala:

 while (!(el instanceof Feature) && el != null) {
   el = (Element) el.refImmediateComposite();

Source: NetBeans FAQ