<commit_before>package br.com.caelum.vraptor.ioc.cdi;

import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

import br.com.caelum.vraptor.ioc.Container;

@ApplicationScoped
@SuppressWarnings("unchecked")
public class CDIBasedContainer implements Container {

	private final BeanManager beanManager;

	/**
	 * @deprecated CDI eyes only
	 */
	protected CDIBasedContainer(){
		this(null);
	}

	@Inject
	public CDIBasedContainer(BeanManager beanManager) {
		this.beanManager = beanManager;
	}

	@Override
	public <T> T instanceFor(Class<T> type) {
		return selectFromContainer(type);
	}

	@Override
	public <T> boolean canProvide(Class<T> type) {
		return selectFromContainer(type) != null;
	}

	private <T> T selectFromContainer(final Class<T> type) {
		Set<Bean<?>> beans = beanManager.getBeans(type);
		Bean<? extends Object> bean = beanManager.resolve(beans);
		CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
		return (T) beanManager.getReference(bean, type, ctx);
	}
}<commit_msg>Only ask CDI if at least one bean is found<commit_after>package br.com.caelum.vraptor.ioc.cdi;

import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

import br.com.caelum.vraptor.ioc.Container;

@ApplicationScoped
@SuppressWarnings("unchecked")
public class CDIBasedContainer implements Container {

	private final BeanManager beanManager;

	/**
	 * @deprecated CDI eyes only
	 */
	protected CDIBasedContainer(){
		this(null);
	}

	@Inject
	public CDIBasedContainer(BeanManager beanManager) {
		this.beanManager = beanManager;
	}

	@Override
	public <T> T instanceFor(Class<T> type) {
		return selectFromContainer(type);
	}

	@Override
	public <T> boolean canProvide(Class<T> type) {
		return selectFromContainer(type) != null;
	}

	private <T> T selectFromContainer(final Class<T> type) {
		Set<Bean<?>> beans = beanManager.getBeans(type);
		if (beans.isEmpty()) { // if no beans found
			return null;
		}
		
		Bean<? extends Object> bean = beanManager.resolve(beans);
		CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
		return (T) beanManager.getReference(bean, type, ctx);
	}
}
{'fraction_non_alphanumeric': 0.11556982343499198, 'fraction_numerical': 0.0, 'mean_word_length': 5.392307692307693, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1697737', 'n_tokens_mistral': 832, 'n_tokens_neox': 803, 'n_words': 175}