diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-06-18 22:03:30 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-06-18 22:03:30 +0200 |
commit | 775abd485de0d960cc9046c97426135466a187ce (patch) | |
tree | c5616f38e974ba018a0d67bc066a39750173a58b /website/features/experimental | |
parent | 8a0d362a7c7aa5cac5870255267a278eed4970fd (diff) | |
download | lombok-775abd485de0d960cc9046c97426135466a187ce.tar.gz lombok-775abd485de0d960cc9046c97426135466a187ce.tar.bz2 lombok-775abd485de0d960cc9046c97426135466a187ce.zip |
Added documentation for @ExtensionMethod feature
Diffstat (limited to 'website/features/experimental')
-rw-r--r-- | website/features/experimental/Accessors.html | 2 | ||||
-rw-r--r-- | website/features/experimental/ExtensionMethod.html | 109 | ||||
-rw-r--r-- | website/features/experimental/index.html | 2 |
3 files changed, 112 insertions, 1 deletions
diff --git a/website/features/experimental/Accessors.html b/website/features/experimental/Accessors.html index 65b3c3b6..f3a7878f 100644 --- a/website/features/experimental/Accessors.html +++ b/website/features/experimental/Accessors.html @@ -84,7 +84,7 @@ </div> </div> <div class="footer"> - <a href="index.html">Back to experimental features</a> | <span class="disabled">Previous feature</span> | <span class="disabled">Next feature</span><br /> + <a href="index.html">Back to experimental features</a> | <span class="disabled">Previous feature</span> | <a href="ExtensionMethod.html">Next feature (@ExtensionMethod)</a><br /> <a href="../../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright © 2009-2012 The Project Lombok Authors, licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</span> </div> <div style="clear: both;"></div> diff --git a/website/features/experimental/ExtensionMethod.html b/website/features/experimental/ExtensionMethod.html new file mode 100644 index 00000000..773904bb --- /dev/null +++ b/website/features/experimental/ExtensionMethod.html @@ -0,0 +1,109 @@ +<!DOCTYPE html> +<html><head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <link rel="stylesheet" type="text/css" href="../../logi/reset.css" /> + <link rel="stylesheet" type="text/css" href="../features.css" /> + <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon" /> + <meta name="description" content="Spice up your java" /> + <title>EXPERIMENTAL - @ExtensionMethod</title> +</head><body><div id="pepper"> + <div class="minimumHeight"></div> + <div class="meat"> + <div class="header"><a href="../../index.html">Project Lombok</a></div> + <h1>@ExtensionMethod</h1> + <div class="byline">Annoying API? Fix it yourself: Add new methods to existing types!</div> + <div class="since"> + <h3>Since</h3> + <p> + @ExtensionMethod was introduced as experimental feature in lombok v0.11.2. + </p> + </div> + <div class="experimental"> + <h3>Experimental</h3> + <p> + Experimental because: + <ul> + <li>High-impact on code style</li> + <li>Really would like to ship with utility methods to expand common classes, but so far lombok doesn't have a good distribution method for such runtime dependencies</li> + <li>Affects quite a bit of eclipse, and auto-complete e.d. do not work yet in netbeans</li> + <li>Should @ExtensionMethod be legal on methods? Should it be legal on packages?</li> + </ul> + Current status: <em>positive</em> - Currently we feel this feature may move out of experimental status with no or minor changes soon. + </div> + <div class="overview"> + <h3>Overview</h3> + <p> + You can make a class containing a bunch of <code>public</code>, <code>static</code> methods which all take at least 1 + parameter. These methods will extend the type of the first parameter, as if they were instance methods, using the + <code>@ExtensionMethod</code> feature. + </p> + </p><p> + For example, if you create <code>public static String toTitleCase(String in) { ... }</code>, you can use the + <code>@ExtensionMethod</code> feature to make it look like the <code>java.lang.String</code> class has a method named + <code>toTitleCase</code>, which has no arguments. The first argument of the static method fills the role of <code>this</code> + in instance methods. + </p><p> + All methods that are <code>public</code>, <code>static</code>, and have at least 1 argument whose type is not primitive, are + considered extension methods, and each will be injected into the namespace of the type of the first parameter as if they were + instance methods. As in the above example, a call that looks like: <code>foo.toTitleCase()</code> is replaced with + <code>ClassContainingYourExtensionMethod.toTitleCase(foo);</code>. Note that it is actually not an instant + <code>NullPointerException</code> if <code>foo</code> is null - it is passed like any other parameter. + </p><p> + You can pass any number of classes to the <code>@ExtensionMethod</code> annotation; they will all be searched for + extension methods. These extension methods apply for any code that is in the annotated class. + </p><p> + Lombok does not (currently) have any runtime dependencies which means lombok does not (currently) ship with any useful + extension methods so you'll have to make your own. However, here's one that might spark your imagination: + <pre>public class ObjectExtensions { + public static <T> or(T object, T ifNull) { + return object != null ? object : ifNull; + } +}</pre> + With the above class, if you add <code>@ExtensionMethod(ObjectExtensions.class)</code> to your class definition, you can write: + <pre>String x = null; +System.out.println(x.or("Hello, World!"));</pre> + The above code will not fail with a <code>NullPointerException</code>; it will actually output <code>Hello, World!</code> + </p> + </div> + <div class="snippets"> + <div class="pre"> + <h3>With Lombok</h3> + <div class="snippet">@HTML_PRE@</div> + </div> + <div class="sep"></div> + <div class="post"> + <h3>Vanilla Java</h3> + <div class="snippet">@HTML_POST@</div> + </div> + </div> + <div style="clear: left;"></div> + <div class="overview"> + <h3>Small print</h3><div class="smallprint"> + <p> + Calls are rewritten to a call to the extension method; the static method itself is not inlined. Therefore, the + extension method must be present both at compile and at runtime. + </p><p> + Generics is fully applied to figure out extension methods. i.e. if the first parameter of your extension method is + <code>List<? extends String></code>, then any expression that is compatible with that will have your extension method, + but other kinds of lists won't. So, a <code>List<Object></code> won't get it, but a <code>List<String></code> will. + </p> + </div> + </div> + <div class="footer"> + <a href="index.html">Back to experimental features</a> | <a href="Accessors.html">Previous feature (@Accessors)</a> | <span class="disabled">Next feature</a><br /> + <a href="../../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright © 2009-2012 The Project Lombok Authors, licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</span> + </div> + <div style="clear: both;"></div> + </div> +</div> +<script type="text/javascript"> + var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); + document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); +</script> +<script type="text/javascript"> + try { + var pageTracker = _gat._getTracker("UA-9884254-1"); + pageTracker._trackPageview(); + } catch(err) {} +</script> +</body></html> diff --git a/website/features/experimental/index.html b/website/features/experimental/index.html index e4e90c41..9b25d710 100644 --- a/website/features/experimental/index.html +++ b/website/features/experimental/index.html @@ -24,6 +24,8 @@ <dl> <dt><a href="Accessors.html"><code>@Accessors</code></a></dt> <dd>A more fluent API for getters and setters.</dd> + <dt><a href="ExtensionMethod.html"><Code>@ExtensionMethod</code></a></dt> + <dd>Annoying API? Fix it yourself: Add new methods to existing types!</dd> </dl> </div> <div class="footer"> |