<commit_before>package gex.jaxrs.provider;

import gex.commons.exception.DomainClassValidationException;
import gex.jaxrs.ApiResponse;
import gex.jaxrs.provider.support.SpringErrorsExtractor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.Errors;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import static org.springframework.util.ReflectionUtils.*;

/**
 * Created by domix on 12/23/14.
 */
@Slf4j
@Provider
public class DomainClassValidationExceptionMapper implements ExceptionMapper<DomainClassValidationException> {
  @Setter
  @Autowired
  private ApplicationContext applicationContext;
  @Setter
  @Autowired
  private ApiResponse apiResponse;

  @Override
  public Response toResponse(DomainClassValidationException exception) {
    log.debug(apiResponse.getClass().getName());
    Object entity = exception.getEntity();

    Errors errors = getErrors(entity);
    if (errors != null) {
      List<String> errorList = new SpringErrorsExtractor().extractError(errors, applicationContext);
      return apiResponse.unprocessableEntity(exception.getMessage(), errorList);
    }

    return apiResponse.unprocessableEntity(exception.getMessage());
  }

  private Errors getErrors(Object entity) {
    Errors errors = getErrorsFromField(entity);

    return errors != null ? errors : getErrorFromGetter(entity);
  }

  private Errors getErrorsFromField(Object entity) {
    Field errorsField = findField(entity.getClass(), "errors", Errors.class);
    Errors errors = null;
    if (errorsField != null) {
      errors = (Errors) getField(errorsField, entity);
    }
    return errors;
  }

  private Errors getErrorFromGetter(Object entity) {
    Errors errors = null;
    Method errorsMethod = findMethod(entity.getClass(), "getErrors", Errors.class);
    if (errorsMethod != null) {
      errors = (Errors) invokeMethod(errorsMethod, entity);
    }

    return errors;<commit_msg>making accesible field and method before accesing them<commit_after>package gex.jaxrs.provider;

import gex.commons.exception.DomainClassValidationException;
import gex.jaxrs.ApiResponse;
import gex.jaxrs.provider.support.SpringErrorsExtractor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.Errors;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import static org.springframework.util.ReflectionUtils.*;

/**
 * Created by domix on 12/23/14.
 */
@Slf4j
@Provider
public class DomainClassValidationExceptionMapper implements ExceptionMapper<DomainClassValidationException> {
  @Setter
  @Autowired
  private ApplicationContext applicationContext;
  @Setter
  @Autowired
  private ApiResponse apiResponse;

  @Override
  public Response toResponse(DomainClassValidationException exception) {
    log.debug(apiResponse.getClass().getName());
    Object entity = exception.getEntity();

    Errors errors = getErrors(entity);
    if (errors != null) {
      List<String> errorList = new SpringErrorsExtractor().extractError(errors, applicationContext);
      return apiResponse.unprocessableEntity(exception.getMessage(), errorList);
    }

    return apiResponse.unprocessableEntity(exception.getMessage());
  }

  private Errors getErrors(Object entity) {
    Errors errors = getErrorsFromField(entity);

    return errors != null ? errors : getErrorFromGetter(entity);
  }

  private Errors getErrorsFromField(Object entity) {
    Field errorsField = findField(entity.getClass(), "errors", Errors.class);
    Errors errors = null;
    if (errorsField != null) {
      makeAccessible(errorsField);
      errors = (Errors) getField(errorsField, entity);
    }
    return errors;
  }

  private Errors getErrorFromGetter(Object entity) {
    Errors errors = null;
    Method errorsMethod = findMethod(entity.getClass(), "getErrors", Errors.class);
    if (errorsMethod != null) {
      makeAccessible(errorsMethod);
      errors = (Errors) invokeMethod(errorsMethod, entity);
    }

    return errors;
{'fraction_non_alphanumeric': 0.10013291980505096, 'fraction_numerical': 0.003987594151528578, 'mean_word_length': 4.894255874673629, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405140', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1248, 'n_words': 276}