@Test public void testGetSetMethod() throws Exception { ReflectionBean bean = new ReflectionBean(); final long start = System.currentTimeMillis(); final Method uncachedSetter = cache.getSetMethod(bean, "name", String.class); final long uncachedTime = System.currentTimeMillis() - start; final long start2 = System.currentTimeMillis(); final Method cachedSetter = cache.getSetMethod(bean, "name", String.class); final long cachedTime = System.currentTimeMillis() - start2; System.out.println(String.format("getSetMethod uncachedTime: %d, cachedTime: %d", uncachedTime, cachedTime)); assertTrue(cachedTime <= uncachedTime); assertEquals(uncachedSetter, cachedSetter); final String name1 = "uncached"; uncachedSetter.invoke(bean, name1); assertEquals(name1, bean.getName()); final String name2 = "cached"; cachedSetter.invoke(bean, name2); assertEquals(name2, bean.getName()); }
public <T> Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType) { if( object == null ) { throw new NullPointerException("object should not be null"); } else if( fieldName == null ) { throw new NullPointerException("fieldName should not be null"); } else if( argumentType == null ) { throw new NullPointerException("argumentType should not be null"); } Method method = setMethodsCache.get(object.getClass(), argumentType, fieldName); if( method == null ) { method = ReflectionUtils.findSetter(object, fieldName, argumentType); setMethodsCache.set(object.getClass(), argumentType, fieldName, method); } return method; }
MethodCache { public <T> Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType) { if( object == null ) { throw new NullPointerException("object should not be null"); } else if( fieldName == null ) { throw new NullPointerException("fieldName should not be null"); } else if( argumentType == null ) { throw new NullPointerException("argumentType should not be null"); } Method method = setMethodsCache.get(object.getClass(), argumentType, fieldName); if( method == null ) { method = ReflectionUtils.findSetter(object, fieldName, argumentType); setMethodsCache.set(object.getClass(), argumentType, fieldName, method); } return method; } }
MethodCache { public <T> Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType) { if( object == null ) { throw new NullPointerException("object should not be null"); } else if( fieldName == null ) { throw new NullPointerException("fieldName should not be null"); } else if( argumentType == null ) { throw new NullPointerException("argumentType should not be null"); } Method method = setMethodsCache.get(object.getClass(), argumentType, fieldName); if( method == null ) { method = ReflectionUtils.findSetter(object, fieldName, argumentType); setMethodsCache.set(object.getClass(), argumentType, fieldName, method); } return method; }  }
MethodCache { public <T> Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType) { if( object == null ) { throw new NullPointerException("object should not be null"); } else if( fieldName == null ) { throw new NullPointerException("fieldName should not be null"); } else if( argumentType == null ) { throw new NullPointerException("argumentType should not be null"); } Method method = setMethodsCache.get(object.getClass(), argumentType, fieldName); if( method == null ) { method = ReflectionUtils.findSetter(object, fieldName, argumentType); setMethodsCache.set(object.getClass(), argumentType, fieldName, method); } return method; }  Method getGetMethod(final Object object, final String fieldName); Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType); }
MethodCache { public <T> Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType) { if( object == null ) { throw new NullPointerException("object should not be null"); } else if( fieldName == null ) { throw new NullPointerException("fieldName should not be null"); } else if( argumentType == null ) { throw new NullPointerException("argumentType should not be null"); } Method method = setMethodsCache.get(object.getClass(), argumentType, fieldName); if( method == null ) { method = ReflectionUtils.findSetter(object, fieldName, argumentType); setMethodsCache.set(object.getClass(), argumentType, fieldName, method); } return method; }  Method getGetMethod(final Object object, final String fieldName); Method getSetMethod(final Object object, final String fieldName, final Class<?> argumentType);  }