Off topic: Thanks to Keith for his suggestion (super source) & Thanks
to the Lombok team for their great work!
Q: Can I use Lombok with GWT? The ConstructorProperties annotation is
not supported by the GWT compiler.
A: Using GWT's super-source behavior (
http://code.google.com/
webtoolkit/doc/latest/
DevGuideOrganizingProjects.html#DevGuideModuleXml) you can provide
your own copy of @ConstructorProperties for the GWT compiler.
1) Create your Lombok annotated class
@Data
public class Greeting {
// NB: My class is immutable; If you want to use GWT RPC with
immutable classes you need to write a custom field serializer.
// Otherwise make this field non-final and update your
lombok annotations
private final String greeting;
}
2) Create your JRE emulation package. If your GWT module package is
com.example.lombok then your jre emulation package is
com.example.lombok.jre
3) Defining your ConstructorProperties class
i) Create the com.example.lombok.jre.java.beans package
ii) Copy the sun ConstructorProperties class into this package
ii) NB: You should not have to make any changes, it is a simple
annotation and compiles as-is. Especially do not update the package;
even though java.beans gives a compiler warnings in an IDE (such as
Eclipse). You should exclude the contents of your JRE package from
your Eclipse build path to disable the error.
4) Define the super-type in your GWT module (GWTLombokDemo.gwt.xml)
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='gwtlombok'>
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<entry-point class='com.example.lombok.client.GWTLombok'/>
<source path='client'/>
<source path='shared'/>
<super-source path='jre'/>
</module>
5) Use delombok and the GWT compiler to create your war artifact
(servlet)
<?xml version='1.0' encoding='UTF-8'?>
<project name="GWTLombokDemo" default="build">
<target name="build">
<!-- Clean up -->
<delete file="target/GWTLombokDemo.war" />
<!-- Build -->
<antcall target="lombok-compile" />
<antcall target="gwt-compile" />
<antcall target="zip-webapp" />
</target>
<target name="lombok-compile" description="Delombok the code;
replaces annotations with standard java code">
<taskdef classname="lombok.delombok.ant.DelombokTask"
classpath="lib/lombok.jar" name="delombok" />
<delombok verbose="true" encoding="UTF-8" to="src-gen" from="src" /
>
</target>
<target name="gwt-compile" description="Convert the standard java
code into javascript for the browser">
<property name="gwt-home" value = "E:/eclipse-java-helios-win32-
x86_64/eclipse/plugins/com.google.gwt.eclipse.sdkbundle.
2.0.4_2.0.4.v201006301309/gwt-2.0.4" />
<java classname="com.google.gwt.dev.Compiler" failonerror="true"
fork="true">
<arg value="-style" />
<arg value="PRETTY" />
<arg value="com.example.lombok.GWTLombokDemo" />
<classpath path="src-gen;
${gwt-home}/gwt-dev.jar;
${gwt-home}/gwt-user.jar;
war/WEB-INF/lib/gwt-servlet.jar" />
</java>
</target>
<target name="zip-webapp" description="Build the servlet">
<zip zipfile="target/GWTLombokDemo.war" whenempty="create">
<fileset dir="war/">
</fileset>
</zip>
</target>
</project>
6) The result of the above is
Compilation output:
Buildfile: E:\workspaces\GWTlocal\GWTLombok\build.xml
build:
[delete] Deleting: E:\workspaces\GWTlocal\GWTLombok\target
\GWTLombokDemo.war
lombok-compile:
[delombok] File: com\example\lombok\client\Greeting.java [delombok-
ed]
[delombok] File: com\example\lombok\client\GreetingService.java
[unchanged]
[delombok] File: com\example\lombok\client\GreetingServiceAsync.java
[unchanged]
[delombok] File: com\example\lombok\client
\Greeting_CustomFieldSerializer.java [unchanged]
[delombok] File: com\example\lombok\client\GWTLombok.java [unchanged]
[delombok] Copying resource file: com\example\lombok
\GWTLombok.gwt.xml
[delombok] File: com\example\lombok\jre\java\beans
\ConstructorProperties.java [unchanged]
[delombok] File: com\example\lombok\server\GreetingServiceImpl.java
[unchanged]
gwt-compile:
[java] Compiling module com.example.lombok.GWTLombok
[java] Compiling 6 permutations
[java] Compiling permutation 0...
[java] Compiling permutation 1...
[java] Compiling permutation 2...
[java] Compiling permutation 3...
[java] Compiling permutation 4...
[java] Compiling permutation 5...
[java] Compile of permutations succeeded
[java] Linking into E:workspaces\GWTlocal\GWTLombok\war
\gwtlombok.
[java] Link succeeded
[java] Compilation succeeded -- 24.239s
zip-webapp:
[zip] Building zip: E:workspaces\GWTlocal\GWTLombok\target
\GWTLombokDemo.war
BUILD SUCCESSFUL
Total time: 26 seconds
Greeting.Java (after delombok)
// Generated by delombok at Thu Aug 05 23:09:16 WST 2010
package com.example.lombok.client;
public class Greeting {
private final String greeting;
@java.beans.ConstructorProperties({"greeting"})
@java.lang.SuppressWarnings("all")
public Greeting(final String greeting) {
this.greeting = greeting;
}
@java.lang.SuppressWarnings("all")
public String getGreeting() {
return this.greeting;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
final Greeting other = (Greeting)o;
if (this.getGreeting() == null ? other.getGreeting() != null : !
this.getGreeting().equals(other.getGreeting())) return false;
return true;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + (this.getGreeting() == null ? 0 :
this.getGreeting().hashCode());
return result;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Greeting(greeting=" + this.getGreeting() + ")";
}
}
The war file produced by GWT is your servlet, drop it into Tomcat/
Jetty or any other servlet container to run it; alternatively if you
configure Eclipse to use your delomboked source instead of lombok
annotated source on its build path you can use development mode in
Eclipse to run your delomboked code.
I hope this helps someone in the future; If I have missed anything or
you have any questions please don't hesitate to ask :)
On Aug 5, 5:15 pm, Roel Spilker <
R.Spil...@topdesk.com> wrote:
> Hi Syntax,
>
> Can you mail the things you did to the mailing list. I'd really like other lombok users the benefit from your findings. We could maybe also put it on the yet to be created FAQ.
>
> Roel
>
>
>
>
>
> > -----Oorspronkelijk bericht-----
> > Google Groups group forhttp://
projectlombok.org/