/*
 * Copyright 2017 Otavio Santana and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jnosql.artemis;


import org.jnosql.artemis.reflection.Reflections;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The Default implementation to {@link Converters}
 */
@ApplicationScoped
class DefaultConverters implements Converters {

    @Inject
    private Reflections reflections;

    private Map<Class<? extends AttributeConverter>, AttributeConverter> attributeConverters = new ConcurrentHashMap<>();


    @Override
    public AttributeConverter get(Class<? extends AttributeConverter> converterClass) throws NullPointerException {
        Objects.requireNonNull(converterClass, "The converterClass is required");
        AttributeConverter converter = attributeConverters.get(converterClass);
        if (Objects.isNull(converter)) {
            converter = reflections.newInstance(converterClass);
            attributeConverters.put(converterClass, converter);
        }

        return converter;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DefaultConverters{");
        sb.append("attributeConverters=").append(attributeConverters.size());
        sb.append('}');
        return sb.toString();
    }
}

@@ -643,101 +643,194 @@
 ort 
-org.jnosql.artemis.reflection.Reflections;%0A%0Aimport javax.enterprise.context.ApplicationScoped
+javax.enterprise.context.ApplicationScoped;%0Aimport javax.enterprise.context.spi.CreationalContext;%0Aimport javax.enterprise.inject.spi.Bean;%0Aimport javax.enterprise.inject.spi.BeanManager
 ;%0Aim
@@ -859,30 +859,8 @@
 ct;%0A
-import java.util.Map;%0A
 impo
@@ -884,55 +884,8 @@
 cts;
-%0Aimport java.util.concurrent.ConcurrentHashMap;
 %0A%0A/*
@@ -1038,156 +1038,32 @@
 ate 
-Reflections reflections;%0A%0A    private Map%3CClass%3C? extends AttributeConverter%3E, AttributeConverter%3E attributeConverters = new ConcurrentHashMap%3C%3E();%0A
+BeanManager beanManager;
 %0A%0A  
@@ -1284,62 +1284,26 @@
     
-AttributeConverter converter = attributeConverters.get
+return getInstance
 (con
@@ -1324,209 +1324,278 @@
     
+%7D%0A%0A
     
-if (Objects.isNull(converter)) %7B%0A            converter = reflections.newInstance(converterClass);%0A            attributeConverters.put(converterClass, converter);%0A        %7D%0A%0A        return converter
+private %3CT%3E T getInstance(Class%3CT%3E clazz) %7B%0A        Bean%3CT%3E bean = (Bean%3CT%3E) beanManager.getBeans(clazz).iterator().next();%0A        CreationalContext%3CT%3E ctx = beanManager.createCreationalContext(bean);%0A        return (T) beanManager.getReference(bean, clazz, ctx)
 ;%0A  
@@ -1737,35 +1737,27 @@
 append(%22
-attributeConvert
+beanManag
 er
-s
 =%22).appe
@@ -1763,34 +1763,19 @@
 end(
-attributeConverters.size()
+beanManager
 );%0A 

Define converter with bean manager