{"id":912,"date":"2015-08-17T08:46:33","date_gmt":"2015-08-17T07:46:33","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=912"},"modified":"2015-08-23T08:56:26","modified_gmt":"2015-08-23T07:56:26","slug":"lambda-parameter-names-with-reflection","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/","title":{"rendered":"Lambda parameter names with reflection"},"content":{"rendered":"<p class=\"lead\"> Java 8 introduced a compiler flag -parameters, which makes method parameter names available at runtime with reflection. Up to now, this has not worked with lambda parameter names. However, Java 8u60 now has a <a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8079044\">fix for this bug<\/a> back-ported which makes it possible.<\/p>\n<p>Some uses that spring to mind (and work as of recent 8u60ea build) are doing things like <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/test\/java\/com\/benjiweber\/HashLiteralExample.java\">hash literals<\/a><\/p>\n<pre lang=\"java\">\r\nMap<String, String> hash = hash(\r\n    hello -> \"world\",\r\n    bob   -> bob,\r\n    bill  -> \"was here\"\r\n);\r\n\r\nassertEquals(\"world\",    hash.get(\"hello\"));\r\nassertEquals(\"bob\",      hash.get(\"bob\"));\r\nassertEquals(\"was here\", hash.get(\"bill\"));\r\n<\/pre>\n<p>Or just another tool for creating DSLs in Java itself. For example, look <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/test\/java\/com\/benjiweber\/PuppetExample.java\">how close<\/a> to <a href=\"https:\/\/docs.puppetlabs.com\/references\/latest\/type.html\">puppet&#8217;s DSL we can get now.<\/p>\n<pre lang=\"java\">\r\nstatic class Apache extends PuppetClass {{\r\n    file(\r\n        name -> \"\/etc\/httpd\/httpd.conf\",\r\n        source -> template(\"httpd.tpl\"),\r\n        owner -> root,\r\n        group -> root\r\n    );\r\n\r\n    cron (\r\n        name -> \"logrotate\",\r\n        command -> \"\/usr\/sbin\/logrotate\",\r\n        user -> root,\r\n        hour -> \"2\",\r\n        minute -> \"*\/10\"\r\n    );\r\n}}\r\n<\/pre>\n<p>Doing <\/p>\n<pre lang=\"java\">\r\nSystem.out.println(new Apache().toString());\r\n<\/pre>\n<p>Would print<\/p>\n<pre lang=\"puppet\">\r\nclass Apache {\r\n\r\n\tfile{\r\n\t\tname\t=> \"\/etc\/httpd\/httpd.conf\",\r\n\t\tsource\t=> template(httpd.tpl),\r\n\t\towner\t=> root,\r\n\t\tgroup\t=> root,\r\n\t}\r\n\tcron{\r\n\t\tname\t=> \"logrotate\",\r\n\t\tcommand\t=> \"\/usr\/sbin\/logrotate\",\r\n\t\tuser\t=> root,\r\n\t\thour\t=> \"2\",\r\n\t\tminute\t=> \"*\/10\",\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>The code examples for the <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/test\/java\/com\/benjiweber\/HashLiteralExample.java\">hash literal example<\/a> and the <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/test\/java\/com\/benjiweber\/PuppetExample.java\">puppet example<\/a> are on Github.<\/p>\n<p>They work by making use of a functional interface that extends Serializable. This makes it possible to get access to a <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/invoke\/SerializedLambda.html\">SerializedLambda<\/a> instance, which then gives us access to the synthetic method generated for the lambda. We can then use the standard reflection API to get the parameter names.<\/p>\n<p>Here&#8217;s a <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/main\/java\/com\/benjiweber\/typeref\/MethodFinder.java\">utility MethodFinder interface<\/a> that we can extend our functional interfaces from that makes it easier.<\/p>\n<pre lang=\"java\">\r\npublic interface MethodFinder extends Serializable {\r\n    default SerializedLambda serialized() {\r\n        try {\r\n            Method replaceMethod = getClass().getDeclaredMethod(\"writeReplace\");\r\n            replaceMethod.setAccessible(true);\r\n            return (SerializedLambda) replaceMethod.invoke(this);\r\n        } catch (Exception e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    default Class<?> getContainingClass() {\r\n        try {\r\n            String className = serialized().getImplClass().replaceAll(\"\/\", \".\");\r\n            return Class.forName(className);\r\n        } catch (Exception e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    default Method method() {\r\n        SerializedLambda lambda = serialized();\r\n        Class<?> containingClass = getContainingClass();\r\n        return asList(containingClass.getDeclaredMethods())\r\n                .stream()\r\n                .filter(method -> Objects.equals(method.getName(), lambda.getImplMethodName()))\r\n                .findFirst()\r\n                .orElseThrow(UnableToGuessMethodException::new);\r\n    }\r\n\r\n    default Parameter parameter(int n) {\r\n        return method().getParameters()[n];\r\n    }\r\n\r\n    class UnableToGuessMethodException extends RuntimeException {}\r\n}\r\n<\/pre>\n<p>Given the above, we can create a <a href=\"https:\/\/github.com\/benjiman\/lambda-type-references\/blob\/master\/src\/main\/java\/com\/benjiweber\/typeref\/NamedValue.java\">NamedValue<\/a> type that allows a lambda to represent a mapping from a String to a value.<\/p>\n<pre lang=\"java\">\r\npublic interface NamedValue<T> extends MethodFinder, Function<String, T> {\r\n    default String name() {\r\n        checkParametersEnabled();\r\n        return parameter(0).getName();\r\n    }\r\n    default void checkParametersEnabled() {\r\n        if (Objects.equals(\"arg0\", parameter(0).getName())) {\r\n            throw new IllegalStateException(\"You need to compile with javac -parameters for parameter reflection to work; You also need java 8u60 or newer to use it with lambdas\");\r\n        }\r\n    }\r\n\r\n    default T value() {\r\n        return apply(name());\r\n    }\r\n}\r\n<\/pre>\n<p>Then we can simply ask our lambda for the name and value.<\/p>\n<pre lang=\"java\">\r\nNamedValue<Integer> pair = five -> 5;\r\nassertEquals(\"five\", pair.name());\r\nassertEquals(5, pair.value());\r\n<\/pre>\n<p>See also <a href=\"http:\/\/benjiweber.co.uk\/blog\/2015\/08\/21\/html-in-java\/\">HTML in Java<\/a> for another example use of this.<\/p>\n<p>I imagine there are more uses too, any ideas?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 8 introduced a compiler flag -parameters, which makes method parameter names available at runtime with reflection. Up to now, this has not worked with lambda parameter names. However, Java 8u60 now has a fix for this bug back-ported which makes it possible. Some uses that spring to mind (and work as of recent 8u60ea&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/\" class=\"more-link\" title=\"Read Lambda parameter names with reflection\">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":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-912","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Lambda parameter names with reflection - Benji&#039;s Blog<\/title>\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\/2015\/08\/17\/lambda-parameter-names-with-reflection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lambda parameter names with reflection - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Java 8 introduced a compiler flag -parameters, which makes method parameter names available at runtime with reflection. Up to now, this has not worked with lambda parameter names. However, Java 8u60 now has a fix for this bug back-ported which makes it possible. Some uses that spring to mind (and work as of recent 8u60ea... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-17T07:46:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-23T07:56:26+00:00\" \/>\n<meta name=\"author\" content=\"benji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"benji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/\"},\"author\":{\"name\":\"benji\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"headline\":\"Lambda parameter names with reflection\",\"datePublished\":\"2015-08-17T07:46:33+00:00\",\"dateModified\":\"2015-08-23T07:56:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/\"},\"wordCount\":230,\"commentCount\":0,\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/\",\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/\",\"name\":\"Lambda parameter names with reflection - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#website\"},\"datePublished\":\"2015-08-17T07:46:33+00:00\",\"dateModified\":\"2015-08-23T07:56:26+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2015\\\/08\\\/17\\\/lambda-parameter-names-with-reflection\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lambda parameter names with reflection\"}]},{\"@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\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"caption\":\"benji\"},\"sameAs\":[\"http:\\\/\\\/benjiweber.co.uk\"],\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/author\\\/benji\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Lambda parameter names with reflection - Benji&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/","og_locale":"en_US","og_type":"article","og_title":"Lambda parameter names with reflection - Benji&#039;s Blog","og_description":"Java 8 introduced a compiler flag -parameters, which makes method parameter names available at runtime with reflection. Up to now, this has not worked with lambda parameter names. However, Java 8u60 now has a fix for this bug back-ported which makes it possible. Some uses that spring to mind (and work as of recent 8u60ea... Read more &raquo;","og_url":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/","og_site_name":"Benji&#039;s Blog","article_published_time":"2015-08-17T07:46:33+00:00","article_modified_time":"2015-08-23T07:56:26+00:00","author":"benji","twitter_card":"summary_large_image","twitter_misc":{"Written by":"benji","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/#article","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/"},"author":{"name":"benji","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"headline":"Lambda parameter names with reflection","datePublished":"2015-08-17T07:46:33+00:00","dateModified":"2015-08-23T07:56:26+00:00","mainEntityOfPage":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/"},"wordCount":230,"commentCount":0,"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/","url":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/","name":"Lambda parameter names with reflection - Benji&#039;s Blog","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/#website"},"datePublished":"2015-08-17T07:46:33+00:00","dateModified":"2015-08-23T07:56:26+00:00","author":{"@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"breadcrumb":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/17\/lambda-parameter-names-with-reflection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/benjiweber.co.uk\/blog\/"},{"@type":"ListItem","position":2,"name":"Lambda parameter names with reflection"}]},{"@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":{"@type":"EntryPoint","urlTemplate":"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09","name":"benji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","caption":"benji"},"sameAs":["http:\/\/benjiweber.co.uk"],"url":"https:\/\/benjiweber.co.uk\/blog\/author\/benji\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/912","targetHints":{"allow":["GET"]}}],"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=912"}],"version-history":[{"count":8,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/912\/revisions"}],"predecessor-version":[{"id":931,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/912\/revisions\/931"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}