Program: 67606
Project: 67606
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){
  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=descriptor.getObjectBuilder().getReadOnlyMappingsForField(field);
    if (allMappings == null) {
      allMappings=new Vector(1);
    }
    allMappings.add(descriptor.getObjectBuilder().getMappingForField(field));
    DatabaseMapping mapping=null;
    Exception elementIsFound=null;
    for (int index=(allMappings.size() - 1); index >= 0; --index) {
      mapping=(DatabaseMapping)allMappings.get(index);
      if (descriptor.isIDSpecified() && !mapping.isIDMapping()) {
        continue;
      }
 else       if (mapping.isForeignReferenceMapping() && !mapping.isOneToOneMapping()) {
        continue;
      }
 else       if (mapping.isAggregateMapping()) {
        ObjectBuilder builder=mapping.getReferenceDescriptor().getObjectBuilder();
        List aggregateMappings=builder.getReadOnlyMappingsForField(field);
        if ((aggregateMappings != null) && (!aggregateMappings.isEmpty())) {
          allMappings.addAll(aggregateMappings);
        }
        DatabaseMapping writableMapping=builder.getMappingForField(field);
        if (writableMapping != null) {
          allMappings.add(writableMapping);
        }
        allMappings.remove(mapping);
        index=allMappings.size();
        continue;
      }
      String fieldName=mapping.getAttributeName();
      if (keyClass == null) {
        pkAttributes[i]=new KeyIsElementAccessor(fieldName,field,mapping);
        if (mapping.isDirectToFieldMapping()) {
          setPKClass(ConversionManager.getObjectClass(mapping.getAttributeClassification()));
        }
 else         if (mapping.isOneToOneMapping()) {
          CMPPolicy refPolicy=mapping.getReferenceDescriptor().getCMPPolicy();
          setPKClass(refPolicy.getPKClass());
        }
        fieldToAccessorMap.put(field,pkAttributes[i]);
        elementIsFound=null;
      }
 else {
        try {
          Field keyField=null;
          if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            try {
              keyField=(Field)AccessController.doPrivileged(new PrivilegedGetField(keyClass,fieldName,true));
            }
 catch (            PrivilegedActionException exception) {
              throw (NoSuchFieldException)exception.getException();
            }
          }
 else {
            keyField=PrivilegedAccessHelper.getField(keyClass,fieldName,true);
          }
          pkAttributes[i]=new FieldAccessor(keyField,fieldName,field,mapping);
          fieldToAccessorMap.put(field,pkAttributes[i]);
          elementIsFound=null;
        }
 catch (        NoSuchFieldException ex) {
          StringBuffer buffer=new StringBuffer();
          buffer.append("String_Node_Str");
          buffer.append(fieldName.substring(0,1).toUpperCase());
          buffer.append(fieldName.substring(1));
          try {
            Method method=null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
              try {
                method=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,buffer.toString(),new Class[]{},true));
              }
 catch (              PrivilegedActionException exception) {
                throw (NoSuchMethodException)exception.getException();
              }
            }
 else {
              method=PrivilegedAccessHelper.getMethod(keyClass,buffer.toString(),new Class[]{},true);
            }
            pkAttributes[i]=new PropertyAccessor(method,fieldName,field,mapping);
            fieldToAccessorMap.put(field,pkAttributes[i]);
            elementIsFound=null;
          }
 catch (          NoSuchMethodException exs) {
            elementIsFound=exs;
          }
        }
      }
      if (mapping.isIDMapping() || (elementIsFound == null)) {
        break;
      }
    }
    if (elementIsFound != null) {
      throw DescriptorException.errorUsingPrimaryKey(keyClass,getDescriptor(),elementIsFound);
    }
  }
  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=descriptor.getObjectBuilder().getReadOnlyMappingsForField(field);
    if (allMappings == null) {
      allMappings=new Vector(1);
    }
    allMappings.add(descriptor.getObjectBuilder().getMappingForField(field));
    DatabaseMapping mapping=null;
    Exception elementIsFound=null;
    for (int index=(allMappings.size() - 1); index >= 0; --index) {
      mapping=(DatabaseMapping)allMappings.get(index);
      if (descriptor.isIDSpecified() && !mapping.isIDMapping()) {
        continue;
      }
 else       if (mapping.isForeignReferenceMapping() && !mapping.isOneToOneMapping()) {
        continue;
      }
 else       if (mapping.isAggregateMapping()) {
        ObjectBuilder builder=mapping.getReferenceDescriptor().getObjectBuilder();
        List aggregateMappings=builder.getReadOnlyMappingsForField(field);
        if ((aggregateMappings != null) && (!aggregateMappings.isEmpty())) {
          allMappings.addAll(aggregateMappings);
        }
        DatabaseMapping writableMapping=builder.getMappingForField(field);
        if (writableMapping != null) {
          allMappings.add(writableMapping);
        }
        allMappings.remove(mapping);
        index=allMappings.size();
        continue;
      }
      String fieldName=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]);
        elementIsFound=null;
      }
 else {
        try {
          Field keyField=null;
          if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            try {
              keyField=(Field)AccessController.doPrivileged(new PrivilegedGetField(keyClass,fieldName,true));
            }
 catch (            PrivilegedActionException exception) {
              throw (NoSuchFieldException)exception.getException();
            }
          }
 else {
            keyField=PrivilegedAccessHelper.getField(keyClass,fieldName,true);
          }
          pkAttributes[i]=new FieldAccessor(keyField,fieldName,field,mapping);
          fieldToAccessorMap.put(field,pkAttributes[i]);
          elementIsFound=null;
        }
 catch (        NoSuchFieldException ex) {
          StringBuffer buffer=new StringBuffer();
          buffer.append("String_Node_Str");
          buffer.append(fieldName.substring(0,1).toUpperCase());
          buffer.append(fieldName.substring(1));
          try {
            Method method=null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
              try {
                method=AccessController.doPrivileged(new PrivilegedGetMethod(keyClass,buffer.toString(),new Class[]{},true));
              }
 catch (              PrivilegedActionException exception) {
                throw (NoSuchMethodException)exception.getException();
              }
            }
 else {
              method=PrivilegedAccessHelper.getMethod(keyClass,buffer.toString(),new Class[]{},true);
            }
            pkAttributes[i]=new PropertyAccessor(method,fieldName,field,mapping);
            fieldToAccessorMap.put(field,pkAttributes[i]);
            elementIsFound=null;
          }
 catch (          NoSuchMethodException exs) {
            elementIsFound=exs;
          }
        }
      }
      if (mapping.isIDMapping() || (elementIsFound == null)) {
        break;
      }
    }
    if (elementIsFound != null) {
      throw DescriptorException.errorUsingPrimaryKey(keyClass,getDescriptor(),elementIsFound);
    }
  }
  return pkAttributes;
}