Program: 67390
Project: 67390
Buggy source:
/** 
 * INTERNAL: Cache the bean's primary key fields so speed up creating of primary key objects and initialization of beans. Note, we have to re-look up the fields for the bean class since these fields may have been loaded with the wrong loader (thank you Kirk). If the key is compound, we also have to look up the fields for the key.
 */
protected KeyElementAccessor[] initializePrimaryKeyFields(Class keyClass,AbstractSession session){
  KeyElementAccessor[] pkAttributes=null;
  ClassDescriptor descriptor=this.getDescriptor();
  fieldToAccessorMap=new HashMap<DatabaseField,KeyElementAccessor>();
  int numberOfIDFields=descriptor.getPrimaryKeyFields().size();
  pkAttributes=new KeyElementAccessor[numberOfIDFields];
  Iterator attributesIter=descriptor.getPrimaryKeyFields().iterator();
  for (int i=0; attributesIter.hasNext(); i++) {
    DatabaseField field=(DatabaseField)attributesIter.next();
    List allMappings=new ArrayList(1);
    addReadOnlyMappings(descriptor,field,allMappings);
    addWritableMapping(descriptor,field,allMappings);
    Exception noSuchElementException=null;
    for (int index=(allMappings.size() - 1); index >= 0; --index) {
      DatabaseMapping mapping=(DatabaseMapping)allMappings.get(index);
      if (descriptor.hasDerivedId() && !mapping.isDerivedIdMapping()) {
        if (mapping.isAggregateMapping() && allMappings.size() > 1) {
          continue;
        }
      }
 else       if (mapping.isForeignReferenceMapping() && !mapping.isOneToOneMapping()) {
        continue;
      }
      if (mapping.isAggregateMapping()) {
        addReadOnlyMappings(mapping.getReferenceDescriptor(),field,allMappings);
        addWritableMapping(mapping.getReferenceDescriptor(),field,allMappings);
        allMappings.remove(mapping);
        index=allMappings.size();
      }
 else {
        String fieldName=(mapping.hasMappedByIdValue()) ? mapping.getMappedByIdValue() : mapping.getAttributeName();
        if (keyClass == null) {
          pkAttributes[i]=new KeyIsElementAccessor(fieldName,field,mapping);
          if (mapping.isDirectToFieldMapping()) {
            setPKClass(ConversionManager.getObjectClass(mapping.getAttributeClassification()));
          }
 else           if (mapping.isOneToOneMapping()) {
            ClassDescriptor refDescriptor=mapping.getReferenceDescriptor();
            refDescriptor.initialize(session);
            CMPPolicy refPolicy=refDescriptor.getCMPPolicy();
            setPKClass(refPolicy.getPKClass());
          }
          fieldToAccessorMap.put(field,pkAttributes[i]);
          noSuchElementException=null;
        }
 else {
          try {
            pkAttributes[i]=new FieldAccessor(getField(keyClass,fieldName),fieldName,field,mapping);
            fieldToAccessorMap.put(field,pkAttributes[i]);
            noSuchElementException=null;
          }
 catch (          NoSuchFieldException ex) {
            if (mapping.getGetMethodName() != null) {
              try {
                Method getMethod=null;
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                  try {
                    getMethod=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,mapping.getGetMethodName(),new Class[]{},true));
                  }
 catch (                  PrivilegedActionException exception) {
                    throw (NoSuchMethodException)exception.getException();
                  }
                }
 else {
                  getMethod=PrivilegedAccessHelper.getMethod(keyClass,mapping.getGetMethodName(),new Class[]{},true);
                }
                Method setMethod=null;
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                  try {
                    setMethod=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,mapping.getSetMethodName(),new Class[]{getMethod.getReturnType()},true));
                  }
 catch (                  PrivilegedActionException exception) {
                    throw (NoSuchMethodException)exception.getException();
                  }
                }
 else {
                  setMethod=PrivilegedAccessHelper.getMethod(keyClass,mapping.getSetMethodName(),new Class[]{getMethod.getReturnType()},true);
                }
                pkAttributes[i]=new PropertyAccessor(getMethod,setMethod,fieldName,field,mapping);
                this.fieldToAccessorMap.put(field,pkAttributes[i]);
                noSuchElementException=null;
              }
 catch (              NoSuchMethodException exs) {
                noSuchElementException=exs;
              }
            }
 else {
              noSuchElementException=ex;
            }
          }
        }
        if (mapping.isDerivedIdMapping() || noSuchElementException == null) {
          break;
        }
      }
    }
    if (noSuchElementException != null) {
      throw DescriptorException.errorUsingPrimaryKey(keyClass,getDescriptor(),noSuchElementException);
    }
  }
  return pkAttributes;
}

Fixed version (ground truth):
/** 
 * INTERNAL: Cache the bean's primary key fields so speed up creating of primary key objects and initialization of beans. Note, we have to re-look up the fields for the bean class since these fields may have been loaded with the wrong loader (thank you Kirk). If the key is compound, we also have to look up the fields for the key.
 */
protected KeyElementAccessor[] initializePrimaryKeyFields(Class keyClass,AbstractSession session){
  KeyElementAccessor[] pkAttributes=null;
  ClassDescriptor descriptor=this.getDescriptor();
  fieldToAccessorMap=new HashMap<DatabaseField,KeyElementAccessor>();
  int numberOfIDFields=descriptor.getPrimaryKeyFields().size();
  pkAttributes=new KeyElementAccessor[numberOfIDFields];
  Iterator attributesIter=descriptor.getPrimaryKeyFields().iterator();
  for (int i=0; attributesIter.hasNext(); i++) {
    DatabaseField field=(DatabaseField)attributesIter.next();
    List allMappings=new ArrayList(1);
    addReadOnlyMappings(descriptor,field,allMappings);
    addWritableMapping(descriptor,field,allMappings);
    Exception noSuchElementException=null;
    for (int index=(allMappings.size() - 1); index >= 0; --index) {
      DatabaseMapping mapping=(DatabaseMapping)allMappings.get(index);
      if (descriptor.hasDerivedId() && !mapping.isDerivedIdMapping()) {
        if (mapping.isAggregateMapping() && allMappings.size() > 1) {
          continue;
        }
      }
 else       if (mapping.isForeignReferenceMapping() && !mapping.isOneToOneMapping()) {
        continue;
      }
      if (mapping.isAggregateMapping()) {
        addReadOnlyMappings(mapping.getReferenceDescriptor(),field,allMappings);
        addWritableMapping(mapping.getReferenceDescriptor(),field,allMappings);
        allMappings.remove(mapping);
        index=allMappings.size();
      }
 else {
        String fieldName=(mapping.hasMappedByIdValue()) ? mapping.getMappedByIdValue() : mapping.getAttributeName();
        if (keyClass == null) {
          pkAttributes[i]=new KeyIsElementAccessor(fieldName,field,mapping);
          if (mapping.isDirectToFieldMapping()) {
            setPKClass(ConversionManager.getObjectClass(mapping.getAttributeClassification()));
          }
 else           if (mapping.isOneToOneMapping()) {
            ClassDescriptor refDescriptor=mapping.getReferenceDescriptor();
            refDescriptor.initialize(session);
            CMPPolicy refPolicy=refDescriptor.getCMPPolicy();
            setPKClass(refPolicy.getPKClass());
          }
          fieldToAccessorMap.put(field,pkAttributes[i]);
          noSuchElementException=null;
        }
 else {
          if (mapping.isOneToOneMapping()) {
            ClassDescriptor refDescriptor=mapping.getReferenceDescriptor();
            refDescriptor.initialize(session);
            CMPPolicy refPolicy=refDescriptor.getCMPPolicy();
            if ((refPolicy != null) && refPolicy.isCMP3Policy() && (refPolicy.getPKClass() == keyClass)) {
              OneToOneMapping refmapping=(OneToOneMapping)mapping;
              DatabaseField targetKey=refmapping.getSourceToTargetKeyFields().get(field);
              pkAttributes[i]=((CMP3Policy)refPolicy).fieldToAccessorMap.get(targetKey);
              this.fieldToAccessorMap.put(field,pkAttributes[i]);
              noSuchElementException=null;
              break;
            }
          }
          try {
            pkAttributes[i]=new FieldAccessor(getField(keyClass,fieldName),fieldName,field,mapping);
            fieldToAccessorMap.put(field,pkAttributes[i]);
            noSuchElementException=null;
          }
 catch (          NoSuchFieldException ex) {
            if (mapping.getGetMethodName() != null) {
              try {
                Method getMethod=null;
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                  try {
                    getMethod=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,mapping.getGetMethodName(),new Class[]{},true));
                  }
 catch (                  PrivilegedActionException exception) {
                    throw (NoSuchMethodException)exception.getException();
                  }
                }
 else {
                  getMethod=PrivilegedAccessHelper.getMethod(keyClass,mapping.getGetMethodName(),new Class[]{},true);
                }
                Method setMethod=null;
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                  try {
                    setMethod=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,mapping.getSetMethodName(),new Class[]{getMethod.getReturnType()},true));
                  }
 catch (                  PrivilegedActionException exception) {
                    throw (NoSuchMethodException)exception.getException();
                  }
                }
 else {
                  setMethod=PrivilegedAccessHelper.getMethod(keyClass,mapping.getSetMethodName(),new Class[]{getMethod.getReturnType()},true);
                }
                pkAttributes[i]=new PropertyAccessor(getMethod,setMethod,fieldName,field,mapping);
                this.fieldToAccessorMap.put(field,pkAttributes[i]);
                noSuchElementException=null;
              }
 catch (              NoSuchMethodException exs) {
                noSuchElementException=exs;
              }
            }
 else {
              noSuchElementException=ex;
            }
          }
        }
        if (mapping.isDerivedIdMapping() || noSuchElementException == null) {
          break;
        }
      }
    }
    if (noSuchElementException != null) {
      throw DescriptorException.errorUsingPrimaryKey(keyClass,getDescriptor(),noSuchElementException);
    }
  }
  return pkAttributes;
}