Program: 8498
Project: 8498
Buggy source:
/** 
 * Invoke a custom query method on a repository and page the results based on URL parameters supplied by the user or the default page size.
 * @param request
 * @param pageSort
 * @param uriBuilder
 * @param repository
 * @param query
 * @return
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IOException
 */
@SuppressWarnings({"String_Node_Str"}) @RequestMapping(value="String_Node_Str",method=RequestMethod.GET) @ResponseBody public ResponseEntity<?> query(ServletServerHttpRequest request,PagingAndSorting pageSort,UriComponentsBuilder uriBuilder,@PathVariable String repository,@PathVariable String query) throws InvocationTargetException, IllegalAccessException, IOException {
  URI baseUri=uriBuilder.build().toUri();
  RepositoryMetadata repoMeta=repositoryMetadataFor(repository);
  Repository repo=repoMeta.repository();
  RepositoryQueryMethod queryMethod=repoMeta.queryMethod(query);
  if (null == queryMethod) {
    return notFoundResponse(request);
  }
  Class<?>[] paramTypes=queryMethod.paramTypes();
  String[] paramNames=queryMethod.paramNames();
  Object[] paramVals=new Object[paramTypes.length];
  for (int i=0; i < paramVals.length; i++) {
    if (Pageable.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=pageSort;
      continue;
    }
 else     if (Sort.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=(null != pageSort ? pageSort.getSort() : null);
      continue;
    }
    String queryVal;
    if (null == (queryVal=request.getServletRequest().getParameter(paramNames[i]))) {
      continue;
    }
    if (String.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=queryVal;
    }
 else     if (hasRepositoryMetadataFor(paramTypes[i])) {
      RepositoryMetadata paramRepoMeta=repositoryMetadataFor(paramTypes[i]);
      Serializable id=stringToSerializable(queryVal,(Class<Serializable>)paramRepoMeta.entityMetadata().idAttribute().type());
      Object o=paramRepoMeta.repository().findOne(id);
      if (null == o) {
        return notFoundResponse(request);
      }
      paramVals[i]=o;
    }
 else     if (conversionService.canConvert(String.class,paramTypes[i])) {
      paramVals[i]=conversionService.convert(queryVal,paramTypes[i]);
    }
 else {
      try {
        paramVals[i]=objectMapper.readValue(queryVal,paramTypes[i]);
      }
 catch (      IOException e) {
        throw new IllegalArgumentException(e);
      }
    }
  }
  Object result;
  if (null == (result=queryMethod.method().invoke(repo,paramVals))) {
    return negotiateResponse(request,HttpStatus.OK,new HttpHeaders(),new Resources());
  }
  Resources resources=new Resources();
  Iterator entities=Collections.emptyList().iterator();
  if (result instanceof Collection) {
    entities=((Collection)result).iterator();
  }
 else   if (result instanceof Page) {
    Page page=(Page)result;
    if (page.hasContent()) {
      entities=page.iterator();
    }
    PageableResources pr=new PageableResources();
    pr.setResourceCount(page.getTotalElements());
    pr.setPaging(new PagingMetadata(page.getNumber() + 1,page.getTotalPages()));
    UriComponentsBuilder selfUri=UriComponentsBuilder.fromUri(baseUri).pathSegment(repository,"String_Node_Str",query);
    for (    String name : request.getServletRequest().getParameterMap().keySet()) {
      if (notPagingParam(name)) {
        selfUri.queryParam(name,request.getServletRequest().getParameter(name));
      }
    }
    URI nextPrevBase=selfUri.build().toUri();
    maybeAddPrevNextLink(nextPrevBase,repoMeta,pageSort,page,!page.isFirstPage() && page.hasPreviousPage(),page.getNumber(),"String_Node_Str",pr.getLinks());
    maybeAddPrevNextLink(nextPrevBase,repoMeta,pageSort,page,!page.isLastPage() && page.hasNextPage(),page.getNumber() + 2,"String_Node_Str",pr.getLinks());
    resources=pr;
  }
 else {
    entities=Collections.singletonList(result).iterator();
  }
  while (entities.hasNext()) {
    Object obj=entities.next();
    RepositoryMetadata elemRepoMeta;
    if (null == (elemRepoMeta=repositoryMetadataFor(obj.getClass()))) {
      resources.addResource(new Resource(obj));
      continue;
    }
    String id=elemRepoMeta.entityMetadata().idAttribute().get(obj).toString();
    if (shouldReturnLinks(request.getServletRequest().getHeader("String_Node_Str"))) {
      String rel=elemRepoMeta.rel() + "String_Node_Str" + elemRepoMeta.entityMetadata().type().getSimpleName();
      URI path=buildUri(baseUri,repository,id);
      resources.addLink(new SimpleLink(rel,path));
    }
 else {
      MapResource res=createResource(repoMeta.rel(),obj,repoMeta.entityMetadata(),buildUri(baseUri,repository,id));
      URI selfUri=buildUri(baseUri,repository,id);
      res.addLink(new SimpleLink(SELF,selfUri));
      resources.addResource(res);
    }
  }
  publishEvent(new BeforeRenderResourcesEvent(request,repoMeta,resources));
  return negotiateResponse(request,HttpStatus.OK,new HttpHeaders(),resources);
}

Fixed version (ground truth):
/** 
 * Invoke a custom query method on a repository and page the results based on URL parameters supplied by the user or the default page size.
 * @param request
 * @param pageSort
 * @param uriBuilder
 * @param repository
 * @param query
 * @return
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IOException
 */
@SuppressWarnings({"String_Node_Str"}) @RequestMapping(value="String_Node_Str",method=RequestMethod.GET) @ResponseBody public ResponseEntity<?> query(ServletServerHttpRequest request,PagingAndSorting pageSort,UriComponentsBuilder uriBuilder,@PathVariable String repository,@PathVariable String query) throws InvocationTargetException, IllegalAccessException, IOException {
  URI baseUri=uriBuilder.build().toUri();
  RepositoryMetadata repoMeta=repositoryMetadataFor(repository);
  Repository repo=repoMeta.repository();
  RepositoryQueryMethod queryMethod=repoMeta.queryMethod(query);
  if (null == queryMethod) {
    return notFoundResponse(request);
  }
  Class<?>[] paramTypes=queryMethod.paramTypes();
  String[] paramNames=queryMethod.paramNames();
  Object[] paramVals=new Object[paramTypes.length];
  for (int i=0; i < paramVals.length; i++) {
    if (Pageable.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=pageSort;
      continue;
    }
 else     if (Sort.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=(null != pageSort ? pageSort.getSort() : null);
      continue;
    }
    String queryVal;
    if (null == (queryVal=request.getServletRequest().getParameter(paramNames[i]))) {
      continue;
    }
    if (String.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=queryVal;
    }
 else     if (hasRepositoryMetadataFor(paramTypes[i])) {
      RepositoryMetadata paramRepoMeta=repositoryMetadataFor(paramTypes[i]);
      Serializable id=stringToSerializable(queryVal,(Class<Serializable>)paramRepoMeta.entityMetadata().idAttribute().type());
      Object o=paramRepoMeta.repository().findOne(id);
      if (null == o) {
        return notFoundResponse(request);
      }
      paramVals[i]=o;
    }
 else     if (conversionService.canConvert(String.class,paramTypes[i])) {
      paramVals[i]=conversionService.convert(queryVal,paramTypes[i]);
    }
 else {
      try {
        paramVals[i]=objectMapper.readValue(queryVal,paramTypes[i]);
      }
 catch (      IOException e) {
        throw new IllegalArgumentException(e);
      }
    }
  }
  Object result;
  if (null == (result=queryMethod.method().invoke(repo,paramVals))) {
    return negotiateResponse(request,HttpStatus.OK,new HttpHeaders(),new Resources());
  }
  Resources resources=new Resources();
  Iterator entities=Collections.emptyList().iterator();
  if (result instanceof Collection) {
    entities=((Collection)result).iterator();
  }
 else   if (result instanceof Page) {
    Page page=(Page)result;
    if (page.hasContent()) {
      entities=page.iterator();
    }
    PageableResources pr=new PageableResources();
    pr.setResourceCount(page.getTotalElements());
    pr.setPaging(new PagingMetadata(page.getNumber() + 1,page.getTotalPages()));
    UriComponentsBuilder selfUri=UriComponentsBuilder.fromUri(baseUri).pathSegment(repository,"String_Node_Str",query);
    for (    String name : request.getServletRequest().getParameterMap().keySet()) {
      if (notPagingParam(name)) {
        selfUri.queryParam(name,request.getServletRequest().getParameter(name));
      }
    }
    URI nextPrevBase=selfUri.build().toUri();
    maybeAddPrevNextLink(nextPrevBase,repoMeta,pageSort,page,!page.isFirstPage() && page.hasPreviousPage(),page.getNumber(),"String_Node_Str",pr.getLinks());
    maybeAddPrevNextLink(nextPrevBase,repoMeta,pageSort,page,!page.isLastPage() && page.hasNextPage(),page.getNumber() + 2,"String_Node_Str",pr.getLinks());
    resources=pr;
  }
 else {
    entities=Collections.singletonList(result).iterator();
  }
  while (entities.hasNext()) {
    Object obj=entities.next();
    RepositoryMetadata elemRepoMeta;
    if (null == (elemRepoMeta=repositoryMetadataFor(obj.getClass()))) {
      resources.addResource(new Resource(obj));
      continue;
    }
    String id=elemRepoMeta.entityMetadata().idAttribute().get(obj).toString();
    if (shouldReturnLinks(request.getServletRequest().getHeader("String_Node_Str"))) {
      String rel=elemRepoMeta.rel() + "String_Node_Str" + elemRepoMeta.entityMetadata().type().getSimpleName();
      URI path=buildUri(baseUri,repository,id);
      resources.addLink(new SimpleLink(rel,path));
    }
 else {
      URI selfUri=buildUri(baseUri,repository,id);
      MapResource res=createResource(repoMeta.rel(),obj,repoMeta.entityMetadata(),selfUri);
      res.addLink(new SimpleLink(SELF,selfUri));
      resources.addResource(res);
    }
  }
  publishEvent(new BeforeRenderResourcesEvent(request,repoMeta,resources));
  return negotiateResponse(request,HttpStatus.OK,new HttpHeaders(),resources);
}