<commit_before>package org.folio.rest.annotations;

import java.util.Arrays;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.annotation.SuppressAjWarnings;

public aspect RestValidator {

  static private ValidatorFactory factory;

  static {
    factory = Validation.buildDefaultValidatorFactory();
  }

  static private ExecutableValidator getMethodValidator() {
    return factory.getValidator().forExecutables();
  }

  pointcut validatedMethodCall() : execution(@Validate * *(..));

  /**
   * Validates the method parameters.
   */
  @SuppressAjWarnings({"adviceDidNotMatch"})
  before() : validatedMethodCall() {

    MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();

    System.out.println("Validating call: with args " + methodSignature.getMethod() + Arrays.toString(thisJoinPoint.getArgs()));

    String[] params = methodSignature.getParameterNames();

    Set<? extends ConstraintViolation<?>> validationErrors = getMethodValidator().validateParameters(thisJoinPoint.getThis(),
        methodSignature.getMethod(), thisJoinPoint.getArgs());

    if (validationErrors.isEmpty()) {
      System.out.println("Valid call......");
    } else {
      System.out.println("Invalid call......");
      RuntimeException ex = buildValidationException(validationErrors, params);
      throw ex;
    }
  }

  /**
   * @param validationErrors
   *          The errors detected in a method/constructor call.
   * @return A RuntimeException with information about the detected validation<commit_msg>replace system.out in aspect with log.debug<commit_after>package org.folio.rest.annotations;

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

import java.util.Arrays;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;

import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.annotation.SuppressAjWarnings;

public aspect RestValidator {

  static private ValidatorFactory factory;
  static final Logger log = LoggerFactory.getLogger(RestValidator.class);
  
  static {
    factory = Validation.buildDefaultValidatorFactory();
  }

  static private ExecutableValidator getMethodValidator() {
    return factory.getValidator().forExecutables();
  }

  pointcut validatedMethodCall() : execution(@Validate * *(..));

  /**
   * Validates the method parameters.
   */
  @SuppressAjWarnings({"adviceDidNotMatch"})
  before() : validatedMethodCall() {

    MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();

    String[] params = methodSignature.getParameterNames();

    Set<? extends ConstraintViolation<?>> validationErrors = getMethodValidator().validateParameters(thisJoinPoint.getThis(),
        methodSignature.getMethod(), thisJoinPoint.getArgs());

    if (validationErrors.isEmpty()) {
      log.debug("Valid call: with args " + methodSignature.getMethod() + Arrays.toString(thisJoinPoint.getArgs()));
    } else {
      log.debug("Invalid call: with args " + methodSignature.getMethod() + Arrays.toString(thisJoinPoint.getArgs()));
      RuntimeException ex = buildValidationException(validationErrors, params);
      throw ex;
    }
  }

  /**
   * @param validationErrors
   *          The errors detected in a method/constructor call.
   * @return A RuntimeException with information about the detected validation
{'fraction_non_alphanumeric': 0.1081578947368421, 'fraction_numerical': 0.0, 'mean_word_length': 4.76783004552352, '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': '3754783', 'n_tokens_mistral': 984, 'n_tokens_neox': 971, 'n_words': 209}