Program: 8488
Project: 8488
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();
  BASE_URI.set(baseUri);
  RepositoryMetadata repoMeta=repositoryMetadataFor(repository);
  Repository repo=repoMeta.repository();
  RepositoryQueryMethod queryMethod=repoMeta.queryMethod(query);
  if (null == queryMethod) {
    return notFoundResponse(request);
  }
  Annotation[][] annotations=queryMethod.method().getParameterAnnotations();
  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[] queryVals;
    if (null == (queryVals=request.getServletRequest().getParameterValues(paramNames[i]))) {
      continue;
    }
    Class<? extends Converter<String[],?>> converter=null;
    for (    Annotation anno : annotations[i]) {
      if (ConvertWith.class.isAssignableFrom(anno.getClass())) {
        converter=((ConvertWith)anno).value();
        break;
      }
    }
    String firstVal=(queryVals.length > 0 ? queryVals[0] : null);
    if (hasRepositoryMetadataFor(paramTypes[i])) {
      RepositoryMetadata paramRepoMeta=repositoryMetadataFor(paramTypes[i]);
      Serializable id=stringToSerializable(firstVal,(Class<Serializable>)paramRepoMeta.entityMetadata().idAttribute().type());
      Object o=paramRepoMeta.repository().findOne(id);
      if (null == o) {
        return notFoundResponse(request);
      }
      paramVals[i]=o;
    }
 else     if (null != converter) {
      try {
        paramVals[i]=converter.newInstance().convert(queryVals);
      }
 catch (      InstantiationException e) {
        throw new IllegalArgumentException(e);
      }
    }
 else     if (String.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=firstVal;
    }
 else     if (conversionService.canConvert(String.class,paramTypes[i])) {
      paramVals[i]=conversionService.convert(firstVal,paramTypes[i]);
    }
 else {
      try {
        paramVals[i]=objectMapper.readValue(firstVal,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 ResourceSet());
  }
  ResourceSet resources=new ResourceSet();
  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();
    }
    PageableResourceSet pr=new PageableResourceSet();
    pr.setPaging(new PagingMetadata(page.getNumber() + 1,page.getSize(),page.getTotalPages(),page.getTotalElements()));
    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 ResourceLink(rel,path));
    }
 else {
      URI selfUri=buildUri(baseUri,repository,id);
      MapResource res=createResource(repoMeta.rel(),obj,repoMeta.entityMetadata(),selfUri);
      res.addLink(new ResourceLink(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();
  BASE_URI.set(baseUri);
  RepositoryMetadata repoMeta=repositoryMetadataFor(repository);
  Repository repo=repoMeta.repository();
  RepositoryQueryMethod queryMethod=repoMeta.queryMethod(query);
  if (null == queryMethod) {
    return notFoundResponse(request);
  }
  Annotation[][] annotations=queryMethod.method().getParameterAnnotations();
  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[] queryVals;
    if (null == (queryVals=request.getServletRequest().getParameterValues(paramNames[i]))) {
      continue;
    }
    TypeDescriptor stringTypeDesc=TypeDescriptor.valueOf(String[].class);
    MethodParameter methodParam=new MethodParameter(queryMethod.method(),i);
    TypeDescriptor targetTypeDesc=new TypeDescriptor(methodParam);
    Class<? extends Converter<String[],?>> converter=null;
    for (    Annotation anno : annotations[i]) {
      if (ConvertWith.class.isAssignableFrom(anno.getClass())) {
        converter=((ConvertWith)anno).value();
        break;
      }
    }
    String firstVal=(queryVals.length > 0 ? queryVals[0] : null);
    if (hasRepositoryMetadataFor(paramTypes[i])) {
      RepositoryMetadata paramRepoMeta=repositoryMetadataFor(paramTypes[i]);
      Serializable id=stringToSerializable(firstVal,(Class<Serializable>)paramRepoMeta.entityMetadata().idAttribute().type());
      Object o=paramRepoMeta.repository().findOne(id);
      if (null == o) {
        return notFoundResponse(request);
      }
      paramVals[i]=o;
    }
 else     if (null != converter) {
      try {
        paramVals[i]=converter.newInstance().convert(queryVals);
      }
 catch (      InstantiationException e) {
        throw new IllegalArgumentException(e);
      }
    }
 else     if (String.class.isAssignableFrom(paramTypes[i])) {
      paramVals[i]=firstVal;
    }
 else     if (conversionService.canConvert(stringTypeDesc,targetTypeDesc)) {
      paramVals[i]=conversionService.convert(queryVals,stringTypeDesc,targetTypeDesc);
    }
 else {
      try {
        paramVals[i]=objectMapper.readValue(firstVal,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 ResourceSet());
  }
  ResourceSet resources=new ResourceSet();
  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();
    }
    PageableResourceSet pr=new PageableResourceSet();
    pr.setPaging(new PagingMetadata(page.getNumber() + 1,page.getSize(),page.getTotalPages(),page.getTotalElements()));
    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 ResourceLink(rel,path));
    }
 else {
      URI selfUri=buildUri(baseUri,repository,id);
      MapResource res=createResource(repoMeta.rel(),obj,repoMeta.entityMetadata(),selfUri);
      res.addLink(new ResourceLink(SELF,selfUri));
      resources.addResource(res);
    }
  }
  publishEvent(new BeforeRenderResourcesEvent(request,repoMeta,resources));
  return negotiateResponse(request,HttpStatus.OK,new HttpHeaders(),resources);
}