<commit_before>package com.cosium.spring.data.jpa.entity.graph.repository.support;

import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph;
import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraphQueryHint;
import com.cosium.spring.data.jpa.entity.graph.domain2.NamedEntityGraph;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.InapplicableEntityGraphException;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.MultipleDefaultEntityGraphException;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.MultipleEntityGraphException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import javax.persistence.EntityManager;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.ResolvableType;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;

/**
 * Captures {@link EntityGraph} on repositories method calls. Created on 22/11/16.
 *
 * @author Reda.Housni-Alaoui
 */
class RepositoryMethodEntityGraphExtractor implements RepositoryProxyPostProcessor {

  private static final Logger LOG =
      LoggerFactory.getLogger(RepositoryMethodEntityGraphExtractor.class);

  private static final ThreadLocal<JpaEntityGraphMethodInterceptor> CURRENT_REPOSITORY =
      new NamedThreadLocal<>("Thread local holding the current repository");

  private final EntityManager entityManager;

  public RepositoryMethodEntityGraphExtractor(EntityManager entityManager) {
    this.entityManager = entityManager;
  }

  public static EntityGraphQueryHintCandidate getCurrentJpaEntityGraph() {
    JpaEntityGraphMethodInterceptor currentRepository = CURRENT_REPOSITORY.get();
    if (currentRepository == null) {
      return null;
    }
    return currentRepository.getCurrentJpaEntityGraph();
  }

  @Override
  public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
    factory.addAdvice(
        new JpaEntityGraphMethodInterceptor(entityManager, repositoryInformation.getDomainType()));
  }

  private static class JpaEntityGraphMethodInterceptor implements MethodInterceptor {

    private static final Logger DEFAULT_ENTITY_GRAPH_LOGGER =
        LoggerFactory.getLogger(JpaEntityGraphMethodInterceptor.class + ".DefaultEntityGraph");

    private static final String DEFAULT_ENTITY_GRAPH_NAME_SUFFIX = ".default";
    private static final AtomicInteger DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT =
        new AtomicInteger();
    private static final int MAX_DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT = 10;

    private final Class<?> domainClass;
    private final EntityManager entityManager;
    private final DefaultEntityGraph defaultEntityGraph;
    private final ThreadLocal<EntityGraphQueryHintCandidate> currentEntityGraph =
        new NamedThreadLocal<>(
            "Thread local holding the current spring data jpa repository entity graph");

    JpaEntityGraphMethodInterceptor(EntityManager entityManager, Class<?> domainClass) {
      this.domainClass = domainClass;
      this.entityManager = entityManager;
      String defaultEntityGraphName =
          findDefaultEntityGraphName(entityManager, domainClass).orElse(null);
      if (defaultEntityGraphName != null
          && DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT.incrementAndGet()
              < MAX_DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT) {
        DEFAULT_ENTITY_GRAPH_LOGGER.warn(<commit_msg>Correct default entity graph logger name<commit_after>package com.cosium.spring.data.jpa.entity.graph.repository.support;

import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraph;
import com.cosium.spring.data.jpa.entity.graph.domain2.EntityGraphQueryHint;
import com.cosium.spring.data.jpa.entity.graph.domain2.NamedEntityGraph;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.InapplicableEntityGraphException;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.MultipleDefaultEntityGraphException;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.MultipleEntityGraphException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import javax.persistence.EntityManager;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.ResolvableType;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;

/**
 * Captures {@link EntityGraph} on repositories method calls. Created on 22/11/16.
 *
 * @author Reda.Housni-Alaoui
 */
class RepositoryMethodEntityGraphExtractor implements RepositoryProxyPostProcessor {

  private static final Logger LOG =
      LoggerFactory.getLogger(RepositoryMethodEntityGraphExtractor.class);

  private static final ThreadLocal<JpaEntityGraphMethodInterceptor> CURRENT_REPOSITORY =
      new NamedThreadLocal<>("Thread local holding the current repository");

  private final EntityManager entityManager;

  public RepositoryMethodEntityGraphExtractor(EntityManager entityManager) {
    this.entityManager = entityManager;
  }

  public static EntityGraphQueryHintCandidate getCurrentJpaEntityGraph() {
    JpaEntityGraphMethodInterceptor currentRepository = CURRENT_REPOSITORY.get();
    if (currentRepository == null) {
      return null;
    }
    return currentRepository.getCurrentJpaEntityGraph();
  }

  @Override
  public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
    factory.addAdvice(
        new JpaEntityGraphMethodInterceptor(entityManager, repositoryInformation.getDomainType()));
  }

  private static class JpaEntityGraphMethodInterceptor implements MethodInterceptor {

    private static final Logger DEFAULT_ENTITY_GRAPH_LOGGER =
        LoggerFactory.getLogger(
            JpaEntityGraphMethodInterceptor.class.getCanonicalName() + ".DefaultEntityGraph");

    private static final String DEFAULT_ENTITY_GRAPH_NAME_SUFFIX = ".default";
    private static final AtomicInteger DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT =
        new AtomicInteger();
    private static final int MAX_DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT = 10;

    private final Class<?> domainClass;
    private final EntityManager entityManager;
    private final DefaultEntityGraph defaultEntityGraph;
    private final ThreadLocal<EntityGraphQueryHintCandidate> currentEntityGraph =
        new NamedThreadLocal<>(
            "Thread local holding the current spring data jpa repository entity graph");

    JpaEntityGraphMethodInterceptor(EntityManager entityManager, Class<?> domainClass) {
      this.domainClass = domainClass;
      this.entityManager = entityManager;
      String defaultEntityGraphName =
          findDefaultEntityGraphName(entityManager, domainClass).orElse(null);
      if (defaultEntityGraphName != null
          && DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT.incrementAndGet()
              < MAX_DEFAULT_ENTITY_GRAPH_DEPRECATION_LOG_COUNT) {
        DEFAULT_ENTITY_GRAPH_LOGGER.warn(
{'fraction_non_alphanumeric': 0.07260852862082212, 'fraction_numerical': 0.0033294916122422846, 'mean_word_length': 5.607445008460237, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1122745', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2066, 'n_words': 387}