NSMutableArray<EOProperty> result = new NSMutableArray<EOProperty>();
String[] parts = keyPath.split("\\.");
String part;
for (int i = 0; i < parts.length - 1; i++) {
part = parts[i];
EORelationship relationship = entity.anyRelationshipNamed(part);
if(relationship == null) {
// CHECKME AK: it would probably be better to return null
// to indicate that this is not a valid path?
return NSArray.EmptyArray;
}
entity = relationship.destinationEntity();
result.addObject(relationship);
}
part = parts[parts.length-1];
EOAttribute attribute = entity.anyAttributeNamed(part);
if(attribute == null) {
EORelationship relationship = entity.anyRelationshipNamed(part);
if(relationship == null) {
throw new IllegalArgumentException("Last element is not an attribute nor a relationship: " + keyPath);
}
if (relationship.isFlattened()) {
NSArray<EOProperty> path = attributePathForKeyPath(entity, relationship.definition());
result.addObjectsFromArray(path);
return result;
}
attribute = relationship.joins().lastObject().sourceAttribute();
}
result.addObject(attribute);
return result;
}