{"id":36,"date":"2011-09-18T00:15:55","date_gmt":"2011-09-18T00:15:55","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=36"},"modified":"2011-09-18T00:26:39","modified_gmt":"2011-09-18T00:26:39","slug":"introducing-fluent-reflection","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/18\/introducing-fluent-reflection\/","title":{"rendered":"Introducing Fluent Reflection"},"content":{"rendered":"<p>I am in the processes of releasing the first alpha build of fluent-reflection, you can get the snapshot version from <a href=\"https:\/\/oss.sonatype.org\/content\/repositories\/snapshots\/com\/lexicalscope\/fluent-reflection\/fluent-reflection\/\" title=\"fluent reflection snapshot repository\">the fluent-reflection snapshot repository<\/a>.<\/p>\n<h3>Motivation<\/h3>\n<p>Reflection code is difficult to write in Java, and even harder to read once it has been written. Although the <a href=\"http:\/\/java.sun.com\/developer\/technicalArticles\/ALT\/Reflection\/\" title=\"Java Reflection API\">Java Reflection API<\/a> allows complete access to the Java runtime type system, the complexity of using the Java Reflection API reduces its usefulness. I don&#8217;t want to have to write any more code like this:<\/p>\n<pre lang=\"java\">\r\nfinal List<Method> getMethods = new ArrayList<Method>();\r\n\r\nClass<? super Bean> klass = Bean.class;\r\nwhile (klass != null) {\r\n   final Method[] declaredMethods = klass.getDeclaredMethods();\r\n   for (final Method method : declaredMethods) {\r\n      if (method.getName().startsWith(\"get\") && \r\n          method.getParameterTypes().length == 0 && \r\n          !method.getReturnType().equals(void.class)) {\r\n         getMethods.add(method);\r\n      }\r\n   }\r\n   klass = klass.getSuperclass();\r\n   [...]\r\n}\r\n<\/pre>\n<h3>Solution<\/h3>\n<p>There are three APIs which have heavily influenced the design of the fluent-reflection API.<\/p>\n<ol>\n<li><a href=\"http:\/\/code.google.com\/p\/lambdaj\/\" title=\"LambdaJ Library\">LambdaJ<\/a><\/li>\n<li><a href=\"http:\/\/code.google.com\/p\/hamcrest\/\" title=\"Hamcrest Library\">Hamcrest<\/a><\/li>\n<li><a href=\"http:\/\/www.jmock.org\" title=\"Jmock 2 Library\">Jmock 2<\/a><\/li>\n<\/ol>\n<p>All of them are examples of a style of API design known as <a href=\"http:\/\/martinfowler.com\/bliki\/FluentInterface.html\" title=\"Fluent Interface\">fluent interfaces<\/a>. They use a combination of static methods, method chaining, generic methods, static factory methods and object scoping to produce a type safe and readable programming style.<\/p>\n<p>In particular, the combination of method chaining and generic methods can help the user discover the usage of each part of the API and also takes advantage of Java&#8217;s static type system to prevent programming errors.<\/p>\n<p>Using fluent-reflection the above code can be re-written in declarative style:<\/p>\n<pre lang=\"java\">\r\nfinal List<ReflectedMethod> getMethods = \r\n   object(new Bean()).methods(\r\n      callableHasNameStartingWith(\"get\").\r\n      and(callableHasNoArguments()).\r\n      and(not(callableHasVoidReturn())));\r\n<\/pre>\n<h3>Extensibility<\/h3>\n<p>The query methods in the API are given queries described by <a href=\"http:\/\/code.google.com\/p\/hamcrest\/source\/browse\/trunk\/hamcrest-java\/hamcrest-core\/src\/main\/java\/org\/hamcrest\/Matcher.java\" title=\"Hamcrest Matcher\">Hamcrest Matcher<\/a>s. So users can easily construct custom and reusable queries.<\/p>\n<p>For example, this Matcher can select methods with a particular annotation:<\/p>\n<pre lang=\"java\">\r\nclass MatcherAnnotatedWith extends ReflectionMatcher<ReflectedAnnotated> {\r\n    private final Class<? extends Annotation> annotation;\r\n\r\n    public MatcherCallableAnnotatedWith(final Class<? extends Annotation> annotation) {\r\n        this.annotation = annotation;\r\n    }\r\n\r\n    @Override protected boolean matchesSafely(final ReflectedAnnotated item) {\r\n        return item.annotation(reflectedTypeReflectingOn(annotation)) != null;\r\n    }\r\n\r\n    @Override public void describeTo(final Description description) {\r\n        description.appendText(\"callable annotated with \").appendValue(annotation);\r\n    }\r\n}\r\n<\/pre>\n<h3>Mashability<\/h3>\n<p>Because the API uses the de facto standard Hamcrest matchers, and the Java collections framework it can be combined with other libraries in powerful ways. <\/p>\n<p>For example, calling all of the <code>@PostConstruct<\/code> methods on an Object using LambdaJ and fluent-reflection:<\/p>\n<pre lang=\"java\">\r\nforEach(\r\n   object(subject).methods(annotatedWith(PostConstruct.class)),\r\n   ReflectedMethod.class).call();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am in the processes of releasing the first alpha build of fluent-reflection, you can get the snapshot version from the fluent-reflection snapshot repository. Motivation Reflection code is difficult to write in Java, and even harder to read once it &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/18\/introducing-fluent-reflection\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,7],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-fluent-reflection","category-software-projects"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":10,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}