From 53a75addb6fcc5b256dd5b505aa5f22c5d7c0529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Sat, 15 Nov 2014 13:08:45 -0200 Subject: [PATCH] Fixing cache store when register a converter --- .../vraptor/core/DefaultConverters.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultConverters.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultConverters.java index a1716003a..43d83555f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultConverters.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/core/DefaultConverters.java @@ -29,8 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Supplier; - import br.com.caelum.vraptor.Convert; import br.com.caelum.vraptor.TwoWayConverter; import br.com.caelum.vraptor.cache.CacheStore; @@ -38,6 +36,8 @@ import br.com.caelum.vraptor.converter.Converter; import br.com.caelum.vraptor.ioc.Container; +import com.google.common.base.Supplier; + /** * Default implementation for {@link Converters}. * @@ -103,47 +103,52 @@ private int getConverterPriority(Class> converter) { @SuppressWarnings("unchecked") @Override public Converter to(Class clazz) { - Class> converterType = findConverterType(clazz); + Class> converterType = findConverterTypeFromCache(clazz); checkState(!converterType.equals(NullConverter.class), "Unable to find converter for %s", clazz.getName()); logger.debug("found converter {} to {}", converterType.getName(), clazz.getName()); return (Converter) container.instanceFor(converterType); } - private Class> findConverterType(final Class clazz) { + private Class> findConverterTypeFromCache(final Class clazz) { return cache.fetch(clazz, new Supplier>>() { @Override public Class> get() { - for (Class> current : classes) { - Class boundType = current.getAnnotation(Convert.class).value(); - if (boundType.isAssignableFrom(clazz)) { - return current; - } - } - - logger.debug("Unable to find a converter for {}. Returning NullConverter.", clazz); - return NullConverter.class; + return findConverterType(clazz); } + }); } + private Class> findConverterType(final Class clazz) { + for (Class> current : classes) { + Class boundType = current.getAnnotation(Convert.class).value(); + if (boundType.isAssignableFrom(clazz)) { + return current; + } + } + + logger.debug("Unable to find a converter for {}. Returning NullConverter.", clazz); + return NullConverter.class; + } + private interface NullConverter extends Converter {}; @Override public boolean existsFor(Class type) { - return !findConverterType(type).equals(NullConverter.class); + return !findConverterTypeFromCache(type).equals(NullConverter.class); } @Override public boolean existsTwoWayFor(Class type) { - return TwoWayConverter.class.isAssignableFrom(findConverterType(type)); + return TwoWayConverter.class.isAssignableFrom(findConverterTypeFromCache(type)); } @Override public TwoWayConverter twoWayConverterFor(Class type) { checkState(existsTwoWayFor(type), "Unable to find two way converter for %s", type.getName()); - return (TwoWayConverter) container.instanceFor(findConverterType(type)); + return (TwoWayConverter) container.instanceFor(findConverterTypeFromCache(type)); } }