<commit_before>package org.apache.ibatis.executor.loader;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.reflection.property.PropertyNamer;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.*;

public class ResultObjectProxy {

  private static final Set<String> objectMethods = new HashSet<String>(Arrays.asList(new String[]{"equals","hashCode","toString"}));
  private static final TypeHandlerRegistry registry = new TypeHandlerRegistry();

  public static Object createProxy(Object target, ResultLoaderMap lazyLoader, boolean aggressive) {
    return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, aggressive);
  }

  private static class EnhancedResultObjectProxyImpl implements MethodInterceptor, Serializable {

    private ResultLoaderMap lazyLoader;
    private boolean aggressive;

    private EnhancedResultObjectProxyImpl(ResultLoaderMap lazyLoader, boolean aggressive) {
      this.lazyLoader = lazyLoader;
      this.aggressive = aggressive;
    }

    public static Object createProxy(Object target, ResultLoaderMap lazyLoader, boolean aggressive) {
      final Class type = target.getClass();
      if (registry.hasTypeHandler(type)) {
        return target;
      } else {
        final Object enhanced = Enhancer.create(type, new EnhancedResultObjectProxyImpl(lazyLoader, aggressive));
        copyInitialState(type, target, enhanced);
        return enhanced;
      }
    }

    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
      try {
        final String methodName = method.getName();
        synchronized (lazyLoader) {
          if (lazyLoader.size() > 0) {
            if (aggressive || objectMethods.contains(methodName)) {
              lazyLoader.loadAll();
            } else if (PropertyNamer.isProperty(methodName)) {
              final String property = PropertyNamer.methodToProperty(methodName);
              if (lazyLoader.hasLoader(property)) {
                lazyLoader.load(property);
              }
            }
          }
        }
        return methodProxy.invokeSuper(o, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }

    private static void copyInitialState(Class type, Object target, Object enhanced) {
      Class parent = type;
      while (parent != null) {
        final Field[] fields = parent.getDeclaredFields();<commit_msg>fixed undesirable lazy load upon finalizer<commit_after>package org.apache.ibatis.executor.loader;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.reflection.property.PropertyNamer;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.*;

public class ResultObjectProxy {

  private static final Set<String> objectMethods = new HashSet<String>(Arrays.asList(new String[]{"equals","clone","hashCode","toString"}));
  private static final TypeHandlerRegistry registry = new TypeHandlerRegistry();
  private static final String FINALIZE_METHOD = "finalize";

  public static Object createProxy(Object target, ResultLoaderMap lazyLoader, boolean aggressive) {
    return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, aggressive);
  }

  private static class EnhancedResultObjectProxyImpl implements MethodInterceptor, Serializable {

    private ResultLoaderMap lazyLoader;
    private boolean aggressive;

    private EnhancedResultObjectProxyImpl(ResultLoaderMap lazyLoader, boolean aggressive) {
      this.lazyLoader = lazyLoader;
      this.aggressive = aggressive;
    }

    public static Object createProxy(Object target, ResultLoaderMap lazyLoader, boolean aggressive) {
      final Class type = target.getClass();
      if (registry.hasTypeHandler(type)) {
        return target;
      } else {
        final Object enhanced = Enhancer.create(type, new EnhancedResultObjectProxyImpl(lazyLoader, aggressive));
        copyInitialState(type, target, enhanced);
        return enhanced;
      }
    }

    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
      try {
        final String methodName = method.getName();
        synchronized (lazyLoader) {
          if (lazyLoader.size() > 0) {
            if (!FINALIZE_METHOD.equals(methodName)) {
              if (aggressive || objectMethods.contains(methodName)) {
                lazyLoader.loadAll();
              } else if (PropertyNamer.isProperty(methodName)) {
                final String property = PropertyNamer.methodToProperty(methodName);
                if (lazyLoader.hasLoader(property)) {
                  lazyLoader.load(property);
                }
              }
            }
          }
        }
        return methodProxy.invokeSuper(o, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }

    private static void copyInitialState(Class type, Object target, Object enhanced) {
      Class parent = type;
      while (parent != null) {
        final Field[] fields = parent.getDeclaredFields();
{'fraction_non_alphanumeric': 0.08984092552422271, 'fraction_numerical': 0.00036153289949385393, 'mean_word_length': 3.377373417721519, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4265395', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1431, 'n_words': 348}