Program: 59587
Project: 59587
Buggy source:
protected OEntity toOEntity(EdmEntitySet ees,Object obj,List<EntitySimpleProperty> expand){
  InMemoryEntityInfo<?,?> ei=eis.get(ees.getName());
  final List<OLink> links=new ArrayList<OLink>();
  final List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  Map<String,Object> keyKVPair=new HashMap<String,Object>();
  for (  String key : ei.keys) {
    Object keyValue=ei.properties.getPropertyValue(obj,key);
    keyKVPair.put(key,keyValue);
  }
  for (  String propName : ei.properties.getPropertyNames()) {
    EdmSimpleType<?> type;
    Object value=ei.properties.getPropertyValue(obj,propName);
    Class<?> propType=ei.properties.getPropertyType(propName);
    type=typeMapping.findEdmType(propType);
    if (type == null)     continue;
    properties.add(OProperties.simple(propName,type,value));
  }
  if (expand != null && !expand.isEmpty()) {
    EdmEntityType edmEntityType=ees.getType();
    HashMap<String,List<EntitySimpleProperty>> expandedProps=new HashMap<String,List<EntitySimpleProperty>>();
    for (    final EntitySimpleProperty propPath : expand) {
      String[] props=propPath.getPropertyName().split("String_Node_Str",2);
      String prop=props[0];
      String remainingPropPath=props.length > 1 ? props[1] : null;
      if (expandedProps.containsKey(prop)) {
        if (remainingPropPath != null) {
          List<EntitySimpleProperty> remainingPropPaths=expandedProps.get(prop);
          remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
        }
      }
 else {
        List<EntitySimpleProperty> remainingPropPaths=new ArrayList<EntitySimpleProperty>();
        if (remainingPropPath != null)         remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
        expandedProps.put(prop,remainingPropPaths);
      }
    }
    for (    final String prop : expandedProps.keySet()) {
      List<EntitySimpleProperty> remainingPropPath=expandedProps.get(prop);
      EdmNavigationProperty edmNavProperty=edmEntityType.findNavigationProperty(prop);
      if (edmNavProperty == null)       continue;
      if (edmNavProperty.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        List<OEntity> relatedEntities=new ArrayList<OEntity>();
        Iterable<?> values=ei.properties.getCollectionValue(obj,prop);
        if (values != null) {
          EdmEntitySet relEntitySet=null;
          for (          final Object entity : values) {
            if (relEntitySet == null) {
              InMemoryEntityInfo<?,?> oei=Enumerable.create(eis.values()).firstOrNull(new Predicate1<InMemoryEntityInfo<?,?>>(){
                @Override public boolean apply(                InMemoryEntityInfo<?,?> input){
                  return entity.getClass().equals(input.entityClass);
                }
              }
);
              relEntitySet=getMetadata().getEdmEntitySet(oei.entitySetName);
            }
            relatedEntities.add(toOEntity(relEntitySet,entity,remainingPropPath));
          }
        }
        links.add(OLinks.relatedEntitiesInline(null,edmNavProperty.getName(),null,relatedEntities));
      }
 else {
        final Object entity=ei.properties.getPropertyValue(obj,prop);
        OEntity relatedEntity=null;
        if (entity != null) {
          InMemoryEntityInfo<?,?> oei=Enumerable.create(eis.values()).firstOrNull(new Predicate1<InMemoryEntityInfo<?,?>>(){
            @Override public boolean apply(            InMemoryEntityInfo<?,?> input){
              return entity.getClass().equals(input.entityClass);
            }
          }
);
          EdmEntitySet relEntitySet=getMetadata().getEdmEntitySet(oei.entitySetName);
          relatedEntity=toOEntity(relEntitySet,entity,remainingPropPath);
        }
        links.add(OLinks.relatedEntityInline(null,edmNavProperty.getName(),null,relatedEntity));
      }
    }
  }
  for (  final EdmNavigationProperty ep : ees.getType().getNavigationProperties()) {
    boolean expanded=null != Enumerable.create(links).firstOrNull(new Predicate1<OLink>(){
      @Override public boolean apply(      OLink t){
        return t.getTitle().equals(ep.getName());
      }
    }
);
    if (!expanded) {
      if (ep.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        links.add(OLinks.relatedEntities(null,ep.getName(),null));
      }
 else {
        links.add(OLinks.relatedEntity(null,ep.getName(),null));
      }
    }
  }
  return OEntities.create(ees,OEntityKey.create(keyKVPair),properties,links,obj);
}

Fixed version (ground truth):
protected OEntity toOEntity(EdmEntitySet ees,Object obj,List<EntitySimpleProperty> expand){
  InMemoryEntityInfo<?> ei=eis.get(ees.getName());
  final List<OLink> links=new ArrayList<OLink>();
  final List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  Map<String,Object> keyKVPair=new HashMap<String,Object>();
  for (  String key : ei.keys) {
    Object keyValue=ei.properties.getPropertyValue(obj,key);
    keyKVPair.put(key,keyValue);
  }
  for (  String propName : ei.properties.getPropertyNames()) {
    EdmSimpleType<?> type;
    Object value=ei.properties.getPropertyValue(obj,propName);
    Class<?> propType=ei.properties.getPropertyType(propName);
    type=typeMapping.findEdmType(propType);
    if (type == null)     continue;
    properties.add(OProperties.simple(propName,type,value));
  }
  if (expand != null && !expand.isEmpty()) {
    EdmEntityType edmEntityType=ees.getType();
    HashMap<String,List<EntitySimpleProperty>> expandedProps=new HashMap<String,List<EntitySimpleProperty>>();
    for (    final EntitySimpleProperty propPath : expand) {
      String[] props=propPath.getPropertyName().split("String_Node_Str",2);
      String prop=props[0];
      String remainingPropPath=props.length > 1 ? props[1] : null;
      if (expandedProps.containsKey(prop)) {
        if (remainingPropPath != null) {
          List<EntitySimpleProperty> remainingPropPaths=expandedProps.get(prop);
          remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
        }
      }
 else {
        List<EntitySimpleProperty> remainingPropPaths=new ArrayList<EntitySimpleProperty>();
        if (remainingPropPath != null)         remainingPropPaths.add(Expression.simpleProperty(remainingPropPath));
        expandedProps.put(prop,remainingPropPaths);
      }
    }
    for (    final String prop : expandedProps.keySet()) {
      List<EntitySimpleProperty> remainingPropPath=expandedProps.get(prop);
      EdmNavigationProperty edmNavProperty=edmEntityType.findNavigationProperty(prop);
      if (edmNavProperty == null)       continue;
      if (edmNavProperty.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        List<OEntity> relatedEntities=new ArrayList<OEntity>();
        Iterable<?> values=ei.properties.getCollectionValue(obj,prop);
        if (values != null) {
          EdmEntitySet relEntitySet=null;
          for (          final Object entity : values) {
            if (relEntitySet == null) {
              InMemoryEntityInfo<?> oei=Enumerable.create(eis.values()).firstOrNull(new Predicate1<InMemoryEntityInfo<?>>(){
                @Override public boolean apply(                InMemoryEntityInfo<?> input){
                  return entity.getClass().equals(input.entityClass);
                }
              }
);
              relEntitySet=getMetadata().getEdmEntitySet(oei.entitySetName);
            }
            relatedEntities.add(toOEntity(relEntitySet,entity,remainingPropPath));
          }
        }
        links.add(OLinks.relatedEntitiesInline(null,edmNavProperty.getName(),null,relatedEntities));
      }
 else {
        final Object entity=ei.properties.getPropertyValue(obj,prop);
        OEntity relatedEntity=null;
        if (entity != null) {
          InMemoryEntityInfo<?> oei=Enumerable.create(eis.values()).firstOrNull(new Predicate1<InMemoryEntityInfo<?>>(){
            @Override public boolean apply(            InMemoryEntityInfo<?> input){
              return entity.getClass().equals(input.entityClass);
            }
          }
);
          EdmEntitySet relEntitySet=getMetadata().getEdmEntitySet(oei.entitySetName);
          relatedEntity=toOEntity(relEntitySet,entity,remainingPropPath);
        }
        links.add(OLinks.relatedEntityInline(null,edmNavProperty.getName(),null,relatedEntity));
      }
    }
  }
  for (  final EdmNavigationProperty ep : ees.getType().getNavigationProperties()) {
    boolean expanded=null != Enumerable.create(links).firstOrNull(new Predicate1<OLink>(){
      @Override public boolean apply(      OLink t){
        return t.getTitle().equals(ep.getName());
      }
    }
);
    if (!expanded) {
      if (ep.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        links.add(OLinks.relatedEntities(null,ep.getName(),null));
      }
 else {
        links.add(OLinks.relatedEntity(null,ep.getName(),null));
      }
    }
  }
  return OEntities.create(ees,OEntityKey.create(keyKVPair),properties,links,obj);
}