{"id":12104,"date":"2015-04-16T15:23:00","date_gmt":"2015-04-16T14:23:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=83a646883a144a46d71748f5dc2005e2"},"modified":"2015-08-18T13:18:30","modified_gmt":"2015-08-18T12:18:30","slug":"ehcache-config-with-beanutils-2","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/04\/16\/ehcache-config-with-beanutils-2\/","title":{"rendered":"EhCache config with BeanUtils"},"content":{"rendered":"<p><a href=\"http:\/\/commons.apache.org\/proper\/commons-beanutils\/\">BeanUtils<\/a> allows you to set Bean properties.<br \/>If you have configuration stored in a Map it&#8217;s tempting to use BeanUtils to automagically setup <a href=\"http:\/\/ehcache.org\/\">EhCache<\/a> configuration.<br \/>Sadly this class has mixed types in setters and getter and thus BeanUtils that use Introspector behind won&#8217;t get getter and setter pairs properly. It will get only getters and thus inform you that these properties are read only: &#8220;Skipping read-only property&#8221;.<\/p>\n<p>My fast solution is to use BeanUtils and have a fallback to Reflection.<\/p>\n<pre><code>public static void setProperty(Object obj, String propertyName, Object propertyValue, boolean silently) {<br \/>        try {<br \/>            PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(obj, propertyName);<br \/>            Method writeMethod = desc.getWriteMethod(); <br \/>                 <br \/>            if (writeMethod == null) {<br \/>                writeMethod = getAlternativeWriteMethod(obj, propertyName, propertyValue.getClass());<br \/>            }<br \/>            <br \/>            if (writeMethod == null) {<br \/>                if (silently) {<br \/>                    return;<br \/>                }<br \/>                throw new IllegalArgumentException(\"Can't find writerMethod for \" + propertyName);<br \/>            }<br \/><br \/>            if (LOG.isTraceEnabled()) {<br \/>                LOG.trace(String.format(\"Setting %s property of %s\", propertyName, obj.getClass().getSimpleName()));<br \/>            }<br \/>            <br \/>            writeMethod.invoke(obj, propertyValue);<br \/>        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {<br \/>            throw new IllegalArgumentException(\"Error when setting object property.\", e);<br \/>        }<br \/>    }<br \/><br \/>    private static Method getAlternativeWriteMethod(Object obj, String propertyName, Class paramClass) throws NoSuchMethodException {<br \/>        String setterMethod = \"set\" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);<br \/>        Method m; <br \/>        if ((m = getMethod(obj, paramClass, setterMethod)) != null) {<br \/>            return m;<br \/>        }<br \/>        Class altClass = paramClass.isPrimitive() ? ClassUtils.primitiveToWrapper(paramClass) : ClassUtils.wrapperToPrimitive(paramClass);<br \/>        if ((m = getMethod(obj, altClass, setterMethod)) != null) {<br \/>            return m;<br \/>        }<br \/>        <br \/>        return null;<br \/>    }<br \/><br \/>    private static Method getMethod(Object obj, Class paramClass, String setterMethod) {<br \/><br \/>        try {<br \/>            return obj.getClass().getMethod(setterMethod, paramClass);<br \/>        } catch (NoSuchMethodException e) {<br \/>            return null;<br \/>        }<br \/>    }<br \/><\/code><\/pre>\n<p>I will think about PR to Configuration class but it&#8217;s complicated as EhCache 2.x is not present on <a href=\"https:\/\/github.com\/ehcache\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"BeanUtils allows you to set Bean properties.If you have configuration stored in a Map it&#8217;s tempting to use BeanUtils to automagically setup EhCache configuration.Sadly this class has mixed types in setters and getter and thus BeanUtils that use Introsp&#8230;\n","protected":false},"author":22,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":{"0":"post-12104","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12104"}],"version-history":[{"count":4,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12104\/revisions"}],"predecessor-version":[{"id":13435,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12104\/revisions\/13435"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}