From 63a4938a7da6d49ed4ef182369faffb9e43e5368 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 17 Jul 2009 18:36:31 +0200 Subject: Added documentation for the @Data annotation. Also fixed whitespace issues in the snippet views, and removed a debug print in one of the snippets. --- build.xml | 7 ++-- usage_examples/DataExample_pre.jpage | 1 - website/features/Data.html | 77 ++++++++++++++++++++++++++++++++++++ website/features/features.css | 10 +++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 website/features/Data.html diff --git a/build.xml b/build.xml index b8f19c6b..3e07f363 100644 --- a/build.xml +++ b/build.xml @@ -20,6 +20,7 @@ THE SOFTWARE. --> + @@ -62,11 +63,9 @@ - @@ -82,14 +81,14 @@ - + - + diff --git a/usage_examples/DataExample_pre.jpage b/usage_examples/DataExample_pre.jpage index 890e648b..775f82ed 100644 --- a/usage_examples/DataExample_pre.jpage +++ b/usage_examples/DataExample_pre.jpage @@ -2,7 +2,6 @@ import lombok.AccessLevel; import lombok.Setter; import lombok.Data; -@lombok.core.PrintAST(printContent=true) public @Data class DataExample { private final String name; private @Setter(AccessLevel.PACKAGE) int age; diff --git a/website/features/Data.html b/website/features/Data.html new file mode 100644 index 00000000..3a9697cc --- /dev/null +++ b/website/features/Data.html @@ -0,0 +1,77 @@ + + + + + + + + @Data + +
+
+
+ +

@Data

+
+

Overview

+

+ Any class definition may be annotated with @Data to let lombok generate all the boilerplate that is associated with simple POJOs + (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, a useful toString, and implementations + of hashCode and equals which consider any two objects of this type with the same values for each field as equal. A + constructor is also generated containing 1 parameter for each final field, in the order the fields are defined. This constructor simply assigns + each parameter to the appropriate field. +

+

+ To override the access level of any getter/setter for any field, annotate the field with a @Setter or @Getter annotation + with the appropriate AccessLevel value. See the example below. For more information on how the getters/setters are generated, + see the documentation for @Getter and @Setter. +

+ All fields marked as transient will not be considered for hashCode and equals. All static fields will be + skipped entirely (not considered for any of the generated methods, and no setter/getter will be made for them). + The generated getter/setter method will be public unless you explicitly specify an AccessLevel, as shown in the example below. + Legal access levels are PUBLIC, PROTECTED, PACKAGE, and PRIVATE. +

+ If any method that would normally be generated exists in name that method will not be generated, and no warning or error is emitted. For example, + if you already have a method with signature void hashCode(int a, int b, int c), no int hashCode() method will be generated, + even though technically int hashCode() is an entirely different method. The same rule applies to the constructor, toString, + hashCode, and all getters and setters. +

+ @Data can handle generics parameters for fields just fine. In order to reduce the boilerplate when constructing objects for classes with + generics, you can use the staticConstructor parameter to generate a private constructor, as well as a static method that returns a new + instance. This way, javac will infer the variable name. Thus, by declaring like so: @Data(staticConstructor="of") class Foo<T> { private T x;} + you can create new instances of Foo by writing: Foo.of(5); instead of having to write: new Foo<Integer>(5);. +

+

Small print

+

+ Arrays are 'deep' compared/printed/hashCoded, which means that arrays that contain themselves will result in StackOverflowErrors. However, + this behaviour is no different from e.g. ArrayList. +

+ You may safely presume that the hashCode implementation used will not change between versions of lombok, however this guarantee is not set in stone; + if there's a significant performance improvement to be gained from using an alternate hash algorithm, that will be substituted in a future version. +

+ For a general idea of how lombok generated the equals, hashCode, and toString methods, check the example below. +

+ For the purposes of equality, 2 NaN (not a number) values for floats and doubles are considered equal, eventhough 'NaN == NaN' would + return false. This is analogous to java.lang.Double's equals method, and is in fact required to ensure that comparing an object + to an exact copy of itself returns true for equality. +

+
+
+
+
+

With Lombok

+
@HTML_PRE@
+
+
+
+

Vanilla Java

+
@HTML_POST@
+
+
+ +
+
+
diff --git a/website/features/features.css b/website/features/features.css index 98446bd8..4f8fe468 100644 --- a/website/features/features.css +++ b/website/features/features.css @@ -33,11 +33,16 @@ h1 { text-align: center; } +.overview code { + font-size: 1.2em; +} + .snippets { margin-top: 32px; } .snippets .pre { + white-space: pre; float: left; width: 45%; } @@ -56,6 +61,7 @@ h1 { } .snippets .post { + white-space: pre; float: left; width: 45%; } @@ -68,6 +74,10 @@ h1 { font-size: 14px; } +.snippet br { + display: none; +} + footer { clear: left; margin: 0 auto 0 auto; -- cgit