From d2c6ecacc5b580e44d87159767370614d4031992 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Wed, 15 Aug 2012 14:28:23 -0500 Subject: [PATCH] Tweaking fluent bean utils by adding fallback to Field access. --- .../data/rest/core/util/FluentBeanUtils.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/FluentBeanUtils.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/FluentBeanUtils.java index e820d45c9..541c270c2 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/FluentBeanUtils.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/FluentBeanUtils.java @@ -11,18 +11,15 @@ import java.util.concurrent.ExecutionException; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.util.ReflectionUtils; /** * Helper methods for dealing with the metadata of "fluent" beans. * - * @author Jon Brisbin + * @author Jon Brisbin */ public abstract class FluentBeanUtils { - private static final Logger log = LoggerFactory.getLogger(FluentBeanUtils.class); private static final LoadingCache, Metadata> metadata = CacheBuilder.newBuilder().build( new CacheLoader, Metadata>() { @Override public Metadata load(Class type) @@ -40,6 +37,7 @@ public abstract class FluentBeanUtils { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if(method.getName().equals(fname)) { + ReflectionUtils.makeAccessible(method); if(method.getParameterTypes().length == 0) { meta.getters.put(fname, method); } else if(method.getParameterTypes().length == 1) { @@ -49,6 +47,8 @@ public abstract class FluentBeanUtils { } } }); + ReflectionUtils.makeAccessible(field); + meta.fields.put(fname, field); } } } @@ -97,14 +97,18 @@ public abstract class FluentBeanUtils { Method setter = metadata.get(type).setters.get(property); if(null != setter) { return setter.invoke(bean, value); - } else { + } + + Field f = metadata.get(type).fields.get(property); + if(null == f) { return null; } + + f.set(bean, value); + + return bean; } catch(Throwable t) { - if(log.isDebugEnabled()) { - log.debug(t.getMessage(), t); - } - return null; + throw new IllegalArgumentException(t.getMessage(), t); } } @@ -128,14 +132,16 @@ public abstract class FluentBeanUtils { Method getter = metadata.get(type).getters.get(property); if(null != getter) { return getter.invoke(bean); - } else { + } + + Field f = metadata.get(type).fields.get(property); + if(null == f) { return null; } + + return f.get(bean); } catch(Throwable t) { - if(log.isDebugEnabled()) { - log.debug(t.getMessage(), t); - } - return null; + throw new IllegalStateException(t.getMessage(), t); } } @@ -159,6 +165,7 @@ public abstract class FluentBeanUtils { public static class Metadata { List fieldNames = new ArrayList(); + Map fields = new HashMap(); Map getters = new HashMap(); Map setters = new HashMap(); @@ -173,6 +180,10 @@ public abstract class FluentBeanUtils { public Map setters() { return setters; } + + public Map fields() { + return fields; + } } }