PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/website/features/experimental/Wither.html

http://github.com/rzwitserloot/lombok
HTML | 113 lines | 113 code | 0 blank | 0 comment | 0 complexity | 4e42cd5a5969aef8d96da064537a8c53 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <!DOCTYPE html>
  2. <html><head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <link rel="stylesheet" type="text/css" href="../../logi/reset.css" />
  5. <link rel="stylesheet" type="text/css" href="../features.css" />
  6. <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon" />
  7. <meta name="description" content="Spice up your java" />
  8. <title>EXPERIMENTAL – @Wither</title>
  9. </head><body><div id="pepper">
  10. <div class="minimumHeight"></div>
  11. <div class="meat">
  12. <div class="header"><a href="../../index.html">Project Lombok</a></div>
  13. <h1>@Wither</h1>
  14. <div class="byline">Immutable 'setters' - methods that create a clone but with one changed field.</div>
  15. <div class="since">
  16. <h3>Since</h3>
  17. <p>
  18. @Wither was introduced as experimental feature in lombok v0.11.4.
  19. </p>
  20. </div>
  21. <div class="experimental">
  22. <h3>Experimental</h3>
  23. <p>
  24. Experimental because:
  25. <ul>
  26. <li>Still not sure that <code>@Wither</code> is an appropriate name for this feature.</li>
  27. <li>Should there be an option to supply a way of cloning the input somehow?</li>
  28. <li>Should the way that the clone is created by configurable?</li>
  29. <li>Should we replace @Wither entirely with a builder class?</li>
  30. </ul>
  31. Current status: <em>neutral</em> - More feedback requires on the items in the above list before promotion to the main package is warranted.
  32. </div>
  33. <div class="overview">
  34. <h3>Overview</h3>
  35. <p>
  36. The next best alternative to a setter for an immutable property is to construct a clone of the object, but with a new value for this one field.
  37. A method to generate this clone is precisely what <code>@Wither</code> generates: a <code>withFieldName(newValue)</code> method which produces a clone
  38. except for the new value for the associated field.
  39. </p><p>
  40. For example, if you create <code>public class Point { private final int x, y; }</code>, setters make no sense because the fields
  41. are final. <code>@Wither</code> can generate a <code>withX(int newXValue)</code> method for you which will return a new point with the supplied
  42. value for <code>x</code> and the same value for <code>y</code>.
  43. </p><p>
  44. Like <a href="../GetterSetter.html"><code>@Setter</code></a>, you can specify an access level in case you want the generated wither to be something other than <code>public</code>:<br />
  45. <code>@Wither(level = AccessLevel.PROTECTED)</code>. Also like <a href="../GetterSetter.html"><code>@Setter</code></a>, you can also put a <code>@Wither</code> annotation on a type, which means
  46. a 'wither' is generated for each field (even non-final fields).
  47. </p><p>
  48. To put annotations on the generated method, you can use <code>onMethod=@__({@AnnotationsHere})</code>; to put annotations on the only parameter of a generated wither method, you can use <code>onParam=@__({@AnnotationsHere})</code>. Be careful though! This is an experimental feature. For more details see the documentation on the <a href="onX.html">onX</a> feature.
  49. </p><p>
  50. <em>NEW in lombok v1.12.0:</em> javadoc on the field will now be copied to generated withers. Normally, all text is copied, and <code>@param</code> is <em>moved</em> to the wither, whilst <code>@return</code> lines are stripped from the wither's javadoc. Moved means: Deleted from the field's javadoc. It is also possible to define unique text for the wither's javadoc. To do that, you create a 'section' named <code>WITHER</code>. A section is a line in your javadoc containing 2 or more dashes, then the text 'WITHER', followed by 2 or more dashes, and nothing else on the line. If you use sections, <code>@return</code> and <code>@param</code> stripping / copying for that section is no longer done (move the <code>@param</code> line into the section).
  51. </p>
  52. </div>
  53. <div class="snippets">
  54. <div class="pre">
  55. <h3>With Lombok</h3>
  56. <div class="snippet">@HTML_PRE@</div>
  57. </div>
  58. <div class="sep"></div>
  59. <div class="post">
  60. <h3>Vanilla Java</h3>
  61. <div class="snippet">@HTML_POST@</div>
  62. </div>
  63. </div>
  64. <div style="clear: left;"></div>
  65. <div class="overview confKeys">
  66. <h3>Supported configuration keys:</h3>
  67. <dl>
  68. <dt><code>lombok.wither.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)</dt>
  69. <dd>Lombok will flag any usage of <code>@Wither</code> as a warning or error if configured.</dd>
  70. </dl>
  71. </div>
  72. <div class="overview">
  73. <h3>Small print</h3><div class="smallprint">
  74. <p>
  75. Withers cannot be generated for static fields because that makes no sense.
  76. </p><p>
  77. Withers can be generated for abstract classes, but this generates an abstract method with the appropriate signature.
  78. </p><p>
  79. When applying <code>@Wither</code> to a type, static fields and fields whose name start with a $ are skipped.
  80. </p><p>
  81. For generating the method names, the first character of the field, if it is a lowercase character, is title-cased, otherwise, it is left unmodified.
  82. Then, <code>with</code> is prefixed.
  83. </p><p>
  84. No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, <code>withX(int x)</code>
  85. will not be generated if there's already a method <code>withX(String... x)</code> even though it is technically possible to make the method. This caveat
  86. exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters.
  87. </p><p>
  88. For <code>boolean</code> fields that start with <code>is</code> immediately followed by a title-case letter, nothing is prefixed to generate the wither name.
  89. </p><p>
  90. Any annotations named <code>@NonNull</code> (case insensitive) on the field are interpreted as: This field must not ever hold
  91. <em>null</em>. Therefore, these annotations result in an explicit null check in the generated wither. Also, these
  92. annotations (as well as any annotation named <code>@Nullable</code> or <code>@CheckForNull</code>) are copied to wither parameter.
  93. </p>
  94. </div>
  95. </div>
  96. <div class="footer">
  97. <a href="index.html">Back to experimental features</a> | <a href="Delegate.html">Previous feature (@Delegate)</a> | <a href="onX.html">Next feature (onX)</a><br />
  98. <a href="../../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright &copy; 2009-2016 The Project Lombok Authors, licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</span>
  99. </div>
  100. <div style="clear: both;"></div>
  101. </div>
  102. </div>
  103. <script type="text/javascript">
  104. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  105. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  106. </script>
  107. <script type="text/javascript">
  108. try {
  109. var pageTracker = _gat._getTracker("UA-9884254-1");
  110. pageTracker._trackPageview();
  111. } catch(err) {}
  112. </script>
  113. </body></html>