<commit_before>package eu.drus.jpa.unit.cdi;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.Producer;
import javax.persistence.EntityManager;

class EntityManagerProducer implements Producer<EntityManager> {
    private static final EntityManagerHolder HOLDER = EntityManagerHolder.getInstance();

    private Producer<EntityManager> delegate;
    private boolean delegateUsed = false;

    public EntityManagerProducer(final Producer<EntityManager> delegate) {
        this.delegate = delegate;
    }

    @Override
    public EntityManager produce(final CreationalContext<EntityManager> ctx) {
        return (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {
                EntityManager.class
        }, (final Object proxy, final Method method, final Object[] args) -> {
            EntityManager em = null;
            if (HOLDER.getEntityManager() != null) {
                em = HOLDER.getEntityManager();
            } else {
                delegateUsed = true;
                return delegate.produce(ctx);
            }

            return method.invoke(em, args);
        });
    }

    @Override
    public void dispose(final EntityManager instance) {
        if (delegateUsed) {
            delegate.dispose(instance);
        }
    }

    @Override
    public Set<InjectionPoint> getInjectionPoints() {
        return delegate.getInjectionPoints();
    }
}<commit_msg>lazy creation of EntityManager from CDI context fixed<commit_after>package eu.drus.jpa.unit.cdi;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.Producer;
import javax.persistence.EntityManager;

class EntityManagerProducer implements Producer<EntityManager> {
    private static final EntityManagerHolder HOLDER = EntityManagerHolder.getInstance();

    private Producer<EntityManager> delegate;
    private boolean delegateUsed = false;

    public EntityManagerProducer(final Producer<EntityManager> delegate) {
        this.delegate = delegate;
    }

    @Override
    public EntityManager produce(final CreationalContext<EntityManager> ctx) {
        return (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {
                EntityManager.class
        }, (final Object proxy, final Method method, final Object[] args) -> {
            EntityManager em = HOLDER.getEntityManager();
            if (em == null) {
                em = delegate.produce(ctx);
                delegateUsed = true;
            }

            return method.invoke(em, args);
        });
    }

    @Override
    public void dispose(final EntityManager instance) {
        if (delegateUsed) {
            delegate.dispose(instance);
        }
    }

    @Override
    public Set<InjectionPoint> getInjectionPoints() {
        return delegate.getInjectionPoints();
    }
}
{'fraction_non_alphanumeric': 0.09116453325007805, 'fraction_numerical': 0.0, 'mean_word_length': 2.9950124688279303, 'pattern_counts': {'":': 0, '<': 13, '<?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': '5775695', 'n_tokens_mistral': 841, 'n_tokens_neox': 820, 'n_words': 186}