{"id":171,"date":"2012-01-16T11:53:12","date_gmt":"2012-01-16T11:53:12","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=171"},"modified":"2012-01-16T11:53:12","modified_gmt":"2012-01-16T11:53:12","slug":"alpha-lightweight-dynamic-proxy-api","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2012\/01\/16\/alpha-lightweight-dynamic-proxy-api\/","title":{"rendered":"Alpha: Lightweight Dynamic Proxy API"},"content":{"rendered":"<p>I have added an <strong>alpha<\/strong> version of a dynamic proxy API to <a href=\"https:\/\/github.com\/lexicalscope\/fluent-reflection\" title=\"fluent reflection\">fluent reflection<\/a>.<\/p>\n<p>It allows you to define Java dynamic proxies using a light-weight fluent syntax. It uses the same reusable hamcrest matchers that are used throughout the <a href=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/18\/introducing-fluent-reflection\/\" title=\"Introducing Fluent Reflection\">rest of the fluent reflection API<\/a>.<\/p>\n<h3>Overview<\/h3>\n<p>For example, to proxy a simple interface like this<\/p>\n<pre lang=\"java\">\r\ninterface TwoQueryMethod {\r\n   int methodA();\r\n\r\n   int methodB();\r\n}\r\n<\/pre>\n<p>You define a new class which extends <code>com.lexicalscope.fluentreflection.dynamicproxy.Implementing<\/code>. You can define as many methods as you like inside this class. Each method should start by asserting what it is proxying by declaring a hamcrest matcher to match against the method currently being proxied. The first matching method found will be executed.<\/p>\n<p>In this simple example each method is matched by exact name, but the matchers can be as complex as you need them to be:<\/p>\n<pre lang=\"java\">\r\nfinal TwoQueryMethod dynamicProxy = dynamicProxy(new Implementing<TwoQueryMethod>() {\r\n   public void bodyA() {\r\n      whenProxying(callableHasName(\"methodA\"));\r\n      returnValue(42);\r\n   }\r\n\r\n   public void bodyB() {\r\n      whenProxying(callableHasName(\"methodB\"));\r\n      returnValue(24);\r\n   }\r\n});\r\n<\/pre>\n<h3>Method Arguments<\/h3>\n<p>The arguments of the original method are available from the context provided by the <code>Implementing<\/code> base class<\/p>\n<pre lang=\"java\">\r\ninterface MethodWithArgument {\r\n   int method(int argument);\r\n   String method(String argument);\r\n}\r\n\r\nfinal MethodWithArgument dynamicProxy = dynamicProxy(new Implementing<MethodWithArgument>() {\r\n   public void body() {\r\n      returnValue(args()[0]);\r\n   }\r\n});\r\n<\/pre>\n<p>An alternative approach can be used if you know in advance what the arguments of the method will be. In this example a matcher which matches against the arguments of each body is implied. The body will only be called if the proxied method call has arguments that can be used to satisfy the requirements of the body:<\/p>\n<pre lang=\"java\">\r\nfinal MethodWithArgument dynamicProxy = dynamicProxy(new Implementing<MethodWithArgument>() {\r\n   public int body(final int agument)\r\n   {\r\n      return 42;\r\n   }\r\n\r\n   public String body(final String agument)\r\n   {\r\n      return \"42\";\r\n   }\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have added an alpha version of a dynamic proxy API to fluent reflection. It allows you to define Java dynamic proxies using a light-weight fluent syntax. It uses the same reusable hamcrest matchers that are used throughout the rest &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2012\/01\/16\/alpha-lightweight-dynamic-proxy-api\/\">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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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},"jetpack_post_was_ever_published":false},"categories":[6,7],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-fluent-reflection","category-software-projects"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-2L","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/171","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=171"}],"version-history":[{"count":5,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}