Program: 39274
Project: 39274
Buggy source:
@SuppressWarnings("String_Node_Str") @Monitorable private Entity performSave(final InternalDataDefinition dataDefinition,final Entity genericEntity,final Set<Entity> alreadySavedEntities,final Set<Entity> newlySavedEntities){
  checkNotNull(dataDefinition,"String_Node_Str");
  checkState(dataDefinition.isEnabled(),"String_Node_Str");
  checkNotNull(genericEntity,"String_Node_Str");
  if (alreadySavedEntities.contains(genericEntity)) {
    return genericEntity;
  }
  Entity genericEntityToSave=genericEntity.copy();
  Object existingDatabaseEntity=getExistingDatabaseEntity(dataDefinition,genericEntity);
  Entity existingGenericEntity=null;
  if (existingDatabaseEntity != null) {
    existingGenericEntity=entityService.convertToGenericEntity(dataDefinition,existingDatabaseEntity);
  }
  validationService.validateGenericEntity(dataDefinition,genericEntity,existingGenericEntity);
  if (!genericEntity.isValid()) {
    copyValidationErrors(dataDefinition,genericEntityToSave,genericEntity);
    if (existingGenericEntity != null) {
      copyMissingFields(genericEntityToSave,existingGenericEntity);
    }
    LOG.info(genericEntityToSave + "String_Node_Str");
    if (LOG.isDebugEnabled()) {
      for (      ErrorMessage error : genericEntityToSave.getGlobalErrors()) {
        LOG.debug("String_Node_Str" + error.getMessage());
      }
      for (      Map.Entry<String,ErrorMessage> error : genericEntityToSave.getErrors().entrySet()) {
        LOG.debug("String_Node_Str" + error.getKey() + "String_Node_Str"+ error.getValue().getMessage());
      }
    }
    return genericEntityToSave;
  }
  Object databaseEntity=entityService.convertToDatabaseEntity(dataDefinition,genericEntity,existingDatabaseEntity);
  if (genericEntity.getId() == null) {
    priorityService.prioritizeEntity(dataDefinition,databaseEntity);
  }
  saveDatabaseEntity(dataDefinition,databaseEntity);
  Entity savedEntity=entityService.convertToGenericEntity(dataDefinition,databaseEntity);
  LOG.info(savedEntity + "String_Node_Str");
  for (  Entry<String,FieldDefinition> fieldEntry : dataDefinition.getFields().entrySet()) {
    if (fieldEntry.getValue().getType() instanceof HasManyType) {
      List<Entity> entities=(List<Entity>)genericEntity.getField(fieldEntry.getKey());
      HasManyType hasManyType=(HasManyType)fieldEntry.getValue().getType();
      if (entities == null || entities instanceof EntityListImpl) {
        savedEntity.setField(fieldEntry.getKey(),entities);
        continue;
      }
      List<Entity> savedEntities=saveHasManyEntities(alreadySavedEntities,newlySavedEntities,hasManyType.getJoinFieldName(),savedEntity.getId(),entities,(InternalDataDefinition)hasManyType.getDataDefinition());
      EntityList dbEntities=savedEntity.getHasManyField(fieldEntry.getKey());
      removeOrphans(savedEntities,(InternalDataDefinition)hasManyType.getDataDefinition(),dbEntities);
      savedEntity.setField(fieldEntry.getKey(),savedEntities);
    }
 else     if (fieldEntry.getValue().getType() instanceof TreeType) {
      List<Entity> entities=(List<Entity>)genericEntity.getField(fieldEntry.getKey());
      if (entities == null || entities instanceof EntityTreeImpl) {
        savedEntity.setField(fieldEntry.getKey(),entities);
        continue;
      }
      TreeType treeType=(TreeType)fieldEntry.getValue().getType();
      List<Entity> savedEntities=saveTreeEntities(alreadySavedEntities,newlySavedEntities,treeType.getJoinFieldName(),savedEntity.getId(),entities,(InternalDataDefinition)treeType.getDataDefinition(),null);
      savedEntity.setField(fieldEntry.getKey(),savedEntities);
    }
  }
  alreadySavedEntities.add(savedEntity);
  if (genericEntity.getId() == null && savedEntity.getId() != null) {
    newlySavedEntities.add(savedEntity);
  }
  return savedEntity;
}

Fixed version (ground truth):
@SuppressWarnings("String_Node_Str") @Monitorable private Entity performSave(final InternalDataDefinition dataDefinition,final Entity genericEntity,final Set<Entity> alreadySavedEntities,final Set<Entity> newlySavedEntities){
  checkNotNull(dataDefinition,DATA_DEFINITION_MUST_BE_GIVEN);
  checkState(dataDefinition.isEnabled(),DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
  checkNotNull(genericEntity,"String_Node_Str");
  if (alreadySavedEntities.contains(genericEntity)) {
    return genericEntity;
  }
  Entity genericEntityToSave=genericEntity.copy();
  Object existingDatabaseEntity=getExistingDatabaseEntity(dataDefinition,genericEntity);
  Entity existingGenericEntity=null;
  if (existingDatabaseEntity != null) {
    existingGenericEntity=entityService.convertToGenericEntity(dataDefinition,existingDatabaseEntity);
  }
  validationService.validateGenericEntity(dataDefinition,genericEntity,existingGenericEntity);
  if (!genericEntity.isValid()) {
    copyValidationErrors(dataDefinition,genericEntityToSave,genericEntity);
    if (existingGenericEntity != null) {
      copyMissingFields(genericEntityToSave,existingGenericEntity);
    }
    LOG.info(genericEntityToSave + "String_Node_Str");
    if (LOG.isDebugEnabled()) {
      for (      ErrorMessage error : genericEntityToSave.getGlobalErrors()) {
        LOG.debug("String_Node_Str" + error.getMessage());
      }
      for (      Map.Entry<String,ErrorMessage> error : genericEntityToSave.getErrors().entrySet()) {
        LOG.debug("String_Node_Str" + error.getKey() + "String_Node_Str"+ error.getValue().getMessage());
      }
    }
    return genericEntityToSave;
  }
  Object databaseEntity=entityService.convertToDatabaseEntity(dataDefinition,genericEntity,existingDatabaseEntity);
  if (genericEntity.getId() == null) {
    priorityService.prioritizeEntity(dataDefinition,databaseEntity);
  }
  saveDatabaseEntity(dataDefinition,databaseEntity);
  Entity savedEntity=entityService.convertToGenericEntity(dataDefinition,databaseEntity);
  LOG.info(savedEntity + "String_Node_Str");
  for (  Entry<String,FieldDefinition> fieldEntry : dataDefinition.getFields().entrySet()) {
    if (fieldEntry.getValue().getType() instanceof HasManyType) {
      List<Entity> entities=(List<Entity>)genericEntity.getField(fieldEntry.getKey());
      HasManyType hasManyType=(HasManyType)fieldEntry.getValue().getType();
      if (entities == null || entities instanceof EntityListImpl) {
        savedEntity.setField(fieldEntry.getKey(),entities);
        continue;
      }
      List<Entity> savedEntities=saveHasManyEntities(alreadySavedEntities,newlySavedEntities,hasManyType.getJoinFieldName(),savedEntity.getId(),entities,(InternalDataDefinition)hasManyType.getDataDefinition());
      EntityList dbEntities=savedEntity.getHasManyField(fieldEntry.getKey());
      removeOrphans(savedEntities,(InternalDataDefinition)hasManyType.getDataDefinition(),dbEntities);
      savedEntity.setField(fieldEntry.getKey(),savedEntities);
    }
 else     if (fieldEntry.getValue().getType() instanceof TreeType) {
      List<Entity> entities=(List<Entity>)genericEntity.getField(fieldEntry.getKey());
      if (entities == null || entities instanceof EntityTreeImpl) {
        savedEntity.setField(fieldEntry.getKey(),entities);
        continue;
      }
      TreeType treeType=(TreeType)fieldEntry.getValue().getType();
      List<Entity> savedEntities=saveTreeEntities(alreadySavedEntities,newlySavedEntities,treeType.getJoinFieldName(),savedEntity.getId(),entities,(InternalDataDefinition)treeType.getDataDefinition(),null);
      savedEntity.setField(fieldEntry.getKey(),savedEntities);
    }
  }
  alreadySavedEntities.add(savedEntity);
  if (genericEntity.getId() == null && savedEntity.getId() != null) {
    newlySavedEntities.add(savedEntity);
  }
  return savedEntity;
}