{"id":8653,"date":"2012-09-25T21:56:44","date_gmt":"2012-09-25T16:26:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8653"},"modified":"2016-12-19T15:17:46","modified_gmt":"2016-12-19T09:47:46","slug":"static-type-checking-in-groovy-2-0","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/static-type-checking-in-groovy-2-0\/","title":{"rendered":"Static Type Checking in Groovy 2.0"},"content":{"rendered":"<p>Groovy is a dynamic language. We can&#8217;t check any typo error, method or property missing error at compile time. To check typo error, method or\u00a0 property missing at compile time, Groovy 2.0 introduces new features named as Static Type Checking which helps to check errors at compile time.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>To use static type checker in groovy<br \/>\nYou have to write <strong>@TypeChecked<\/strong> annotation above the method. It enables method to check errors at compile time.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>[groovy]<br \/>\nimport groovy.transform.TypeChecked<\/p>\n<p>void findData() {}<\/p>\n<p>@TypeChecked<br \/>\nint testData() {<br \/>\n    \/\/ compilation error:<br \/>\n    \/\/ cannot find matching method findssssData()<br \/>\n    findssssData()<\/p>\n<p>    def name = &amp;quot;Hello&amp;quot;<\/p>\n<p>    \/\/ compilation error:<br \/>\n    \/\/ the variable naaammme is undeclared<br \/>\n    println naaammme<\/p>\n<p>\/\/ compilation error<br \/>\n\/\/ cannot return string value if method return type is integer<br \/>\nreturn &amp;quot;hello&amp;quot;<br \/>\n}<\/p>\n<p>[\/groovy]<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>This code will give the errors at compile time for method missing (findssssData), variable missing (variable name) and invalid return type.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>You can use static type checking in following case:<\/p>\n<ol>\n<li>To check the return type of any method<\/li>\n<li>Assignment type checking<\/li>\n<li>Method missing<\/li>\n<li>Variable missing<\/li>\n<\/ol>\n<p style=\"padding-bottom: 10px\">\n<p>To know more about static type checking, you can use following links:-<\/p>\n<p><a href=\"http:\/\/www.infoq.com\/articles\/new-groovy-20\">http:\/\/www.infoq.com\/articles\/new-groovy-20<\/a><\/p>\n<p><a href=\"http:\/\/www.infoq.com\/news\/2011\/11\/groovy-updates\">http:\/\/www.infoq.com\/news\/2011\/11\/groovy-updates<\/a><\/p>\n<p style=\"padding-bottom: 10px\">\n","protected":false},"excerpt":{"rendered":"<p>Groovy is a dynamic language. We can&#8217;t check any typo error, method or property missing error at compile time. To check typo error, method or\u00a0 property missing at compile time, Groovy 2.0 introduces new features named as Static Type Checking which helps to check errors at compile time. To use static type checker in groovy [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8653"}],"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\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8653"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8653\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}