DefUnit parentDefUnit = NodeUtil.getOuterDefUnit(defUnit);
if(parentDefUnit == null) {
return sourceModule.getType(defUnit.getName());
} else {
IMember parentElement = searchForModelElement(parentDefUnit, sourceModule, returnNonExisting);
if(parentElement == null) {
return null;
}
IMember bestMatch = null;
if(parentElement.exists()) {
final IModelElement[] children = (IModelElement[]) parentElement.getChildren();
for (int i = 0; i < children.length; i++) {
IModelElement modelElement = children[i];
if(!modelElement.getElementName().equals(defUnit.getName()))
continue;
switch (modelElement.getElementType()) {
case IModelElement.FIELD:
if(!isFieldElement(defUnit)) continue;
break;
case IModelElement.METHOD:
if(!isMethodElement(defUnit)) continue;
break;
case IModelElement.TYPE:
if(!isTypeElement(defUnit)) continue;
break;
default:
assertFail();
}
IMember member = (IMember) modelElement;
ISourceRange nameRange = member.getNameRange();
if(nameRange != null && nameRange.getOffset() == defUnit.defname.getStartPos()) {
return member; // We found a perfect match
}
bestMatch = member;
}