Program: 67422
Project: 67422
Buggy source:
/** 
 * Return an instance of the receivers javaClass. Set the attributes of an instance from the values stored in the database row.
 */
protected Object buildObject(ObjectBuildingQuery query,AbstractRecord databaseRow,AbstractSession session,Vector primaryKey,ClassDescriptor concreteDescriptor,JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
  Object domainObject=null;
  CacheKey cacheKey=null;
  boolean cacheHit=true;
  try {
    if (query.shouldMaintainCache()) {
      if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || (joinManager != null))) {
        cacheKey=session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
        domainObject=cacheKey.getObject();
        int counter=0;
        while ((domainObject == null) && (counter < 1000)) {
          if (cacheKey.getMutex().getActiveThread() == Thread.currentThread()) {
            break;
          }
          cacheKey.releaseDeferredLock();
          try {
            Thread.sleep(10);
          }
 catch (          InterruptedException exception) {
          }
          cacheKey=session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
          domainObject=cacheKey.getObject();
          counter++;
        }
        if (counter == 1000) {
          throw ConcurrencyException.maxTriesLockOnBuildObjectExceded(cacheKey.getMutex().getActiveThread(),Thread.currentThread());
        }
      }
 else {
        cacheKey=session.getIdentityMapAccessorInstance().acquireLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
        domainObject=cacheKey.getObject();
      }
    }
    if (domainObject == null) {
      cacheHit=false;
      if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) {
        domainObject=((ReadObjectQuery)query).getSelectionObject();
      }
 else {
        domainObject=concreteDescriptor.getObjectBuilder().buildNewInstance();
      }
      if (query.shouldMaintainCache()) {
        cacheKey.setObject(domainObject);
        copyQueryInfoToCacheKey(cacheKey,query,databaseRow,session,concreteDescriptor);
      }
      concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,false);
      if (query.shouldMaintainCache()) {
        if ((query.getFetchGroup() != null) && concreteDescriptor.hasFetchGroupManager()) {
          concreteDescriptor.getFetchGroupManager().setObjectFetchGroup(domainObject,query.getFetchGroup(),session);
        }
      }
      if (domainObject instanceof PersistenceEntity) {
        ((PersistenceEntity)domainObject)._persistence_setCacheKey(cacheKey);
        ((PersistenceEntity)domainObject)._persistence_setPKVector(primaryKey);
      }
    }
 else {
      if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) {
        copyInto(domainObject,((ReadObjectQuery)query).getSelectionObject());
        domainObject=((ReadObjectQuery)query).getSelectionObject();
      }
      boolean isInvalidated=concreteDescriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey,query.getExecutionTime());
      if ((cacheKey.getMutex().getActiveThread() == Thread.currentThread()) && ((query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache() || isInvalidated) && ((cacheKey.getLastUpdatedQueryId() != query.getQueryId()) && !cacheKey.getMutex().isLockedByMergeManager()))) {
        if (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject)) {
          cacheHit=false;
          revertFetchGroupData(domainObject,concreteDescriptor,cacheKey,(query),joinManager,databaseRow,session);
        }
 else {
          boolean refreshRequired=true;
          if (concreteDescriptor.usesOptimisticLocking()) {
            OptimisticLockingPolicy policy=concreteDescriptor.getOptimisticLockingPolicy();
            Object cacheValue=policy.getValueToPutInCache(databaseRow,session);
            if (concreteDescriptor.shouldOnlyRefreshCacheIfNewerVersion()) {
              refreshRequired=policy.isNewerVersion(databaseRow,domainObject,primaryKey,session);
              if (!refreshRequired) {
                cacheKey.setReadTime(query.getExecutionTime());
              }
            }
            if (refreshRequired) {
              cacheKey.setWriteLockValue(cacheValue);
            }
          }
          if (refreshRequired) {
            cacheHit=false;
            cacheKey.setLastUpdatedQueryId(query.getQueryId());
            concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,true);
            cacheKey.setReadTime(query.getExecutionTime());
          }
        }
      }
 else       if (concreteDescriptor.hasFetchGroupManager() && (concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject) && (!concreteDescriptor.getFetchGroupManager().isObjectValidForFetchGroup(domainObject,query.getFetchGroup())))) {
        cacheHit=false;
        concreteDescriptor.getFetchGroupManager().unionFetchGroupIntoObject(domainObject,query.getFetchGroup(),session);
        concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,false);
      }
 else       if (joinManager != null && joinManager.hasJoinedAttributeExpressions()) {
        List joinExpressions=joinManager.getJoinedAttributeExpressions();
        int size=joinExpressions.size();
        for (int index=0; index < size; index++) {
          QueryKeyExpression queryKeyExpression=(QueryKeyExpression)joinExpressions.get(index);
          if (queryKeyExpression.getBaseExpression().isExpressionBuilder()) {
            DatabaseMapping mapping=getMappingForAttributeName(queryKeyExpression.getName());
            if (mapping == null) {
              throw ValidationException.missingMappingForAttribute(concreteDescriptor,queryKeyExpression.getName(),toString());
            }
 else {
              Object attributeValue=mapping.getAttributeValueFromObject(domainObject);
              if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping)mapping).usesIndirection()&& (!((ForeignReferenceMapping)mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                mapping.readFromRowIntoObject(databaseRow,joinManager,domainObject,query,query.getExecutionSession());
              }
            }
          }
        }
      }
 else       if (query.isReadAllQuery() && ((ReadAllQuery)query).hasBatchReadAttributes()) {
        List batchExpressions=((ReadAllQuery)query).getBatchReadAttributeExpressions();
        int size=batchExpressions.size();
        for (int index=0; index < size; index++) {
          QueryKeyExpression queryKeyExpression=(QueryKeyExpression)batchExpressions.get(index);
          if (queryKeyExpression.getBaseExpression().isExpressionBuilder()) {
            DatabaseMapping mapping=getMappingForAttributeName(queryKeyExpression.getName());
            if (mapping == null) {
              throw ValidationException.missingMappingForAttribute(concreteDescriptor,queryKeyExpression.getName(),this.toString());
            }
 else {
              Object attributeValue=mapping.getAttributeValueFromObject(domainObject);
              if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping)mapping).usesIndirection()&& (!((ForeignReferenceMapping)mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                mapping.readFromRowIntoObject(databaseRow,joinManager,domainObject,query,query.getExecutionSession());
              }
            }
          }
        }
      }
    }
  }
  finally {
    if (query.shouldMaintainCache() && (cacheKey != null)) {
      if (cacheKey.getObject() != null) {
        cacheKey.updateAccess();
      }
      if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || (joinManager != null))) {
        cacheKey.releaseDeferredLock();
      }
 else {
        cacheKey.release();
      }
    }
  }
  if (!cacheHit) {
    concreteDescriptor.getObjectBuilder().instantiateEagerMappings(domainObject,session);
  }
  return domainObject;
}

Fixed version (ground truth):
/** 
 * Return an instance of the receivers javaClass. Set the attributes of an instance from the values stored in the database row.
 */
protected Object buildObject(ObjectBuildingQuery query,AbstractRecord databaseRow,AbstractSession session,Vector primaryKey,ClassDescriptor concreteDescriptor,JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
  Object domainObject=null;
  CacheKey cacheKey=null;
  boolean cacheHit=true;
  try {
    if (query.shouldMaintainCache()) {
      if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || (joinManager != null))) {
        cacheKey=session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
        domainObject=cacheKey.getObject();
        int counter=0;
        while ((domainObject == null) && (counter < 1000)) {
          if (cacheKey.getMutex().getActiveThread() == Thread.currentThread()) {
            break;
          }
          cacheKey.releaseDeferredLock();
          try {
            Thread.sleep(10);
          }
 catch (          InterruptedException exception) {
          }
          cacheKey=session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
          domainObject=cacheKey.getObject();
          counter++;
        }
        if (counter == 1000) {
          throw ConcurrencyException.maxTriesLockOnBuildObjectExceded(cacheKey.getMutex().getActiveThread(),Thread.currentThread());
        }
      }
 else {
        cacheKey=session.getIdentityMapAccessorInstance().acquireLock(primaryKey,concreteDescriptor.getJavaClass(),concreteDescriptor);
        domainObject=cacheKey.getObject();
      }
    }
    if (domainObject == null) {
      cacheHit=false;
      if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) {
        domainObject=((ReadObjectQuery)query).getSelectionObject();
      }
 else {
        domainObject=concreteDescriptor.getObjectBuilder().buildNewInstance();
      }
      if (query.shouldMaintainCache()) {
        cacheKey.setObject(domainObject);
        copyQueryInfoToCacheKey(cacheKey,query,databaseRow,session,concreteDescriptor);
      }
      concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,false);
      if (query.shouldMaintainCache()) {
        if ((query.getFetchGroup() != null) && concreteDescriptor.hasFetchGroupManager()) {
          concreteDescriptor.getFetchGroupManager().setObjectFetchGroup(domainObject,query.getFetchGroup(),session);
        }
      }
      if (domainObject instanceof PersistenceEntity) {
        ((PersistenceEntity)domainObject)._persistence_setCacheKey(cacheKey);
        ((PersistenceEntity)domainObject)._persistence_setPKVector(primaryKey);
      }
    }
 else {
      if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) {
        copyInto(domainObject,((ReadObjectQuery)query).getSelectionObject());
        domainObject=((ReadObjectQuery)query).getSelectionObject();
      }
      boolean isInvalidated=concreteDescriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey,query.getExecutionTime());
      if ((cacheKey.getMutex().getActiveThread() == Thread.currentThread()) && ((query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache() || isInvalidated) && ((cacheKey.getLastUpdatedQueryId() != query.getQueryId()) && !cacheKey.getMutex().isLockedByMergeManager()))) {
        if (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject)) {
          cacheHit=false;
          revertFetchGroupData(domainObject,concreteDescriptor,cacheKey,(query),joinManager,databaseRow,session);
        }
 else {
          boolean refreshRequired=true;
          if (concreteDescriptor.usesOptimisticLocking()) {
            OptimisticLockingPolicy policy=concreteDescriptor.getOptimisticLockingPolicy();
            Object cacheValue=policy.getValueToPutInCache(databaseRow,session);
            if (concreteDescriptor.shouldOnlyRefreshCacheIfNewerVersion()) {
              refreshRequired=policy.isNewerVersion(databaseRow,domainObject,primaryKey,session);
              if (!refreshRequired) {
                cacheKey.setReadTime(query.getExecutionTime());
              }
            }
            if (refreshRequired) {
              cacheKey.setWriteLockValue(cacheValue);
            }
          }
          if (refreshRequired) {
            cacheHit=false;
            cacheKey.setLastUpdatedQueryId(query.getQueryId());
            cacheKey.setReadTime(query.getExecutionTime());
            concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,true);
          }
        }
      }
 else       if (concreteDescriptor.hasFetchGroupManager() && (concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject) && (!concreteDescriptor.getFetchGroupManager().isObjectValidForFetchGroup(domainObject,query.getFetchGroup())))) {
        cacheHit=false;
        concreteDescriptor.getFetchGroupManager().unionFetchGroupIntoObject(domainObject,query.getFetchGroup(),session);
        concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject,databaseRow,query,joinManager,false);
      }
 else       if (joinManager != null && joinManager.hasJoinedAttributeExpressions()) {
        List joinExpressions=joinManager.getJoinedAttributeExpressions();
        int size=joinExpressions.size();
        for (int index=0; index < size; index++) {
          QueryKeyExpression queryKeyExpression=(QueryKeyExpression)joinExpressions.get(index);
          if (queryKeyExpression.getBaseExpression().isExpressionBuilder()) {
            DatabaseMapping mapping=getMappingForAttributeName(queryKeyExpression.getName());
            if (mapping == null) {
              throw ValidationException.missingMappingForAttribute(concreteDescriptor,queryKeyExpression.getName(),toString());
            }
 else {
              Object attributeValue=mapping.getAttributeValueFromObject(domainObject);
              if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping)mapping).usesIndirection()&& (!((ForeignReferenceMapping)mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                mapping.readFromRowIntoObject(databaseRow,joinManager,domainObject,query,query.getExecutionSession());
              }
            }
          }
        }
      }
 else       if (query.isReadAllQuery() && ((ReadAllQuery)query).hasBatchReadAttributes()) {
        List batchExpressions=((ReadAllQuery)query).getBatchReadAttributeExpressions();
        int size=batchExpressions.size();
        for (int index=0; index < size; index++) {
          QueryKeyExpression queryKeyExpression=(QueryKeyExpression)batchExpressions.get(index);
          if (queryKeyExpression.getBaseExpression().isExpressionBuilder()) {
            DatabaseMapping mapping=getMappingForAttributeName(queryKeyExpression.getName());
            if (mapping == null) {
              throw ValidationException.missingMappingForAttribute(concreteDescriptor,queryKeyExpression.getName(),this.toString());
            }
 else {
              Object attributeValue=mapping.getAttributeValueFromObject(domainObject);
              if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping)mapping).usesIndirection()&& (!((ForeignReferenceMapping)mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                mapping.readFromRowIntoObject(databaseRow,joinManager,domainObject,query,query.getExecutionSession());
              }
            }
          }
        }
      }
    }
  }
  finally {
    if (query.shouldMaintainCache() && (cacheKey != null)) {
      if (cacheKey.getObject() != null) {
        cacheKey.updateAccess();
      }
      if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || (joinManager != null))) {
        cacheKey.releaseDeferredLock();
      }
 else {
        cacheKey.release();
      }
    }
  }
  if (!cacheHit) {
    concreteDescriptor.getObjectBuilder().instantiateEagerMappings(domainObject,session);
  }
  return domainObject;
}