{"id":8080,"date":"2012-09-26T23:59:25","date_gmt":"2012-09-26T18:29:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8080"},"modified":"2012-10-05T16:36:03","modified_gmt":"2012-10-05T11:06:03","slug":"getting-property-class-from-property-name","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/","title":{"rendered":"Getting property class from property name"},"content":{"rendered":"<p>Recently in one of my project i had a requirement of identifying the class of a property of an object with object class and property name and then determining whether it&#8217;s an Enum class or not. I found an easy solution for this using Reflection API. Using Java Reflection you can inspect the fields (member variables) of classes. This is done via the Java class java.lang.reflect.Field and calling <code>geType()<\/code> on this Field object gives the class of \u00a0that property.<\/p>\n<p>[java]<\/p>\n<p>class Utils {<\/p>\n<p>public static def getClass(Class objectClass, String propertyName) {<\/p>\n<p>def propertyClass = null<\/p>\n<p>try {<\/p>\n<p>propertyClass = objectClass.getDeclaredField(propertyName)?.type<\/p>\n<p>}<\/p>\n<p>catch (NoSuchFieldException ex){<\/p>\n<p>log.error(&quot;No such property exist in this object class&amp;&quot;+objectClass)<\/p>\n<p>}<\/p>\n<p>return propertyClass<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/Checking enum class or not<\/p>\n<p>Utils.getClass(object.class,&quot;someFieldName&quot;)?.isEnum()<\/p>\n<p>[\/java]<\/p>\n<p>There is one more benefit. One can also check whether particular property exists in a class or not as\u00a0<code>getDeclaredField<\/code> throws <code>NoSuchFieldException<\/code> if there is no such property with given name in the object class.<\/p>\n<p><!--more--><\/p>\n<p><!--more--><\/p>\n<p><!--more--><\/p>\n<p>Regards<\/p>\n<p>$achin verma<\/p>\n<p><span style=\"color: #ff6600\"> sachin.verma<\/span>@<span style=\"color: #0000ff\">intelligrape.com<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in one of my project i had a requirement of identifying the class of a property of an object with object class and property name and then determining whether it&#8217;s an Enum class or not. I found an easy solution for this using Reflection API. Using Java Reflection you can inspect the fields (member [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":25},"categories":[7],"tags":[555,1071,4844,1070],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8080"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8080"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8080\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}