{"id":125,"date":"2010-09-16T21:23:40","date_gmt":"2010-09-16T20:23:40","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=125"},"modified":"2010-09-16T21:26:30","modified_gmt":"2010-09-16T20:26:30","slug":"java-abuse-inline-instanceof","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/","title":{"rendered":"Java Abuse: Inline instanceof"},"content":{"rendered":"<p class=\"lead\">One annoyance in Java is having to do instanceof checks on multiple lines. e.g.<\/p>\n<pre lang=\"java\">\r\nif (object instanceof Foo) {\r\n    Foo foo = (Foo)object;\r\n    foo.foo();\r\n}\r\n<\/pre>\n<p>While this is often a sign of a design failure, there are times when instanceof checks are required often due to framework constraints etc. The above is quite ugly and involves using 3 lines instead of 1 for a single method call.  If foo() is a void method we can&#8217;t even use the ternary ? : operator to make it more concise. <\/p>\n<p>In c# with Lambdas and Extension methods one can create something like : <\/p>\n<pre lang=\"csharp\">\r\n    object.When<Foo>(foo => foo.Foo());\r\n<\/pre>\n<p>Where &#8220;When&#8221; is an extension method added to all objects which takes a type parameter and a lambda to execute if the type parameter matches. (Implementation available if anyone cares).<\/p>\n<p>Since Java doesn&#8217;t have Extension methods or Lambdas yet (Oracle minus minus) we can&#8217;t do this. We can however always use reflection and get something like:<\/p>\n<pre lang=\"java\">\r\npackage scratchpad;\r\n\r\nimport java.lang.reflect.Method;\r\nimport net.sf.cglib.proxy.Enhancer;\r\nimport net.sf.cglib.proxy.MethodInterceptor;\r\nimport net.sf.cglib.proxy.MethodProxy;\r\n\r\nimport static scratchpad.When.when;\r\n\r\npublic class Let {\r\n\tpublic static void main(String... args) {\r\n\t\tObject a = new Foo();\r\n\t\tObject b = \"hello world\";\r\n\t\t\r\n\t\twhen(Foo.class).isTypeOf(a).foo(); \/\/ Prints \"foo\"\r\n\t\twhen(Foo.class).isTypeOf(b).foo(); \/\/ Does nothing at all\r\n\t}\r\n}\r\n\r\nclass Foo {\r\n\tpublic void foo() {\r\n\t\tSystem.out.println(\"foo\");\r\n\t}\r\n}\r\n\r\nclass When {\r\n\tpublic static <T> Is<T> when(final Class<T> cls) {\r\n\t\treturn new Is<T>() {\r\n\t\t\t@SuppressWarnings(\"unchecked\")\r\n\t\t\tpublic T isTypeOf(Object o) {\r\n\t\t\t\treturn o != null && cls.isAssignableFrom(o.getClass()) \r\n\t\t\t\t\t? (T)o\r\n\t\t\t\t\t: NullObject.create(cls);\r\n\t\t\t}\r\n\r\n\t\t};\r\n\t}\r\n\t\t\r\n\tpublic interface Is<T> {\r\n\t\tpublic T isTypeOf(Object o);\r\n\t}\r\n}\r\n\r\nclass NullObject implements MethodInterceptor  {\r\n\t\r\n\t@SuppressWarnings(\"unchecked\")\r\n\tpublic static <T> T create(Class<T> cls){\r\n\t\tEnhancer enhancer = new Enhancer();\r\n\t\tenhancer.setSuperclass(cls);\r\n\t\tenhancer.setCallback(new NullObject());\r\n\t\treturn (T)enhancer.create();\r\n\t}\r\n\r\n\tpublic Object intercept(Object o, Method method, Object[] os, MethodProxy mp) throws Throwable {\r\n\t\treturn null;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One annoyance in Java is having to do instanceof checks on multiple lines. e.g. if (object instanceof Foo) { Foo foo = (Foo)object; foo.foo(); } While this is often a sign of a design failure, there are times when instanceof checks are required often due to framework constraints etc. The above is quite ugly and&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\" class=\"more-link\" title=\"Read Java Abuse: Inline instanceof\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,1],"tags":[23],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Abuse: Inline instanceof - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"One annoyance in Java is having to do instanceof checks on multiple lines. e.g. if (object instanceof Foo) { Foo foo = (Foo)object; foo.foo(); } While this is often a sign of a design failure, there are times when instanceof checks are required often due to framework constraints etc. The above is quite ugly and... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2010-09-16T20:23:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2010-09-16T20:26:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\",\"name\":\"Java Abuse: Inline instanceof - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"datePublished\":\"2010-09-16T20:23:40+00:00\",\"dateModified\":\"2010-09-16T20:26:30+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/125"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":7,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}