{"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":30,"footnotes":""},"categories":[7],"tags":[555,1071,4844,1070],"class_list":["post-8080","post","type-post","status-publish","format-standard","hentry","category-grails","tag-enum","tag-field","tag-java","tag-reflection-api"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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&#039;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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sachin Verma\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Getting property class from property name | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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&#039;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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Getting property class from property name | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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&#039;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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#article\",\"name\":\"Getting property class from property name | TO THE NEW Blog\",\"headline\":\"Getting property class from property name\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sachin-verma\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-09-26T23:59:25+05:30\",\"dateModified\":\"2012-10-05T16:36:03+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#webpage\"},\"articleSection\":\"Grails, enum, field, Java, reflection api\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#listItem\",\"name\":\"Getting property class from property name\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#listItem\",\"position\":3,\"name\":\"Getting property class from property name\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sachin-verma\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sachin-verma\\\/\",\"name\":\"Sachin Verma\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/94829ab955a49dca68aebd5e61c357bc7b22dff3b3c43353fece63ce4831657a?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sachin Verma\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/\",\"name\":\"Getting property class from property name | TO THE NEW Blog\",\"description\":\"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'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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/getting-property-class-from-property-name\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sachin-verma\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sachin-verma\\\/#author\"},\"datePublished\":\"2012-09-26T23:59:25+05:30\",\"dateModified\":\"2012-10-05T16:36:03+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Getting property class from property name | TO THE NEW Blog","description":"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'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","canonical_url":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#article","name":"Getting property class from property name | TO THE NEW Blog","headline":"Getting property class from property name","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sachin-verma\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2012-09-26T23:59:25+05:30","dateModified":"2012-10-05T16:36:03+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#webpage"},"articleSection":"Grails, enum, field, Java, reflection api"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/www.tothenew.com\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#listItem","name":"Getting property class from property name"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#listItem","position":3,"name":"Getting property class from property name","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/sachin-verma\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/sachin-verma\/","name":"Sachin Verma","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/94829ab955a49dca68aebd5e61c357bc7b22dff3b3c43353fece63ce4831657a?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sachin Verma"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/","name":"Getting property class from property name | TO THE NEW Blog","description":"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'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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sachin-verma\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/sachin-verma\/#author"},"datePublished":"2012-09-26T23:59:25+05:30","dateModified":"2012-10-05T16:36:03+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Getting property class from property name | TO THE NEW Blog","og:description":"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'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","og:url":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Getting property class from property name | TO THE NEW Blog","twitter:description":"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'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","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"8080","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 02:05:17","updated":"2024-02-29 09:35:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGetting property class from property name\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Grails","link":"https:\/\/www.tothenew.com\/blog\/category\/grails\/"},{"label":"Getting property class from property name","link":"https:\/\/www.tothenew.com\/blog\/getting-property-class-from-property-name\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8080","targetHints":{"allow":["GET"]}}],"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}]}}