Program: 63406
Project: 63406
Buggy source:
/** 
 * Execute <tt>SELECT</tt> operation on the database
 * @param xrService parent <code>XRService</code> that owns this <code>Operation</code>
 * @param invocation contains runtime argument values to be bound to the list of{@link Parameter}'s.
 * @return  result - the result of the underlying <tt>SELECT</tt> operation onthe database, or <code>null</code>.
 * @see  {@link Operation}
 */
@Override public Object invoke(XRServiceAdapter xrService,Invocation invocation){
  DatabaseQuery query=queryHandler.getDatabaseQuery();
  if (query.getProperty(DATABASEQUERY_STR) != null) {
    query=(DatabaseQuery)query.getProperty(DATABASEQUERY_STR);
  }
  if (query.getArguments().size() == 0) {
    int idx=0;
    for (    Parameter param : getParameters()) {
      query.addArgument(Integer.toString(++idx),Util.SCHEMA_2_CLASS.get(param.getType()));
      query.addArgumentValue(invocation.getParameter(param.getName()));
    }
  }
 else {
    List<Object> argVals=new ArrayList<Object>();
    for (    Parameter param : getParameters()) {
      argVals.add(invocation.getParameter(param.getName()));
    }
    query.setArgumentValues(argVals);
  }
  if (isSimpleXMLFormat() && query.isDataReadQuery()) {
    ((DataReadQuery)query).setResultType(DataReadQuery.MAP);
  }
  Object value=xrService.getORSession().getActiveSession().executeQuery(query);
  if (value != null) {
    if (result != null && (result.getType() == INT_QNAME || result.getType().equals(SXF_QNAME))) {
      if (value instanceof ArrayList && ((ArrayList)value).isEmpty()) {
        ((ArrayList)value).add(1);
      }
 else       if (value instanceof Vector && ((Vector)value).isEmpty()) {
        ((Vector)value).add(1);
      }
    }
    if (value instanceof ArrayList) {
      ArrayList returnedList=(ArrayList)value;
      if (returnedList.size() > 0 && returnedList.get(0) instanceof Object[]) {
        Object[] objs=(Object[])returnedList.get(0);
        if (isCollection()) {
          value=new ArrayList();
          for (          Object obj : objs) {
            ((ArrayList)value).add(obj);
          }
        }
 else {
          value=objs[0];
        }
      }
    }
    if (isSimpleXMLFormat()) {
      value=createSimpleXMLFormat(xrService,value);
    }
 else {
      if (!isCollection() && value instanceof Vector) {
        if (((Vector)value).isEmpty()) {
          return null;
        }
        value=((Vector)value).firstElement();
      }
      QName resultType=getResultType();
      if (resultType != null) {
        if (isAttachment() || (!isCollection() && resultType.equals(BASE_64_BINARY_QNAME))) {
          String mimeType=DEFAULT_ATTACHMENT_MIMETYPE;
          if (isAttachment() && result.getAttachment().getMimeType() != null) {
            mimeType=result.getAttachment().getMimeType();
          }
          if (value instanceof Blob) {
            value=((XMLConversionManager)xrService.getOXSession().getDatasourcePlatform().getConversionManager()).convertObject((Blob)value,ClassConstants.APBYTE);
          }
          return AttachmentHelper.buildAttachmentHandler((byte[])value,mimeType);
        }
        if (resultType.getNamespaceURI().equals(SCHEMA_URL)) {
          ValueObject vo=new ValueObject();
          vo.value=value;
          value=vo;
        }
 else {
          Object targetObject=value;
          if (xrService.descriptorsByQName.containsKey(resultType)) {
            XMLDescriptor xdesc=xrService.descriptorsByQName.get(resultType);
            ClassDescriptor desc=xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
            if (desc.isAggregateDescriptor() && !desc.isObjectRelationalDataTypeDescriptor() && !desc.isRelationalDescriptor()) {
              if (isCollection()) {
                XRDynamicEntity_CollectionWrapper xrCollWrapper=new XRDynamicEntity_CollectionWrapper();
                Vector<AbstractRecord> results=(Vector<AbstractRecord>)value;
                for (int i=0, len=results.size(); i < len; i++) {
                  Object o=desc.getObjectBuilder().buildNewInstance();
                  populateTargetObjectFromRecord(desc.getMappings(),results.get(i),o,(AbstractSession)xrService.getORSession());
                  xrCollWrapper.add(o);
                }
                targetObject=xrCollWrapper;
              }
 else {
                targetObject=desc.getObjectBuilder().buildNewInstance();
                populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)value,targetObject,(AbstractSession)xrService.getORSession());
              }
            }
 else             if (isCollection() && value instanceof Vector) {
              if (((Vector)value).size() > 0 && !(((Vector)value).get(0) instanceof AbstractRecord)) {
                return value;
              }
              XRDynamicEntity_CollectionWrapper xrCollWrapper=new XRDynamicEntity_CollectionWrapper();
              Vector<AbstractRecord> results=(Vector<AbstractRecord>)value;
              for (int i=0, len=results.size(); i < len; i++) {
                Object o=desc.getObjectBuilder().buildNewInstance();
                populateTargetObjectFromRecord(desc.getMappings(),results.get(i),o,(AbstractSession)xrService.getORSession());
                xrCollWrapper.add(o);
              }
              targetObject=xrCollWrapper;
            }
 else             if (value instanceof AbstractRecord) {
              targetObject=desc.getObjectBuilder().buildNewInstance();
              populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)value,targetObject,(AbstractSession)xrService.getORSession());
            }
          }
          if (value instanceof ArrayList) {
            XMLDescriptor xdesc=xrService.descriptorsByQName.get(resultType);
            ClassDescriptor desc=xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
            targetObject=desc.getObjectBuilder().buildNewInstance();
            Object[] objs=new Object[1];
            objs[0]=((ArrayList)value).get(0);
            DatabaseRecord dr=new DatabaseRecord();
            dr.add(new DatabaseField(ITEMS_STR),objs);
            populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)dr,targetObject,(AbstractSession)xrService.getORSession());
          }
          value=targetObject;
        }
      }
    }
  }
  return value;
}

Fixed version (ground truth):
/** 
 * Execute <tt>SELECT</tt> operation on the database
 * @param xrService parent <code>XRService</code> that owns this <code>Operation</code>
 * @param invocation contains runtime argument values to be bound to the list of{@link Parameter}'s.
 * @return  result - the result of the underlying <tt>SELECT</tt> operation onthe database, or <code>null</code>.
 * @see  {@link Operation}
 */
@Override public Object invoke(XRServiceAdapter xrService,Invocation invocation){
  DatabaseQuery query=queryHandler.getDatabaseQuery();
  if (query.getProperty(DATABASEQUERY_STR) != null) {
    query=(DatabaseQuery)query.getProperty(DATABASEQUERY_STR);
  }
  if (query.getArguments().size() == 0) {
    int idx=0;
    for (    Parameter param : getParameters()) {
      query.addArgument(Integer.toString(++idx),Util.SCHEMA_2_CLASS.get(param.getType()));
      query.addArgumentValue(invocation.getParameter(param.getName()));
    }
  }
 else {
    List<Object> argVals=new ArrayList<Object>();
    for (    Parameter param : getParameters()) {
      argVals.add(invocation.getParameter(param.getName()));
    }
    query.setArgumentValues(argVals);
  }
  if (isSimpleXMLFormat() && query.isDataReadQuery()) {
    ((DataReadQuery)query).setResultType(DataReadQuery.MAP);
  }
  Object value=xrService.getORSession().getActiveSession().executeQuery(query);
  if (value != null) {
    if (result != null && (result.getType() == INT_QNAME || result.getType().equals(SXF_QNAME))) {
      if (value instanceof ArrayList && ((ArrayList)value).isEmpty()) {
        ((ArrayList)value).add(1);
      }
 else       if (value instanceof Vector && ((Vector)value).isEmpty()) {
        ((Vector)value).add(1);
      }
    }
    if (value instanceof ArrayList) {
      ArrayList returnedList=(ArrayList)value;
      if (returnedList.size() > 0 && returnedList.get(0) instanceof Object[]) {
        Object[] objs=(Object[])returnedList.get(0);
        if (isCollection()) {
          value=new ArrayList();
          for (          Object obj : objs) {
            ((ArrayList)value).add(obj);
          }
        }
 else {
          value=objs[0];
        }
      }
    }
    if (isSimpleXMLFormat()) {
      value=createSimpleXMLFormat(xrService,value);
    }
 else {
      if (!isCollection() && value instanceof Vector) {
        if (((Vector)value).isEmpty()) {
          return null;
        }
        value=((Vector)value).firstElement();
      }
      QName resultType=getResultType();
      if (resultType != null) {
        if (isAttachment() || (!isCollection() && resultType.equals(BASE_64_BINARY_QNAME))) {
          String mimeType=DEFAULT_ATTACHMENT_MIMETYPE;
          if (isAttachment() && result.getAttachment().getMimeType() != null) {
            mimeType=result.getAttachment().getMimeType();
          }
          if (value instanceof Blob) {
            value=((XMLConversionManager)xrService.getOXSession().getDatasourcePlatform().getConversionManager()).convertObject(value,ClassConstants.APBYTE);
          }
          return AttachmentHelper.buildAttachmentHandler((byte[])value,mimeType);
        }
        if (resultType.getNamespaceURI().equals(SCHEMA_URL)) {
          ValueObject vo=new ValueObject();
          vo.value=value;
          value=vo;
        }
 else {
          Object targetObject=value;
          if (xrService.descriptorsByQName.containsKey(resultType)) {
            XMLDescriptor xdesc=xrService.descriptorsByQName.get(resultType);
            ClassDescriptor desc=xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
            if (desc.isAggregateDescriptor() && !desc.isObjectRelationalDataTypeDescriptor() && !desc.isRelationalDescriptor()) {
              if (isCollection()) {
                XRDynamicEntity_CollectionWrapper xrCollWrapper=new XRDynamicEntity_CollectionWrapper();
                Vector<AbstractRecord> results=(Vector<AbstractRecord>)value;
                for (int i=0, len=results.size(); i < len; i++) {
                  Object o=desc.getObjectBuilder().buildNewInstance();
                  populateTargetObjectFromRecord(desc.getMappings(),results.get(i),o,(AbstractSession)xrService.getORSession());
                  xrCollWrapper.add(o);
                }
                targetObject=xrCollWrapper;
              }
 else {
                targetObject=desc.getObjectBuilder().buildNewInstance();
                populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)value,targetObject,(AbstractSession)xrService.getORSession());
              }
            }
 else             if (isCollection() && value instanceof Vector) {
              if (((Vector)value).size() > 0 && !(((Vector)value).get(0) instanceof AbstractRecord)) {
                return value;
              }
              XRDynamicEntity_CollectionWrapper xrCollWrapper=new XRDynamicEntity_CollectionWrapper();
              Vector<AbstractRecord> results=(Vector<AbstractRecord>)value;
              for (int i=0, len=results.size(); i < len; i++) {
                Object o=desc.getObjectBuilder().buildNewInstance();
                populateTargetObjectFromRecord(desc.getMappings(),results.get(i),o,(AbstractSession)xrService.getORSession());
                xrCollWrapper.add(o);
              }
              targetObject=xrCollWrapper;
            }
 else             if (value instanceof AbstractRecord) {
              targetObject=desc.getObjectBuilder().buildNewInstance();
              populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)value,targetObject,(AbstractSession)xrService.getORSession());
            }
          }
          if (value instanceof ArrayList) {
            XMLDescriptor xdesc=xrService.descriptorsByQName.get(resultType);
            ClassDescriptor desc=xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
            targetObject=desc.getObjectBuilder().buildNewInstance();
            Object[] objs=new Object[1];
            objs[0]=((ArrayList)value).get(0);
            DatabaseRecord dr=new DatabaseRecord();
            dr.add(new DatabaseField(ITEMS_STR),objs);
            populateTargetObjectFromRecord(desc.getMappings(),(AbstractRecord)dr,targetObject,(AbstractSession)xrService.getORSession());
          }
          value=targetObject;
        }
      }
    }
  }
  return value;
}