From 893b5ea69b6c584d924c5d7fb5b1a6e5355295c4 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Date: Mon, 11 Mar 2013 21:01:53 +0100
Subject: startOnFirstThread is no longer necessary on modern JVMs on mac.
---
buildScripts/eclipse-debug-target.template | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'buildScripts')
diff --git a/buildScripts/eclipse-debug-target.template b/buildScripts/eclipse-debug-target.template
index 033f8197..bb45cd69 100644
--- a/buildScripts/eclipse-debug-target.template
+++ b/buildScripts/eclipse-debug-target.template
@@ -21,7 +21,7 @@
-
+
--
cgit
From 56150952c451f0d8c2018424191d4480ac5e8460 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Date: Mon, 11 Mar 2013 22:04:27 +0100
Subject: Added @Log4j2 support.
---
buildScripts/ivy.xml | 1 +
doc/changelog.markdown | 1 +
src/core/lombok/eclipse/handlers/HandleLog.java | 15 +++++-
.../lombok/extern/apachecommons/CommonsLog.java | 3 +-
src/core/lombok/extern/java/Log.java | 3 +-
src/core/lombok/extern/log4j/Log4j.java | 3 +-
src/core/lombok/extern/log4j/Log4j2.java | 62 ++++++++++++++++++++++
src/core/lombok/extern/slf4j/Slf4j.java | 3 +-
src/core/lombok/extern/slf4j/XSlf4j.java | 3 +-
src/core/lombok/javac/handlers/HandleLog.java | 17 +++++-
.../resource/after-delombok/LoggerCommons.java | 1 -
.../resource/after-delombok/LoggerJul.java | 1 -
.../resource/after-delombok/LoggerLog4j.java | 1 -
.../resource/after-delombok/LoggerLog4j2.java | 6 +++
.../resource/after-delombok/LoggerSlf4j.java | 2 -
.../resource/after-delombok/LoggerXSlf4j.java | 1 -
.../transform/resource/after-ecj/LoggerLog4j2.java | 17 ++++++
test/transform/resource/before/LoggerLog4j2.java | 9 ++++
website/features/Log.html | 6 ++-
19 files changed, 139 insertions(+), 16 deletions(-)
create mode 100644 src/core/lombok/extern/log4j/Log4j2.java
create mode 100644 test/transform/resource/after-delombok/LoggerLog4j2.java
create mode 100644 test/transform/resource/after-ecj/LoggerLog4j2.java
create mode 100644 test/transform/resource/before/LoggerLog4j2.java
(limited to 'buildScripts')
diff --git a/buildScripts/ivy.xml b/buildScripts/ivy.xml
index b1440db5..fd6c503a 100644
--- a/buildScripts/ivy.xml
+++ b/buildScripts/ivy.xml
@@ -17,6 +17,7 @@
+
diff --git a/doc/changelog.markdown b/doc/changelog.markdown
index a415cd01..3b56aa00 100644
--- a/doc/changelog.markdown
+++ b/doc/changelog.markdown
@@ -4,6 +4,7 @@ Lombok Changelog
### v0.11.7 (Edgy Guinea Pig)
* CHANGE: {Experimental} The experimental `@Value` feature no longer implies the also experimental `@Wither`. If you like your `@Value` classes to make withers, add `@Wither` to the class right next to `@Value`.
* FEATURE: {Experimental} Reintroduced `onMethod`, `onConstructor` and `onParam` to `@Getter`, `@Setter`, `@Wither`, and `@XArgsConstructor`. These parameters allow you to add annotations to the methods/constructors that lombok will generate. This is a workaround feature: The stability of the feature on future versions of javac is not guaranteed, and if a better way to implement this feature is found, this feature's current incarnation will be removed without a reasonable period of deprecation. [Documentation on the onX feature](http://projectlombok.org/features/experimental/onX.html)
+* FEATURE: Added support for Log4j v2.0 via `@Log4j2` [Issue #432](http://code.google.com/p/projectlombok/issues/detail?id=432)
### v0.11.6 (October 30th, 2012)
* FEATURE: Lombok can be disabled entirely for any given compile run by using JVM switch `-Dlombok.disable`. This might be useful for code style checkers and such.
diff --git a/src/core/lombok/eclipse/handlers/HandleLog.java b/src/core/lombok/eclipse/handlers/HandleLog.java
index bffe2d62..2e7b4475 100644
--- a/src/core/lombok/eclipse/handlers/HandleLog.java
+++ b/src/core/lombok/eclipse/handlers/HandleLog.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -177,6 +177,16 @@ public class HandleLog {
}
}
+ /**
+ * Handles the {@link lombok.extern.log4j.Log4j2} annotation for Eclipse.
+ */
+ @ProviderFor(EclipseAnnotationHandler.class)
+ public static class HandleLog4j2Log extends EclipseAnnotationHandler {
+ @Override public void handle(AnnotationValues annotation, Annotation source, EclipseNode annotationNode) {
+ processAnnotation(LoggingFramework.LOG4J2, annotation, source, annotationNode);
+ }
+ }
+
/**
* Handles the {@link lombok.extern.slf4j.Slf4j} annotation for Eclipse.
*/
@@ -224,6 +234,9 @@ public class HandleLog {
// private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TargetType.class);
LOG4J("org.apache.log4j.Logger", "org.apache.log4j.Logger", "getLogger", "@Log4j"),
+ // private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(TargetType.class);
+ LOG4J2("org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager", "getLogger", "@Log4j2"),
+
// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TargetType.class);
SLF4J("org.slf4j.Logger", "org.slf4j.LoggerFactory", "getLogger", "@Slf4j"),
diff --git a/src/core/lombok/extern/apachecommons/CommonsLog.java b/src/core/lombok/extern/apachecommons/CommonsLog.java
index f178ae05..024e3744 100644
--- a/src/core/lombok/extern/apachecommons/CommonsLog.java
+++ b/src/core/lombok/extern/apachecommons/CommonsLog.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -52,6 +52,7 @@ import java.lang.annotation.Target;
* @see org.apache.commons.logging.LogFactory#getLog(java.lang.Class) org.apache.commons.logging.LogFactory.getLog(Class target)
* @see lombok.extern.java.Log @Log
* @see lombok.extern.log4j.Log4j @Log4j
+ * @see lombok.extern.log4j.Log4j2 @Log4j2
* @see lombok.extern.slf4j.Slf4j @Slf4j
* @see lombok.extern.slf4j.XSlf4j @XSlf4j
*/
diff --git a/src/core/lombok/extern/java/Log.java b/src/core/lombok/extern/java/Log.java
index 90c62956..7ae4e07b 100644
--- a/src/core/lombok/extern/java/Log.java
+++ b/src/core/lombok/extern/java/Log.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -51,6 +51,7 @@ import java.lang.annotation.Target;
* @see java.util.logging.Logger#getLogger(java.lang.String) java.util.logging.Logger.getLogger(String name)
* @see lombok.extern.apachecommons.CommonsLog @CommonsLog
* @see lombok.extern.log4j.Log4j @Log4j
+ * @see lombok.extern.log4j.Log4j2 @Log4j2
* @see lombok.extern.slf4j.Slf4j @Slf4j
* @see lombok.extern.slf4j.XSlf4j @XSlf4j
*/
diff --git a/src/core/lombok/extern/log4j/Log4j.java b/src/core/lombok/extern/log4j/Log4j.java
index 9cfc5839..29e1b27c 100644
--- a/src/core/lombok/extern/log4j/Log4j.java
+++ b/src/core/lombok/extern/log4j/Log4j.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -50,6 +50,7 @@ import java.lang.annotation.Target;
*
* @see org.apache.log4j.Logger org.apache.log4j.Logger
* @see org.apache.log4j.Logger#getLogger(java.lang.Class) org.apache.log4j.Logger.getLogger(Class target)
+ * @see lombok.extern.log4j.Log4j2 @Log4j2
* @see lombok.extern.apachecommons.CommonsLog @CommonsLog
* @see lombok.extern.java.Log @Log
* @see lombok.extern.slf4j.Slf4j @Slf4j
diff --git a/src/core/lombok/extern/log4j/Log4j2.java b/src/core/lombok/extern/log4j/Log4j2.java
new file mode 100644
index 00000000..2a0f09e1
--- /dev/null
+++ b/src/core/lombok/extern/log4j/Log4j2.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2013 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.extern.log4j;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Causes lombok to generate a logger field.
+ *
+ * @Log4j2
+ * public class LogExample {
+ * }
+ *
+ *
+ * will generate:
+ *
+ *
+ * public class LogExample {
+ * private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.Logger.getLogger(LogExample.class);
+ * }
+ *
+ *
+ * This annotation is valid for classes and enumerations.
+ *
+ * @see org.apache.logging.log4j.Logger org.apache.logging.log4j.Logger
+ * @see org.apache.logging.log4j.LogManager#getLogger(java.lang.Class) org.apache.logging.log4j.LogManager.getLogger(Class target)
+ * @see lombok.extern.log4j.Log4j @Log4j
+ * @see lombok.extern.apachecommons.CommonsLog @CommonsLog
+ * @see lombok.extern.java.Log @Log
+ * @see lombok.extern.slf4j.Slf4j @Slf4j
+ * @see lombok.extern.slf4j.XSlf4j @XSlf4j
+ */
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.TYPE)
+public @interface Log4j2 {
+}
\ No newline at end of file
diff --git a/src/core/lombok/extern/slf4j/Slf4j.java b/src/core/lombok/extern/slf4j/Slf4j.java
index 14dbcba6..45942971 100644
--- a/src/core/lombok/extern/slf4j/Slf4j.java
+++ b/src/core/lombok/extern/slf4j/Slf4j.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -51,6 +51,7 @@ import java.lang.annotation.Target;
* @see lombok.extern.apachecommons.CommonsLog @CommonsLog
* @see lombok.extern.java.Log @Log
* @see lombok.extern.log4j.Log4j @Log4j
+ * @see lombok.extern.log4j.Log4j2 @Log4j2
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
diff --git a/src/core/lombok/extern/slf4j/XSlf4j.java b/src/core/lombok/extern/slf4j/XSlf4j.java
index bdf8a62c..599c68ab 100644
--- a/src/core/lombok/extern/slf4j/XSlf4j.java
+++ b/src/core/lombok/extern/slf4j/XSlf4j.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012 The Project Lombok Authors.
+ * Copyright (C) 2012-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -51,6 +51,7 @@ import java.lang.annotation.Target;
* @see lombok.extern.apachecommons.CommonsLog @CommonsLog
* @see lombok.extern.java.Log @Log
* @see lombok.extern.log4j.Log4j @Log4j
+ * @see lombok.extern.log4j.Log4j2 @Log4j2
* @see lombok.extern.slf4j.Slf4j @Slf4j
*/
@Retention(RetentionPolicy.SOURCE)
diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java
index 62a55c44..35a32be5 100644
--- a/src/core/lombok/javac/handlers/HandleLog.java
+++ b/src/core/lombok/javac/handlers/HandleLog.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012 The Project Lombok Authors.
+ * Copyright (C) 2010-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -82,7 +82,7 @@ public class HandleLog {
private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source) {
TreeMaker maker = typeNode.getTreeMaker();
- // private static final log = ();
+ // private static final log = ();
JCExpression loggerType = chainDotsString(typeNode, framework.getLoggerTypeName());
JCExpression factoryMethod = chainDotsString(typeNode, framework.getLoggerFactoryMethodName());
@@ -127,6 +127,16 @@ public class HandleLog {
}
}
+ /**
+ * Handles the {@link lombok.extern.log4j.Log4j2} annotation for javac.
+ */
+ @ProviderFor(JavacAnnotationHandler.class)
+ public static class HandleLog4j2Log extends JavacAnnotationHandler {
+ @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) {
+ processAnnotation(LoggingFramework.LOG4J2, annotation, annotationNode);
+ }
+ }
+
/**
* Handles the {@link lombok.extern.slf4j.Slf4j} annotation for javac.
*/
@@ -163,6 +173,9 @@ public class HandleLog {
// private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TargetType.class);
LOG4J(lombok.extern.log4j.Log4j.class, "org.apache.log4j.Logger", "org.apache.log4j.Logger.getLogger"),
+ // private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(TargetType.class);
+ LOG4J2(lombok.extern.log4j.Log4j2.class, "org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager.getLogger"),
+
// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TargetType.class);
SLF4J(lombok.extern.slf4j.Slf4j.class, "org.slf4j.Logger", "org.slf4j.LoggerFactory.getLogger"),
diff --git a/test/transform/resource/after-delombok/LoggerCommons.java b/test/transform/resource/after-delombok/LoggerCommons.java
index c2a03815..dfe3e88d 100644
--- a/test/transform/resource/after-delombok/LoggerCommons.java
+++ b/test/transform/resource/after-delombok/LoggerCommons.java
@@ -1,7 +1,6 @@
class LoggerCommons {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LoggerCommons.class);
}
-
class LoggerCommonsWithImport {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LoggerCommonsWithImport.class);
}
\ No newline at end of file
diff --git a/test/transform/resource/after-delombok/LoggerJul.java b/test/transform/resource/after-delombok/LoggerJul.java
index 39cb2aac..b020c540 100644
--- a/test/transform/resource/after-delombok/LoggerJul.java
+++ b/test/transform/resource/after-delombok/LoggerJul.java
@@ -1,7 +1,6 @@
class LoggerJul {
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LoggerJul.class.getName());
}
-
class LoggerJulWithImport {
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LoggerJulWithImport.class.getName());
}
\ No newline at end of file
diff --git a/test/transform/resource/after-delombok/LoggerLog4j.java b/test/transform/resource/after-delombok/LoggerLog4j.java
index 6892a7d8..dfbad89a 100644
--- a/test/transform/resource/after-delombok/LoggerLog4j.java
+++ b/test/transform/resource/after-delombok/LoggerLog4j.java
@@ -1,7 +1,6 @@
class LoggerLog4j {
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LoggerLog4j.class);
}
-
class LoggerLog4jWithImport {
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LoggerLog4jWithImport.class);
}
\ No newline at end of file
diff --git a/test/transform/resource/after-delombok/LoggerLog4j2.java b/test/transform/resource/after-delombok/LoggerLog4j2.java
new file mode 100644
index 00000000..3447a9a5
--- /dev/null
+++ b/test/transform/resource/after-delombok/LoggerLog4j2.java
@@ -0,0 +1,6 @@
+class LoggerLog4j2 {
+ private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggerLog4j2.class);
+}
+class LoggerLog4j2WithImport {
+ private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggerLog4j2WithImport.class);
+}
\ No newline at end of file
diff --git a/test/transform/resource/after-delombok/LoggerSlf4j.java b/test/transform/resource/after-delombok/LoggerSlf4j.java
index cb1486ba..4cc7c107 100644
--- a/test/transform/resource/after-delombok/LoggerSlf4j.java
+++ b/test/transform/resource/after-delombok/LoggerSlf4j.java
@@ -1,11 +1,9 @@
class LoggerSlf4j {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoggerSlf4j.class);
}
-
class LoggerSlf4jWithImport {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoggerSlf4jWithImport.class);
}
-
class LoggerSlf4jOuter {
static class Inner {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Inner.class);
diff --git a/test/transform/resource/after-delombok/LoggerXSlf4j.java b/test/transform/resource/after-delombok/LoggerXSlf4j.java
index 0239c60b..7d8f3236 100644
--- a/test/transform/resource/after-delombok/LoggerXSlf4j.java
+++ b/test/transform/resource/after-delombok/LoggerXSlf4j.java
@@ -1,7 +1,6 @@
class LoggerXSlf4j {
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LoggerXSlf4j.class);
}
-
class LoggerXSlf4jWithImport {
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LoggerXSlf4jWithImport.class);
}
\ No newline at end of file
diff --git a/test/transform/resource/after-ecj/LoggerLog4j2.java b/test/transform/resource/after-ecj/LoggerLog4j2.java
new file mode 100644
index 00000000..c1368e5d
--- /dev/null
+++ b/test/transform/resource/after-ecj/LoggerLog4j2.java
@@ -0,0 +1,17 @@
+import lombok.extern.log4j.Log4j2;
+@lombok.extern.log4j.Log4j2 class LoggerLog4j2 {
+ private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggerLog4j2.class);
+ () {
+ }
+ LoggerLog4j2() {
+ super();
+ }
+}
+@Log4j2 class LoggerLog4j2WithImport {
+ private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggerLog4j2WithImport.class);
+ () {
+ }
+ LoggerLog4j2WithImport() {
+ super();
+ }
+}
\ No newline at end of file
diff --git a/test/transform/resource/before/LoggerLog4j2.java b/test/transform/resource/before/LoggerLog4j2.java
new file mode 100644
index 00000000..b7ea99ee
--- /dev/null
+++ b/test/transform/resource/before/LoggerLog4j2.java
@@ -0,0 +1,9 @@
+import lombok.extern.log4j.Log4j2;
+
+@lombok.extern.log4j.Log4j2
+class LoggerLog4j2 {
+}
+
+@Log4j2
+class LoggerLog4j2WithImport {
+}
\ No newline at end of file
diff --git a/website/features/Log.html b/website/features/Log.html
index fb529426..2fb91956 100644
--- a/website/features/Log.html
+++ b/website/features/Log.html
@@ -16,9 +16,9 @@
Overview
NEW in lombok 0.10: You can annotate any class with a log annotation to let lombok generate a logger field.
- The logger is named log and field's type depends on which logger you have selected.
+ The logger is named log and the field's type depends on which logger you have selected.
- There are four choices available:
+ There are six choices available:
--
cgit
From 2e27817ca743858c7188128f96c8c32b894ea42e Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Date: Tue, 23 Apr 2013 03:31:22 +0200
Subject: bugfix in website ant buildscript for finding the Java2HtmlTask
---
buildScripts/website.ant.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'buildScripts')
diff --git a/buildScripts/website.ant.xml b/buildScripts/website.ant.xml
index 78dc8ed7..405b388f 100644
--- a/buildScripts/website.ant.xml
+++ b/buildScripts/website.ant.xml
@@ -76,7 +76,7 @@ such as converting the changelog into HTML, and creating javadoc.
-
+
--
cgit
From 5a3e9bd8049469169410107011ad0e26b3b629e3 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Date: Fri, 31 May 2013 01:03:38 +0200
Subject: Added @NonNull on parameters feature (issue 514), including docs and
changelog.
---
buildScripts/website.ant.xml | 3 +
doc/changelog.markdown | 1 +
src/core/lombok/NonNull.java | 16 ++-
.../eclipse/handlers/EclipseHandlerUtil.java | 7 +-
.../lombok/eclipse/handlers/NonNullHandler.java | 147 ++++++++++++++++++++
.../lombok/javac/handlers/HandleSneakyThrows.java | 12 +-
.../lombok/javac/handlers/JavacHandlerUtil.java | 23 +++-
src/core/lombok/javac/handlers/NonNullHandler.java | 148 +++++++++++++++++++++
src/utils/lombok/javac/Javac.java | 32 +++++
.../resource/after-delombok/DataOnLocalClass.java | 8 +-
.../after-delombok/NonNullOnParameter.java | 48 +++++++
.../resource/after-delombok/NonNullPlain.java | 8 +-
.../resource/after-delombok/SetterOnClass.java | 4 +-
.../resource/after-delombok/WitherOnClass.java | 4 +-
.../resource/after-ecj/DataOnLocalClass.java | 8 +-
.../resource/after-ecj/NonNullOnParameter.java | 61 +++++++++
.../transform/resource/after-ecj/NonNullPlain.java | 8 +-
.../resource/after-ecj/SetterOnClass.java | 4 +-
.../resource/after-ecj/WitherOnClass.java | 4 +-
.../resource/before/NonNullOnParameter.java | 30 +++++
.../NonNullOnParameter.java.messages | 1 +
.../messages-delombok/NonNullPlain.java.messages | 1 +
.../messages-ecj/NonNullOnParameter.java.messages | 3 +
.../messages-ecj/NonNullPlain.java.messages | 1 +
.../NonNullOnParameter.java.messages | 1 +
.../messages-idempotent/NonNullPlain.java.messages | 3 +
usage_examples/NonNullExample_post.jpage | 13 ++
usage_examples/NonNullExample_pre.jpage | 10 ++
website/features/Cleanup.html | 2 +-
website/features/Data.html | 2 +-
website/features/Delegate.html | 11 +-
website/features/GetterLazy.html | 2 +-
website/features/GetterSetter.html | 2 +-
website/features/Log.html | 2 +-
website/features/NonNull.html | 73 ++++++++++
website/features/SneakyThrows.html | 4 +-
website/features/Synchronized.html | 2 +-
website/features/ToString.html | 2 +-
website/features/index.html | 18 +--
website/features/val.html | 2 +-
40 files changed, 674 insertions(+), 57 deletions(-)
create mode 100644 src/core/lombok/eclipse/handlers/NonNullHandler.java
create mode 100644 src/core/lombok/javac/handlers/NonNullHandler.java
create mode 100644 test/transform/resource/after-delombok/NonNullOnParameter.java
create mode 100644 test/transform/resource/after-ecj/NonNullOnParameter.java
create mode 100644 test/transform/resource/before/NonNullOnParameter.java
create mode 100644 test/transform/resource/messages-delombok/NonNullOnParameter.java.messages
create mode 100644 test/transform/resource/messages-delombok/NonNullPlain.java.messages
create mode 100644 test/transform/resource/messages-ecj/NonNullOnParameter.java.messages
create mode 100644 test/transform/resource/messages-ecj/NonNullPlain.java.messages
create mode 100644 test/transform/resource/messages-idempotent/NonNullOnParameter.java.messages
create mode 100644 test/transform/resource/messages-idempotent/NonNullPlain.java.messages
create mode 100644 usage_examples/NonNullExample_post.jpage
create mode 100644 usage_examples/NonNullExample_pre.jpage
create mode 100644 website/features/NonNull.html
(limited to 'buildScripts')
diff --git a/buildScripts/website.ant.xml b/buildScripts/website.ant.xml
index 405b388f..41130bd2 100644
--- a/buildScripts/website.ant.xml
+++ b/buildScripts/website.ant.xml
@@ -145,6 +145,9 @@ such as converting the changelog into HTML, and creating javadoc.
+
+
+
diff --git a/doc/changelog.markdown b/doc/changelog.markdown
index e2e3d6b5..aaf66030 100644
--- a/doc/changelog.markdown
+++ b/doc/changelog.markdown
@@ -2,6 +2,7 @@ Lombok Changelog
----------------
### v0.11.9 (Edgy Guinea Pig)
+* FEATURE: `@NonNull` on a method or constructor parameter now generates a null-check statement at the start of your method. This nullcheck will throw a `NullPointerException` with the name of the parameter as the message. [Issue #514](https://code.google.com/p/projectlombok/issues/detail?id=514)
* BUGFIX: Usage of `Lombok.sneakyThrow()` or `@SneakyThrows` would sometimes result in invalid classes (classes which fail with `VerifyError`). [Issue #470](https://code.google.com/p/projectlombok/issues/detail?id=470)
* BUGFIX: Using `val` in try-with-resources did not work for javac. [Issue #520](https://code.google.com/p/projectlombok/issues/detail?id=520)
* BUGFIX: When using `@Data`, warnings are not generated if certain aspects are not generated because you wrote explicit versions of them. However, this gets confusing with `equals` / `hashCode` / `canEqual`, as nothing is generated if any one of those methods is present. Now, if one of `equals` or `hashCode` is present but not the other one (or `canEqual` is present but `equals` and/or `hashCode` is missing), a warning is emitted to explain that lombok will not generate any of the equals / hashCode methods, and that you should either write them all yourself or remove them all. [Issue #513](https://code.google.com/p/projectlombok/issues/detail?id=513)
diff --git a/src/core/lombok/NonNull.java b/src/core/lombok/NonNull.java
index 5f5d8ed2..96813170 100644
--- a/src/core/lombok/NonNull.java
+++ b/src/core/lombok/NonNull.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Project Lombok Authors.
+ * Copyright (C) 2009-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -28,12 +28,14 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
- * Lombok is smart enough to translate any annotation named {@code @NonNull} in any casing and
- * with any package name to the return type of generated getters and the parameter of generated setters and constructors,
- * as well as generate the appropriate null checks in the setter and constructor.
- *
- * You can use this annotation for the purpose, though you can also use JSR305's annotation, findbugs's, pmd's, or IDEA's, or just
- * about anyone elses. As long as it is named {@code @NonNull}.
+ * If put on a parameter, lombok will insert a null-check at the start of the method / constructor's body, throwing a
+ * {@code NullPointerException} with the parameter's name as message. If put on a field, any generated method assigning
+ * a value to this field will also produce these nullchecks.
+ *
+ * Note that any annotation named {@code NonNull} with any casing and any package will result in nullchecks produced for
+ * generated methods (and the annotation will be copied to the getter return type and any parameters of generated methods),
+ * but only this annotation, if present on a parameter, will result in a null check inserted into your otherwise
+ * handwritten method.
*
* WARNING: If the java community ever does decide on supporting a single {@code @NonNull} annotation (for example via JSR305), then
* this annotation will be deleted from the lombok package. If the need to update an import statement scares
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 7703336f..dc99dabf 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -61,6 +61,7 @@ import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.Block;
import org.eclipse.jdt.internal.compiler.ast.CastExpression;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
@@ -1334,7 +1335,11 @@ public class EclipseHandlerUtil {
EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL);
equalExpression.sourceStart = pS; equalExpression.statementEnd = equalExpression.sourceEnd = pE;
setGeneratedBy(equalExpression, source);
- IfStatement ifStatement = new IfStatement(equalExpression, throwStatement, 0, 0);
+ Block throwBlock = new Block(0);
+ throwBlock.statements = new Statement[] {throwStatement};
+ throwBlock.sourceStart = pS; throwBlock.sourceEnd = pE;
+ setGeneratedBy(throwBlock, source);
+ IfStatement ifStatement = new IfStatement(equalExpression, throwBlock, 0, 0);
setGeneratedBy(ifStatement, source);
return ifStatement;
}
diff --git a/src/core/lombok/eclipse/handlers/NonNullHandler.java b/src/core/lombok/eclipse/handlers/NonNullHandler.java
new file mode 100644
index 00000000..5c58069c
--- /dev/null
+++ b/src/core/lombok/eclipse/handlers/NonNullHandler.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2013 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.eclipse.handlers;
+
+import java.util.Arrays;
+
+import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
+import org.eclipse.jdt.internal.compiler.ast.Argument;
+import org.eclipse.jdt.internal.compiler.ast.Block;
+import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
+import org.eclipse.jdt.internal.compiler.ast.Expression;
+import org.eclipse.jdt.internal.compiler.ast.IfStatement;
+import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
+import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
+import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
+import org.eclipse.jdt.internal.compiler.ast.Statement;
+import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
+import org.mangosdk.spi.ProviderFor;
+
+import lombok.NonNull;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.eclipse.DeferUntilPostDiet;
+import lombok.eclipse.EclipseAnnotationHandler;
+import lombok.eclipse.EclipseNode;
+
+import static lombok.eclipse.Eclipse.*;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
+
+@DeferUntilPostDiet
+@ProviderFor(EclipseAnnotationHandler.class)
+public class NonNullHandler extends EclipseAnnotationHandler {
+ @Override public void handle(AnnotationValues annotation, Annotation ast, EclipseNode annotationNode) {
+ if (annotationNode.up().getKind() == Kind.FIELD) {
+ // This is meaningless unless the field is used to generate a method (@Setter, @RequiredArgsConstructor, etc),
+ // but in that case those handlers will take care of it. However, we DO check if the annotation is applied to
+ // a primitive, because those handlers trigger on any annotation named @NonNull and we only want the warning
+ // behaviour on _OUR_ 'lombok.NonNull'.
+
+ try {
+ if (isPrimitive(((AbstractVariableDeclaration) annotationNode.up().get()).type)) {
+ annotationNode.addWarning("@NonNull is meaningless on a primitive.");
+ }
+ } catch (Exception ignore) {}
+
+ return;
+ }
+
+ if (annotationNode.up().getKind() != Kind.ARGUMENT) return;
+
+ Argument arg;
+ AbstractMethodDeclaration declaration;
+
+ try {
+ arg = (Argument) annotationNode.up().get();
+ declaration = (AbstractMethodDeclaration) annotationNode.up().up().get();
+ } catch (Exception e) {
+ return;
+ }
+
+ if (isGenerated(declaration)) return;
+
+ // Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter,
+ // and if they exist, create a new method in the class: 'private static T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and
+ // wrap all references to it in the super/this to a call to this method.
+
+ Statement nullCheck = generateNullCheck(arg, ast);
+
+ if (nullCheck == null) {
+ // @NonNull applied to a primitive. Kinda pointless. Let's generate a warning.
+ annotationNode.addWarning("@NonNull is meaningless on a primitive.");
+ return;
+ }
+
+ if (declaration.statements == null) {
+ declaration.statements = new Statement[] {nullCheck};
+ } else {
+ char[] expectedName = arg.name;
+ for (Statement stat : declaration.statements) {
+ char[] varNameOfNullCheck = returnVarNameIfNullCheck(stat);
+ if (varNameOfNullCheck == null) break;
+ if (Arrays.equals(expectedName, varNameOfNullCheck)) return;
+ }
+
+ Statement[] newStatements = new Statement[declaration.statements.length + 1];
+ int skipOver = 0;
+ for (Statement stat : declaration.statements) {
+ if (isGenerated(stat)) skipOver++;
+ else break;
+ }
+ System.arraycopy(declaration.statements, 0, newStatements, 0, skipOver);
+ System.arraycopy(declaration.statements, skipOver, newStatements, skipOver + 1, declaration.statements.length - skipOver);
+ newStatements[skipOver] = nullCheck;
+ declaration.statements = newStatements;
+ }
+ annotationNode.up().up().rebuild();
+ }
+
+ private char[] returnVarNameIfNullCheck(Statement stat) {
+ if (!(stat instanceof IfStatement)) return null;
+
+ /* Check that the if's statement is a throw statement, possibly in a block. */ {
+ Statement then = ((IfStatement) stat).thenStatement;
+ if (then instanceof Block) {
+ Statement[] blockStatements = ((Block) then).statements;
+ if (blockStatements == null || blockStatements.length == 0) return null;
+ then = blockStatements[0];
+ }
+
+ if (!(then instanceof ThrowStatement)) return null;
+ }
+
+ /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate
+ a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ {
+ Expression cond = ((IfStatement) stat).condition;
+ if (!(cond instanceof EqualExpression)) return null;
+ EqualExpression bin = (EqualExpression) cond;
+ int operatorId = ((bin.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT);
+ if (operatorId != OperatorIds.EQUAL_EQUAL) return null;
+ if (!(bin.left instanceof SingleNameReference)) return null;
+ if (!(bin.right instanceof NullLiteral)) return null;
+ return ((SingleNameReference) bin.left).token;
+ }
+ }
+}
diff --git a/src/core/lombok/javac/handlers/HandleSneakyThrows.java b/src/core/lombok/javac/handlers/HandleSneakyThrows.java
index c2394fc8..c818f630 100644
--- a/src/core/lombok/javac/handlers/HandleSneakyThrows.java
+++ b/src/core/lombok/javac/handlers/HandleSneakyThrows.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 The Project Lombok Authors.
+ * Copyright (C) 2009-2013 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -36,8 +36,6 @@ import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
-import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
-import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCBlock;
@@ -114,14 +112,6 @@ public class HandleSneakyThrows extends JavacAnnotationHandler {
}
}
- private boolean isConstructorCall(final JCStatement supect) {
- if (!(supect instanceof JCExpressionStatement)) return false;
- final JCExpression supectExpression = ((JCExpressionStatement) supect).expr;
- if (!(supectExpression instanceof JCMethodInvocation)) return false;
- final String methodName = ((JCMethodInvocation) supectExpression).meth.toString();
- return "super".equals(methodName) || "this".equals(methodName);
- }
-
private JCStatement buildTryCatchBlock(JavacNode node, List contents, String exception, JCTree source) {
TreeMaker maker = node.getTreeMaker();
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index ef1a9f50..7cbaa5ac 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -50,9 +50,11 @@ import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.JCTree.JCAssign;
+import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCImport;
@@ -121,6 +123,7 @@ public class JavacHandlerUtil {
}
public static T recursiveSetGeneratedBy(T node, JCTree source) {
+ if (node == null) return null;
setGeneratedBy(node, source);
node.accept(new MarkingScanner(source));
@@ -543,6 +546,23 @@ public class JavacHandlerUtil {
return MemberExistsResult.NOT_EXISTS;
}
+ public static boolean isConstructorCall(final JCStatement statement) {
+ if (!(statement instanceof JCExpressionStatement)) return false;
+ JCExpression expr = ((JCExpressionStatement) statement).expr;
+ if (!(expr instanceof JCMethodInvocation)) return false;
+ JCExpression invocation = ((JCMethodInvocation) expr).meth;
+ String name;
+ if (invocation instanceof JCFieldAccess) {
+ name = ((JCFieldAccess) invocation).name.toString();
+ } else if (invocation instanceof JCIdent) {
+ name = ((JCIdent) invocation).name.toString();
+ } else {
+ name = "";
+ }
+
+ return "super".equals(name) || "this".equals(name);
+ }
+
/**
* Turns an {@code AccessLevel} instance into the flag bit used by javac.
*/
@@ -890,7 +910,8 @@ public class JavacHandlerUtil {
JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException");
JCTree exception = treeMaker.NewClass(null, List.nil(), npe, List.of(treeMaker.Literal(fieldName.toString())), null);
JCStatement throwStatement = treeMaker.Throw(exception);
- return treeMaker.If(treeMaker.Binary(CTC_EQUAL, treeMaker.Ident(fieldName), treeMaker.Literal(CTC_BOT, null)), throwStatement, null);
+ JCBlock throwBlock = treeMaker.Block(0, List.of(throwStatement));
+ return treeMaker.If(treeMaker.Binary(CTC_EQUAL, treeMaker.Ident(fieldName), treeMaker.Literal(CTC_BOT, null)), throwBlock, null);
}
/**
diff --git a/src/core/lombok/javac/handlers/NonNullHandler.java b/src/core/lombok/javac/handlers/NonNullHandler.java
new file mode 100644
index 00000000..415d6032
--- /dev/null
+++ b/src/core/lombok/javac/handlers/NonNullHandler.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2013 The Project Lombok Authors.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package lombok.javac.handlers;
+
+import static lombok.javac.Javac.*;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
+
+import org.mangosdk.spi.ProviderFor;
+
+import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+import com.sun.tools.javac.tree.JCTree.JCBinary;
+import com.sun.tools.javac.tree.JCTree.JCBlock;
+import com.sun.tools.javac.tree.JCTree.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCIdent;
+import com.sun.tools.javac.tree.JCTree.JCIf;
+import com.sun.tools.javac.tree.JCTree.JCLiteral;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
+import com.sun.tools.javac.tree.JCTree.JCParens;
+import com.sun.tools.javac.tree.JCTree.JCStatement;
+import com.sun.tools.javac.tree.JCTree.JCThrow;
+import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
+import com.sun.tools.javac.util.List;
+
+import lombok.NonNull;
+import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacNode;
+
+@ProviderFor(JavacAnnotationHandler.class)
+public class NonNullHandler extends JavacAnnotationHandler {
+ @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) {
+ if (annotationNode.up().getKind() == Kind.FIELD) {
+ // This is meaningless unless the field is used to generate a method (@Setter, @RequiredArgsConstructor, etc),
+ // but in that case those handlers will take care of it. However, we DO check if the annotation is applied to
+ // a primitive, because those handlers trigger on any annotation named @NonNull and we only want the warning
+ // behaviour on _OUR_ 'lombok.NonNull'.
+
+ try {
+ if (isPrimitive(((JCVariableDecl) annotationNode.up().get()).vartype)) {
+ annotationNode.addWarning("@NonNull is meaningless on a primitive.");
+ }
+ } catch (Exception ignore) {}
+
+ return;
+ }
+
+ if (annotationNode.up().getKind() != Kind.ARGUMENT) return;
+
+ JCMethodDecl declaration;
+
+ try {
+ declaration = (JCMethodDecl) annotationNode.up().up().get();
+ } catch (Exception e) {
+ return;
+ }
+
+ if (JavacHandlerUtil.isGenerated(declaration)) return;
+
+ // Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter,
+ // and if they exist, create a new method in the class: 'private static T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and
+ // wrap all references to it in the super/this to a call to this method.
+
+ JCStatement nullCheck = recursiveSetGeneratedBy(generateNullCheck(annotationNode.getTreeMaker(), annotationNode.up()), ast);
+
+ if (nullCheck == null) {
+ // @NonNull applied to a primitive. Kinda pointless. Let's generate a warning.
+ annotationNode.addWarning("@NonNull is meaningless on a primitive.");
+ return;
+ }
+
+ List statements = declaration.body.stats;
+
+ String expectedName = annotationNode.up().getName();
+ for (JCStatement stat : statements) {
+ if (JavacHandlerUtil.isConstructorCall(stat)) continue;
+ String varNameOfNullCheck = returnVarNameIfNullCheck(stat);
+ if (varNameOfNullCheck == null) break;
+ if (varNameOfNullCheck.equals(expectedName)) return;
+ }
+
+ List tail = statements;
+ List head = List.nil();
+ for (JCStatement stat : statements) {
+ if (JavacHandlerUtil.isConstructorCall(stat) || JavacHandlerUtil.isGenerated(stat)) {
+ tail = tail.tail;
+ head = head.prepend(stat);
+ continue;
+ }
+ break;
+ }
+
+ List newList = tail.prepend(nullCheck);
+ for (JCStatement stat : head) newList = newList.prepend(stat);
+ declaration.body.stats = newList;
+ }
+
+ /**
+ * Checks if the statement is of the form 'if (x == null) {throw WHATEVER;},
+ * where the block braces are optional. If it is of this form, returns "x".
+ * If it is not of this form, returns null.
+ */
+ private String returnVarNameIfNullCheck(JCStatement stat) {
+ if (!(stat instanceof JCIf)) return null;
+
+ /* Check that the if's statement is a throw statement, possibly in a block. */ {
+ JCStatement then = ((JCIf) stat).thenpart;
+ if (then instanceof JCBlock) {
+ List stats = ((JCBlock) then).stats;
+ if (stats.length() == 0) return null;
+ then = stats.get(0);
+ }
+ if (!(then instanceof JCThrow)) return null;
+ }
+
+ /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate
+ a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ {
+ JCExpression cond = ((JCIf) stat).cond;
+ while (cond instanceof JCParens) cond = ((JCParens) cond).expr;
+ if (!(cond instanceof JCBinary)) return null;
+ JCBinary bin = (JCBinary) cond;
+ if (getTag(bin) != CTC_EQUAL) return null;
+ if (!(bin.lhs instanceof JCIdent)) return null;
+ if (!(bin.rhs instanceof JCLiteral)) return null;
+ if (((JCLiteral) bin.rhs).typetag != CTC_BOT) return null;
+ return ((JCIdent) bin.lhs).name.toString();
+ }
+ }
+}
diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java
index b4e58b8f..08c7c957 100644
--- a/src/utils/lombok/javac/Javac.java
+++ b/src/utils/lombok/javac/Javac.java
@@ -21,6 +21,8 @@
*/
package lombok.javac;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -129,4 +131,34 @@ public class Javac {
throw new RuntimeException(e);
}
}
+
+ private static final Field JCTREE_TAG;
+ private static final Method JCTREE_GETTAG;
+ static {
+ Field f = null;
+ try {
+ f = JCTree.class.getDeclaredField("tag");
+ } catch (NoSuchFieldException e) {}
+ JCTREE_TAG = f;
+
+ Method m = null;
+ try {
+ m = JCTree.class.getDeclaredMethod("getTag");
+ } catch (NoSuchMethodException e) {}
+ JCTREE_GETTAG = m;
+ }
+
+ public static int getTag(JCTree node) {
+ if (JCTREE_GETTAG != null) {
+ try {
+ return (Integer) JCTREE_GETTAG.invoke(node);
+ } catch (Exception e) {}
+ }
+ try {
+ return (Integer) JCTREE_TAG.get(node);
+ } catch (Exception e) {
+ throw new IllegalStateException("Can't get node tag");
+ }
+ }
+
}
diff --git a/test/transform/resource/after-delombok/DataOnLocalClass.java b/test/transform/resource/after-delombok/DataOnLocalClass.java
index ed4d30ca..abe2757b 100644
--- a/test/transform/resource/after-delombok/DataOnLocalClass.java
+++ b/test/transform/resource/after-delombok/DataOnLocalClass.java
@@ -63,7 +63,9 @@ class DataOnLocalClass2 {
String name;
@java.lang.SuppressWarnings("all")
public InnerLocal(@lombok.NonNull final String name) {
- if (name == null) throw new java.lang.NullPointerException("name");
+ if (name == null) {
+ throw new java.lang.NullPointerException("name");
+ }
this.name = name;
}
@lombok.NonNull
@@ -73,7 +75,9 @@ class DataOnLocalClass2 {
}
@java.lang.SuppressWarnings("all")
public void setName(@lombok.NonNull final String name) {
- if (name == null) throw new java.lang.NullPointerException("name");
+ if (name == null) {
+ throw new java.lang.NullPointerException("name");
+ }
this.name = name;
}
@java.lang.Override
diff --git a/test/transform/resource/after-delombok/NonNullOnParameter.java b/test/transform/resource/after-delombok/NonNullOnParameter.java
new file mode 100644
index 00000000..a27d19c9
--- /dev/null
+++ b/test/transform/resource/after-delombok/NonNullOnParameter.java
@@ -0,0 +1,48 @@
+class NonNullOnParameter extends Thread {
+ NonNullOnParameter(@lombok.NonNull String arg) {
+ this(arg, "");
+ if (arg == null) {
+ throw new java.lang.NullPointerException("arg");
+ }
+ }
+ NonNullOnParameter(@lombok.NonNull String arg, @lombok.NonNull String arg2) {
+ super(arg);
+ if (arg2 == null) {
+ throw new java.lang.NullPointerException("arg2");
+ }
+ if (arg == null) throw new NullPointerException();
+ }
+ public void test2(@lombok.NonNull String arg, @lombok.NonNull String arg2, @lombok.NonNull String arg3) {
+ if (arg == null) {
+ throw new java.lang.NullPointerException("arg");
+ }
+ if (arg3 == null) {
+ throw new java.lang.NullPointerException("arg3");
+ }
+ if (arg2 == null) {
+ throw new NullPointerException("arg2");
+ }
+ if (arg == null) System.out.println("Hello");
+ }
+ public void test3(@lombok.NonNull String arg) {
+ if (arg == null) {
+ throw new java.lang.NullPointerException("arg");
+ }
+ if (arg != null) throw new IllegalStateException();
+ }
+ public void test(@lombok.NonNull String stringArg, @lombok.NonNull String arg2, @lombok.NonNull int primitiveArg) {
+ if (stringArg == null) {
+ throw new java.lang.NullPointerException("stringArg");
+ }
+ if (arg2 == null) {
+ throw new java.lang.NullPointerException("arg2");
+ }
+ }
+ public void test(@lombok.NonNull String arg) {
+ if (arg == null) {
+ throw new java.lang.NullPointerException("arg");
+ }
+ System.out.println("Hey");
+ if (arg == null) throw new NullPointerException();
+ }
+}
diff --git a/test/transform/resource/after-delombok/NonNullPlain.java b/test/transform/resource/after-delombok/NonNullPlain.java
index 064e00b9..6b85cbf7 100644
--- a/test/transform/resource/after-delombok/NonNullPlain.java
+++ b/test/transform/resource/after-delombok/NonNullPlain.java
@@ -16,7 +16,9 @@ class NonNullPlain {
@java.beans.ConstructorProperties({"i", "s"})
@java.lang.SuppressWarnings("all")
public NonNullPlain(@lombok.NonNull final int i, @lombok.NonNull final String s) {
- if (s == null) throw new java.lang.NullPointerException("s");
+ if (s == null) {
+ throw new java.lang.NullPointerException("s");
+ }
this.i = i;
this.s = s;
}
@@ -45,7 +47,9 @@ class NonNullPlain {
@java.lang.SuppressWarnings("all")
public void setS(@lombok.NonNull final String s) {
- if (s == null) throw new java.lang.NullPointerException("s");
+ if (s == null) {
+ throw new java.lang.NullPointerException("s");
+ }
this.s = s;
}
diff --git a/test/transform/resource/after-delombok/SetterOnClass.java b/test/transform/resource/after-delombok/SetterOnClass.java
index 151bc179..7077c492 100644
--- a/test/transform/resource/after-delombok/SetterOnClass.java
+++ b/test/transform/resource/after-delombok/SetterOnClass.java
@@ -53,7 +53,9 @@ class SetterOnClass6 {
}
@java.lang.SuppressWarnings("all")
public void setNonNull(@lombok.NonNull final String nonNull) {
- if (nonNull == null) throw new java.lang.NullPointerException("nonNull");
+ if (nonNull == null) {
+ throw new java.lang.NullPointerException("nonNull");
+ }
this.nonNull = nonNull;
}
}
\ No newline at end of file
diff --git a/test/transform/resource/after-delombok/WitherOnClass.java b/test/transform/resource/after-delombok/WitherOnClass.java
index 783fede1..45d0c4b5 100644
--- a/test/transform/resource/after-delombok/WitherOnClass.java
+++ b/test/transform/resource/after-delombok/WitherOnClass.java
@@ -35,7 +35,9 @@ class WitherOnClass3 {
}
@java.lang.SuppressWarnings("all")
public WitherOnClass3 withNonNull(@lombok.NonNull final String nonNull) {
- if (nonNull == null) throw new java.lang.NullPointerException("nonNull");
+ if (nonNull == null) {
+ throw new java.lang.NullPointerException("nonNull");
+ }
return this.nonNull == nonNull ? this : new WitherOnClass3(this.couldBeNull, nonNull);
}
}
diff --git a/test/transform/resource/after-ecj/DataOnLocalClass.java b/test/transform/resource/after-ecj/DataOnLocalClass.java
index 137edf50..2f8dcca1 100644
--- a/test/transform/resource/after-ecj/DataOnLocalClass.java
+++ b/test/transform/resource/after-ecj/DataOnLocalClass.java
@@ -63,7 +63,9 @@ class DataOnLocalClass2 {
}
public @java.lang.SuppressWarnings("all") void setName(final @lombok.NonNull String name) {
if ((name == null))
- throw new java.lang.NullPointerException("name");
+ {
+ throw new java.lang.NullPointerException("name");
+ }
this.name = name;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final java.lang.Object o) {
@@ -96,7 +98,9 @@ class DataOnLocalClass2 {
public @java.lang.SuppressWarnings("all") InnerLocal(final @lombok.NonNull String name) {
super();
if ((name == null))
- throw new java.lang.NullPointerException("name");
+ {
+ throw new java.lang.NullPointerException("name");
+ }
this.name = name;
}
}
diff --git a/test/transform/resource/after-ecj/NonNullOnParameter.java b/test/transform/resource/after-ecj/NonNullOnParameter.java
new file mode 100644
index 00000000..bbceb153
--- /dev/null
+++ b/test/transform/resource/after-ecj/NonNullOnParameter.java
@@ -0,0 +1,61 @@
+class NonNullOnParameter extends Thread {
+ NonNullOnParameter(@lombok.NonNull String arg) {
+ this(arg, "");
+ if ((arg == null))
+ {
+ throw new java.lang.NullPointerException("arg");
+ }
+ }
+ NonNullOnParameter(@lombok.NonNull String arg, @lombok.NonNull String arg2) {
+ super(arg);
+ if ((arg2 == null))
+ {
+ throw new java.lang.NullPointerException("arg2");
+ }
+ if ((arg == null))
+ throw new NullPointerException();
+ }
+ public void test2(@lombok.NonNull String arg, @lombok.NonNull String arg2, @lombok.NonNull String arg3) {
+ if ((arg == null))
+ {
+ throw new java.lang.NullPointerException("arg");
+ }
+ if ((arg3 == null))
+ {
+ throw new java.lang.NullPointerException("arg3");
+ }
+ if ((arg2 == null))
+ {
+ throw new NullPointerException("arg2");
+ }
+ if ((arg == null))
+ System.out.println("Hello");
+ }
+ public void test3(@lombok.NonNull String arg) {
+ if ((arg == null))
+ {
+ throw new java.lang.NullPointerException("arg");
+ }
+ if ((arg != null))
+ throw new IllegalStateException();
+ }
+ public void test(@lombok.NonNull String stringArg, @lombok.NonNull String arg2, @lombok.NonNull int primitiveArg) {
+ if ((stringArg == null))
+ {
+ throw new java.lang.NullPointerException("stringArg");
+ }
+ if ((arg2 == null))
+ {
+ throw new java.lang.NullPointerException("arg2");
+ }
+ }
+ public void test(@lombok.NonNull String arg) {
+ if ((arg == null))
+ {
+ throw new java.lang.NullPointerException("arg");
+ }
+ System.out.println("Hey");
+ if ((arg == null))
+ throw new NullPointerException();
+ }
+}
\ No newline at end of file
diff --git a/test/transform/resource/after-ecj/NonNullPlain.java b/test/transform/resource/after-ecj/NonNullPlain.java
index c9c96d0a..6e937f6a 100644
--- a/test/transform/resource/after-ecj/NonNullPlain.java
+++ b/test/transform/resource/after-ecj/NonNullPlain.java
@@ -8,7 +8,9 @@ import java.lang.annotation.*;
public @java.beans.ConstructorProperties({"i", "s"}) @java.lang.SuppressWarnings("all") NonNullPlain(final @lombok.NonNull int i, final @lombok.NonNull String s) {
super();
if ((s == null))
- throw new java.lang.NullPointerException("s");
+ {
+ throw new java.lang.NullPointerException("s");
+ }
this.i = i;
this.s = s;
}
@@ -26,7 +28,9 @@ import java.lang.annotation.*;
}
public @java.lang.SuppressWarnings("all") void setS(final @lombok.NonNull String s) {
if ((s == null))
- throw new java.lang.NullPointerException("s");
+ {
+ throw new java.lang.NullPointerException("s");
+ }
this.s = s;
}
public @java.lang.SuppressWarnings("all") void setO(final Object o) {
diff --git a/test/transform/resource/after-ecj/SetterOnClass.java b/test/transform/resource/after-ecj/SetterOnClass.java
index da928f24..aa3459bb 100644
--- a/test/transform/resource/after-ecj/SetterOnClass.java
+++ b/test/transform/resource/after-ecj/SetterOnClass.java
@@ -63,7 +63,9 @@
}
public @java.lang.SuppressWarnings("all") void setNonNull(final @lombok.NonNull String nonNull) {
if ((nonNull == null))
- throw new java.lang.NullPointerException("nonNull");
+ {
+ throw new java.lang.NullPointerException("nonNull");
+ }
this.nonNull = nonNull;
}
}
diff --git a/test/transform/resource/after-ecj/WitherOnClass.java b/test/transform/resource/after-ecj/WitherOnClass.java
index ff4566e5..82132e87 100644
--- a/test/transform/resource/after-ecj/WitherOnClass.java
+++ b/test/transform/resource/after-ecj/WitherOnClass.java
@@ -33,7 +33,9 @@
}
public @java.lang.SuppressWarnings("all") WitherOnClass3 withNonNull(final @lombok.NonNull String nonNull) {
if ((nonNull == null))
- throw new java.lang.NullPointerException("nonNull");
+ {
+ throw new java.lang.NullPointerException("nonNull");
+ }
return ((this.nonNull == nonNull) ? this : new WitherOnClass3(this.couldBeNull, nonNull));
}
}
diff --git a/test/transform/resource/before/NonNullOnParameter.java b/test/transform/resource/before/NonNullOnParameter.java
new file mode 100644
index 00000000..7eb4c565
--- /dev/null
+++ b/test/transform/resource/before/NonNullOnParameter.java
@@ -0,0 +1,30 @@
+class NonNullOnParameter extends Thread {
+ NonNullOnParameter(@lombok.NonNull String arg) {
+ this(arg, "");
+ }
+
+ NonNullOnParameter(@lombok.NonNull String arg, @lombok.NonNull String arg2) {
+ super(arg);
+ if (arg == null) throw new NullPointerException();
+ }
+
+ public void test2(@lombok.NonNull String arg, @lombok.NonNull String arg2, @lombok.NonNull String arg3) {
+ if (arg2 == null) {
+ throw new NullPointerException("arg2");
+ }
+ if (arg == null) System.out.println("Hello");
+ }
+
+ public void test3(@lombok.NonNull String arg) {
+ if (arg != null) throw new IllegalStateException();
+ }
+
+ public void test(@lombok.NonNull String stringArg, @lombok.NonNull String arg2, @lombok.NonNull int primitiveArg) {
+
+ }
+
+ public void test(@lombok.NonNull String arg) {
+ System.out.println("Hey");
+ if (arg == null) throw new NullPointerException();
+ }
+}
\ No newline at end of file
diff --git a/test/transform/resource/messages-delombok/NonNullOnParameter.java.messages b/test/transform/resource/messages-delombok/NonNullOnParameter.java.messages
new file mode 100644
index 00000000..ac87adcd
--- /dev/null
+++ b/test/transform/resource/messages-delombok/NonNullOnParameter.java.messages
@@ -0,0 +1 @@
+22:89 @NonNull is meaningless on a primitive.
diff --git a/test/transform/resource/messages-delombok/NonNullPlain.java.messages b/test/transform/resource/messages-delombok/NonNullPlain.java.messages
new file mode 100644
index 00000000..67eb8abe
--- /dev/null
+++ b/test/transform/resource/messages-delombok/NonNullPlain.java.messages
@@ -0,0 +1 @@
+7:9 @NonNull is meaningless on a primitive.
\ No newline at end of file
diff --git a/test/transform/resource/messages-ecj/NonNullOnParameter.java.messages b/test/transform/resource/messages-ecj/NonNullOnParameter.java.messages
new file mode 100644
index 00000000..1539929b
--- /dev/null
+++ b/test/transform/resource/messages-ecj/NonNullOnParameter.java.messages
@@ -0,0 +1,3 @@
+15:460 Dead code
+22:683 @NonNull is meaningless on a primitive.
+28:823 Dead code
\ No newline at end of file
diff --git a/test/transform/resource/messages-ecj/NonNullPlain.java.messages b/test/transform/resource/messages-ecj/NonNullPlain.java.messages
new file mode 100644
index 00000000..96eed252
--- /dev/null
+++ b/test/transform/resource/messages-ecj/NonNullPlain.java.messages
@@ -0,0 +1 @@
+7:116 @NonNull is meaningless on a primitive.
\ No newline at end of file
diff --git a/test/transform/resource/messages-idempotent/NonNullOnParameter.java.messages b/test/transform/resource/messages-idempotent/NonNullOnParameter.java.messages
new file mode 100644
index 00000000..fd23a32a
--- /dev/null
+++ b/test/transform/resource/messages-idempotent/NonNullOnParameter.java.messages
@@ -0,0 +1 @@
+33:89 @NonNull is meaningless on a primitive.
\ No newline at end of file
diff --git a/test/transform/resource/messages-idempotent/NonNullPlain.java.messages b/test/transform/resource/messages-idempotent/NonNullPlain.java.messages
new file mode 100644
index 00000000..c48da311
--- /dev/null
+++ b/test/transform/resource/messages-idempotent/NonNullPlain.java.messages
@@ -0,0 +1,3 @@
+4:9 @NonNull is meaningless on a primitive.
+18:29 @NonNull is meaningless on a primitive.
+44:26 @NonNull is meaningless on a primitive.
\ No newline at end of file
diff --git a/usage_examples/NonNullExample_post.jpage b/usage_examples/NonNullExample_post.jpage
new file mode 100644
index 00000000..24175e06
--- /dev/null
+++ b/usage_examples/NonNullExample_post.jpage
@@ -0,0 +1,13 @@
+import lombok.NonNull;
+
+public class NonNullExample extends Something {
+ private String name;
+
+ public NonNullExample(@NonNull Person person) {
+ super("Hello");
+ if (person == null) {
+ throw new NullPointerException("person");
+ }
+ this.name = person.getName();
+ }
+}
diff --git a/usage_examples/NonNullExample_pre.jpage b/usage_examples/NonNullExample_pre.jpage
new file mode 100644
index 00000000..47556ce7
--- /dev/null
+++ b/usage_examples/NonNullExample_pre.jpage
@@ -0,0 +1,10 @@
+import lombok.NonNull;
+
+public class NonNullExample extends Something {
+ private String name;
+
+ public NonNullExample(@NonNull Person person) {
+ super("Hello");
+ this.name = person.getName();
+ }
+}
diff --git a/website/features/Cleanup.html b/website/features/Cleanup.html
index d1637dd4..a6e41f39 100644
--- a/website/features/Cleanup.html
+++ b/website/features/Cleanup.html
@@ -60,7 +60,7 @@
diff --git a/website/features/Data.html b/website/features/Data.html
index 8ace96cb..1c8510b7 100644
--- a/website/features/Data.html
+++ b/website/features/Data.html
@@ -75,7 +75,7 @@
diff --git a/website/features/Delegate.html b/website/features/Delegate.html
index 532f3f54..02cdf290 100644
--- a/website/features/Delegate.html
+++ b/website/features/Delegate.html
@@ -55,16 +55,15 @@
When passing classes to the annotation's types or excludes parameter, you cannot include generics.
This is a limitation of java. Use private inner interfaces or classes that extend the intended type including the
generics parameter to work around this problem.
-
-
+
When passing classes to the annotation, these classes do not need to be supertypes of the field. See the example.
-
-
+
@Delegate cannot be used on static fields or methods.
-
or: How I learned to stop worrying and love the NullPointerException.
+
+
Overview
+
+ NEW in Lombok 0.11.10: You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.
+
+ Lombok has always treated any annotation named @NonNull on a field as a signal to generate a null-check if lombok generates an entire method or constructor for you, via
+ for example @Data. Now, however, using lombok's own @lombok.NonNull on a parameter results in the insertion of just the null-check
+ statement inside your own method or constructor.
+
+ The null-check looks like if (param == null) throw new NullPointerException("param"); and will be inserted at the very top of your method. For constructors, the null-check
+ will be inserted immediately following any explicit this() or super() calls.
+
+ If a null-check is already present at the top, no additional null-check will be generated.
+
+
+
+
+
With Lombok
+
@HTML_PRE@
+
+
+
+
Vanilla Java
+
@HTML_POST@
+
+
+
+
+
Small print
+
+ Lombok's detection scheme for already existing null-checks consists of scanning for if statements that look just like lombok's own. Any 'throws' statement as
+ the 'then' part of the if statement, whether in braces or not, counts. The conditional of the if statement must look exactly like PARAMNAME == null.
+ The first statement in your method that is not such a null-check stops the process of inspecting for null-checks.
+
+ While @Data and other method-generating lombok annotations will trigger on any annotation named @NonNull regardless of casing or package name,
+ this feature only triggers on lombok's own @NonNull annotation from the lombok package.
+
+ A @NonNull on a primitive parameter results in a warning. No null-check will be generated.
+
Because @SneakyThrows is an implementation detail and not part of your method signature, it is an error if you try to
declare a checked exception as sneakily thrown when you don't call any methods that throw this exception. (Doing so is perfectly legal
for throws statements to accommodate subclasses). Similarly, @SneakyThrows does not inherit.
@@ -72,7 +70,7 @@
All together now: A shortcut for @ToString, @EqualsAndHashCode,
@Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!
It's like drinking tea with an extended pinky while wearing a monocle: No-hassle fancy-pants APIs for object creation!
+
+
Since
+
+ @Builder was introduced as experimental feature in lombok v0.11.10.
+
+
+
+
Experimental
+
+ Experimental because:
+
+
New feature - community feedback requested.
+
This feature will move to the core package soon.
+
+ Current status: sure thing - This feature will move to the core package soon.
+
+
+
Overview
+
+ The @Builder annotation produces complex builder APIs for your classes.
+
+ @Builder lets you automatically produce the code required to have your class be instantiable with code such as:
+ Person.builder().name("Adam Savage").city("San Francisco").worksAt("Mythbusters").build();
+
+ @Builder can be placed on a class, or on a constructor, or on a static method. While the "on a class" and "on a constructor"
+ mode are the most common use-case, @Builder is most easily explained with the "static method" use-case.
+
+ A static method annotated with @Builder (from now on called the target) causes the following 7 things to be generated:
+
An inner static class named FooBuilder, with the same type arguments as the static method (called the builder).
+
In the builder: One private non-static non-final field for each parameter of the target.
+
In the builder: A package private no-args empty constructor.
+
In the builder: A 'setter'-like method for each parmeter of the target: It has the same type as that parameter and the same name.
+ It returns the builder itself, so that the setter calls can be chained, as in the above example.
+
In the builder: A build() method which calls the static method, passing in each field. It returns the same type that the
+ target returns.
+
In the builder: A sensible toString() implementation.
+
In the class containing the target: A builder() method, which creates a new instance of the builder.
+
+ Each listed generated element will be silently skipped if that element already exists (disregarding parameter counts and looking only at names). This
+ includes the builder itself: If that class already exists, lombok will simply start injecting fields and methods inside this already existing
+ class, unless of course the fields / methods to be injected already exist.
+
+ Now that the "static method" mode is clear, putting a @Builder annotation on a constructor functions similarly; effectively,
+ constructors are just static methods that have a special syntax to invoke them: Their 'return type' is the class they construct, and their
+ type parameters are the same as the type parameters of the class itself.
+
+ Finally, applying @Builder to a class is as if you added @AllArgsConstructor(acces = AccessLevel.PACKAGE) to the class and applied the
+ @Builder annotation to this all-args-constructor. Note that this constructor is only generated if there is no explicit
+ constructor present in the so annotated class, but this @AllArgsConstructor has priority over any other implicitly
+ generated lombok constructor (such as @Data and @Value). If an explicit constructor is present, no constructor is generated,
+ but the builder will be created with the assumption that this constructor exists. If you've written another constructor, you'll get a compilation error.
+ The solution is to either let lombok write this constructor (delete your own), or, annotate your constructor instead.
+
+ The name of the builder class is FoobarBuilder, where Foobar is the simplified, title-cased form of the return type of the
+ target - that is, the name of your type for @Builder on constructors and types, and the name of the return type for @Builder
+ on static methods. For example, if @Builder is applied to a class named com.yoyodyne.FancyList<T>, then the builder name will be
+ FancyListBuilder<T>. If @Builder is applied to a static method that returns void, the builder will be named
+ VoidBuilder.
+
+ The only configurable aspect of builder are the builder's class name (default: return type + 'Builder'), the build() method's name, and the
+ builder() method's name:
+ @Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld")
+
+
+
+
+
With Lombok
+
@HTML_PRE@
+
+
+
+
Vanilla Java
+
@HTML_POST@
+
+
+
+
+
Small print
+
+ Another strategy for fluent APIs is that the programmer using your library statically imports your 'builder' method. In this case, you might want to name your builder
+ method equal to your type's name. So, the builder method for a class called Person would become person(). This is nicer if the builder method
+ is statically imported.
+
+ If the return type of your target static method is a type parameter (such as T), lombok will enforce an explicit builder class name.
+
+ You don't HAVE to use @Builder to build anything; you can for example put it on a static method that has lots of parameter to improve the API of it.
+ In this case, we suggest you use buildMethodName = to rename the build method to execute() instead.
+
+ The builder class will NOT get an auto-generated implementation of hashCode or equals methods! These would suggest that it is sensible to use
+ instances of a builder as keys in a set or map. However, that's not a sensible thing to do. Hence, no hashCode or equals.
+
+ Generics are sorted out for you.
+
+
+
+
+
+
+
+
+
+
diff --git a/website/features/experimental/index.html b/website/features/experimental/index.html
index 24fbb541..d0a086a0 100644
--- a/website/features/experimental/index.html
+++ b/website/features/experimental/index.html
@@ -22,6 +22,8 @@
Features that receive positive community feedback and which seem to produce clean, flexible code will eventually become accepted
as a core feature and move out of the experimental package.
--
cgit
From e7ff097fec867714b8a064b559dfc9e5162a489c Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Date: Mon, 8 Jul 2013 21:17:01 +0200
Subject: Fixed value's snippet integration (it hadn't been updated yet now
that Value has moved from experimental into core).
---
buildScripts/website.ant.xml | 6 +-
doc/changelog.markdown | 2 +-
src/core/lombok/javac/handlers/HandleWither.java | 5 +-
.../lombok/javac/handlers/JavacHandlerUtil.java | 27 +++--
.../resource/after-delombok/WitherDeprecated.java | 3 +
usage_examples/ValueExample_post.jpage | 120 +++++++++++++++++++++
usage_examples/ValueExample_pre.jpage | 19 ++++
.../experimental/ValueExample_post.jpage | 120 ---------------------
usage_examples/experimental/ValueExample_pre.jpage | 19 ----
website/features/experimental/Wither.html | 2 +
10 files changed, 170 insertions(+), 153 deletions(-)
create mode 100644 usage_examples/ValueExample_post.jpage
create mode 100644 usage_examples/ValueExample_pre.jpage
delete mode 100644 usage_examples/experimental/ValueExample_post.jpage
delete mode 100644 usage_examples/experimental/ValueExample_pre.jpage
(limited to 'buildScripts')
diff --git a/buildScripts/website.ant.xml b/buildScripts/website.ant.xml
index 521ca59b..baf869a4 100644
--- a/buildScripts/website.ant.xml
+++ b/buildScripts/website.ant.xml
@@ -142,6 +142,9 @@ such as converting the changelog into HTML, and creating javadoc.
+
+
+
@@ -163,9 +166,6 @@ such as converting the changelog into HTML, and creating javadoc.
-
-
-
diff --git a/doc/changelog.markdown b/doc/changelog.markdown
index 75825d77..d15e24be 100644
--- a/doc/changelog.markdown
+++ b/doc/changelog.markdown
@@ -2,7 +2,7 @@ Lombok Changelog
----------------
### v0.11.9 (Edgy Guinea Pig)
-* FEATURE: javadoc on fields will now be copied to generated getters / setters. There are ways to specify separate javadoc for the field, the setter, and the getter, and `@param` and `@return` are handled appropriately. Addresses feature request [Issue #59](https://code.google.com/p/projectlombok/issues/detail?id=59). [@Getter and @Setter documentation](http://projectlombok.org/features/GetterSetter.html).
+* FEATURE: javadoc on fields will now be copied to generated getters / setters / withers. There are ways to specify separate javadoc for the field, the setter, and the getter, and `@param` and `@return` are handled appropriately. Addresses feature request [Issue #59](https://code.google.com/p/projectlombok/issues/detail?id=59). [@Getter and @Setter documentation](http://projectlombok.org/features/GetterSetter.html). [@Wither documentation](http://projectlombok.org/features/experimental/Wither.html).
* CHANGE: The desugaring of @Getter(lazy=true) is now less object creation intensive. Documentation has been updated to reflect what the new desugaring looks like. [@Getter(lazy=true) documentation](http://projectlombok.org/features/GetterLazy.html).
* PROMOTION: `@Value` has been promoted from experimental to the main package with no changes. The 'old' experimental one is still around but is deprecated, and is an alias for the new main package one. [@Value documentation](http://projectlombok.org/features/Value.html).
* FEATURE: {Experimental} `@Builder` support. One of our earliest feature request issues, [Issue #16](https://code.google.com/p/projectlombok/issues/detail?id=16), has finally been addressed. [@Builder documentation](http://projectlombok.org/features/experimental/Builder.html).
diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java
index ba5aa72d..b3f218b8 100644
--- a/src/core/lombok/javac/handlers/HandleWither.java
+++ b/src/core/lombok/javac/handlers/HandleWither.java
@@ -33,6 +33,7 @@ import lombok.core.TransformationsUtil;
import lombok.experimental.Wither;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
+import lombok.javac.handlers.JavacHandlerUtil.CopyJavadoc;
import lombok.javac.handlers.JavacHandlerUtil.FieldAccess;
import org.mangosdk.spi.ProviderFor;
@@ -264,7 +265,9 @@ public class HandleWither extends JavacAnnotationHandler {
if (isFieldDeprecated(field)) {
annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.nil()));
}
- return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, returnType,
+ JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, returnType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source);
+ copyJavadoc(field, decl, CopyJavadoc.WITHER);
+ return decl;
}
}
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 6aed5508..a24dad7d 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1165,7 +1165,7 @@ public class JavacHandlerUtil {
return (JCExpression) in;
}
- private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
+ private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER|WITHER)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
private static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) {
Pattern p = Pattern.compile("^\\s*\\**\\s*" + regexpFragment + "\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
@@ -1213,17 +1213,26 @@ public class JavacHandlerUtil {
},
SETTER {
@Override public String[] split(String javadoc) {
- // step 1: Check if there is a 'SETTER' section. If yes, that becomes the new one and we strip that from the original.
- String[] out = splitJavadocOnSectionIfPresent(javadoc, "SETTER");
- if (out != null) return out;
- // failing that, create a copy, but strip @param from the original and @return from the copy.
- String copy = javadoc;
- javadoc = stripLinesWithTagFromJavadoc(javadoc, "@param(?:eter)?\\s+.*");
- copy = stripLinesWithTagFromJavadoc(copy, "@returns?\\s+.*");
- return new String[] {copy, javadoc};
+ return splitForSetters(javadoc, "SETTER");
+ }
+ },
+ WITHER {
+ @Override public String[] split(String javadoc) {
+ return splitForSetters(javadoc, "WITHER");
}
};
+ private static String[] splitForSetters(String javadoc, String sectionName) {
+ // step 1: Check if there is a 'SETTER' section. If yes, that becomes the new one and we strip that from the original.
+ String[] out = splitJavadocOnSectionIfPresent(javadoc, sectionName);
+ if (out != null) return out;
+ // failing that, create a copy, but strip @param from the original and @return from the copy.
+ String copy = javadoc;
+ javadoc = stripLinesWithTagFromJavadoc(javadoc, "@param(?:eter)?\\s+.*");
+ copy = stripLinesWithTagFromJavadoc(copy, "@returns?\\s+.*");
+ return new String[] {copy, javadoc};
+ }
+
/** Splits the javadoc into the section to be copied (ret[0]) and the section to replace the original with (ret[1]) */
public String[] split(String javadoc) {
return new String[] {javadoc, javadoc};
diff --git a/test/transform/resource/after-delombok/WitherDeprecated.java b/test/transform/resource/after-delombok/WitherDeprecated.java
index f076d90e..29192012 100644
--- a/test/transform/resource/after-delombok/WitherDeprecated.java
+++ b/test/transform/resource/after-delombok/WitherDeprecated.java
@@ -12,6 +12,9 @@ class WitherDeprecated {
public WitherDeprecated withAnnotation(final int annotation) {
return this.annotation == annotation ? this : new WitherDeprecated(annotation, this.javadoc);
}
+ /**
+ * @deprecated
+ */
@java.lang.Deprecated
@java.lang.SuppressWarnings("all")
public WitherDeprecated withJavadoc(final int javadoc) {
diff --git a/usage_examples/ValueExample_post.jpage b/usage_examples/ValueExample_post.jpage
new file mode 100644
index 00000000..ac9b64d1
--- /dev/null
+++ b/usage_examples/ValueExample_post.jpage
@@ -0,0 +1,120 @@
+import java.util.Arrays;
+
+public final class ValueExample {
+ private final String name;
+ private int age;
+ private final double score;
+ protected final String[] tags;
+
+ @java.beans.ConstructorProperties({"name", "age", "score", "tags"})
+ public ValueExample(String name, int age, double score, String[] tags) {
+ this.name = name;
+ this.age = age;
+ this.score = score;
+ this.tags = tags;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int getAge() {
+ return this.age;
+ }
+
+ public double getScore() {
+ return this.score;
+ }
+
+ public String[] getTags() {
+ return this.tags;
+ }
+
+ @java.lang.Override
+ public boolean equals(Object o) {
+ if (o == this) return true;
+ if (!(o instanceof ValueExample)) return false;
+ final ValueExample other = (ValueExample)o;
+ final Object this$name = this.getName();
+ final Object other$name = other.getName();
+ if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
+ if (this.getAge() != other.getAge()) return false;
+ if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
+ if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
+ return true;
+ }
+
+ @java.lang.Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ final Object $name = this.getName();
+ result = result * PRIME + ($name == null ? 0 : $name.hashCode());
+ result = result * PRIME + this.getAge();
+ final long $score = Double.doubleToLongBits(this.getScore());
+ result = result * PRIME + (int)($score >>> 32 ^ $score);
+ result = result * PRIME + Arrays.deepHashCode(this.getTags());
+ return result;
+ }
+
+ @java.lang.Override
+ public String toString() {
+ return "ValueExample(name=" + getName() + ", age=" + getAge() + ", score=" + getScore() + ", tags=" + Arrays.deepToString(getTags()) + ")";
+ }
+
+ ValueExample withAge(int age) {
+ return this.age == age ? this : new ValueExample(name, age, score, tags);
+ }
+
+ public static final class Exercise {
+ private final String name;
+ private final T value;
+
+ private Exercise(String name, T value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public static Exercise of(String name, T value) {
+ return new Exercise(name, value);
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public T getValue() {
+ return this.value;
+ }
+
+ @java.lang.Override
+ public boolean equals(Object o) {
+ if (o == this) return true;
+ if (!(o instanceof ValueExample.Exercise)) return false;
+ final Exercise> other = (Exercise>)o;
+ final Object this$name = this.getName();
+ final Object other$name = other.getName();
+ if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
+ final Object this$value = this.getValue();
+ final Object other$value = other.getValue();
+ if (this$value == null ? other$value != null : !this$value.equals(other$value)) return false;
+ return true;
+ }
+
+ @java.lang.Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ final Object $name = this.getName();
+ result = result * PRIME + ($name == null ? 0 : $name.hashCode());
+ final Object $value = this.getValue();
+ result = result * PRIME + ($value == null ? 0 : $value.hashCode());
+ return result;
+ }
+
+ @java.lang.Override
+ public String toString() {
+ return "ValueExample.Exercise(name=" + getName() + ", value=" + getValue() + ")";
+ }
+ }
+}
\ No newline at end of file
diff --git a/usage_examples/ValueExample_pre.jpage b/usage_examples/ValueExample_pre.jpage
new file mode 100644
index 00000000..d9550c25
--- /dev/null
+++ b/usage_examples/ValueExample_pre.jpage
@@ -0,0 +1,19 @@
+import lombok.AccessLevel;
+import lombok.experimental.NonFinal;
+import lombok.experimental.Value;
+import lombok.experimental.Wither;
+import lombok.ToString;
+
+@Value public class ValueExample {
+ String name;
+ @Wither(AccessLevel.PACKAGE) @NonFinal int age;
+ double score;
+ protected String[] tags;
+
+ @ToString(includeFieldNames=true)
+ @Value(staticConstructor="of")
+ public static class Exercise {
+ String name;
+ T value;
+ }
+}
diff --git a/usage_examples/experimental/ValueExample_post.jpage b/usage_examples/experimental/ValueExample_post.jpage
deleted file mode 100644
index ac9b64d1..00000000
--- a/usage_examples/experimental/ValueExample_post.jpage
+++ /dev/null
@@ -1,120 +0,0 @@
-import java.util.Arrays;
-
-public final class ValueExample {
- private final String name;
- private int age;
- private final double score;
- protected final String[] tags;
-
- @java.beans.ConstructorProperties({"name", "age", "score", "tags"})
- public ValueExample(String name, int age, double score, String[] tags) {
- this.name = name;
- this.age = age;
- this.score = score;
- this.tags = tags;
- }
-
- public String getName() {
- return this.name;
- }
-
- public int getAge() {
- return this.age;
- }
-
- public double getScore() {
- return this.score;
- }
-
- public String[] getTags() {
- return this.tags;
- }
-
- @java.lang.Override
- public boolean equals(Object o) {
- if (o == this) return true;
- if (!(o instanceof ValueExample)) return false;
- final ValueExample other = (ValueExample)o;
- final Object this$name = this.getName();
- final Object other$name = other.getName();
- if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
- if (this.getAge() != other.getAge()) return false;
- if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
- if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
- return true;
- }
-
- @java.lang.Override
- public int hashCode() {
- final int PRIME = 31;
- int result = 1;
- final Object $name = this.getName();
- result = result * PRIME + ($name == null ? 0 : $name.hashCode());
- result = result * PRIME + this.getAge();
- final long $score = Double.doubleToLongBits(this.getScore());
- result = result * PRIME + (int)($score >>> 32 ^ $score);
- result = result * PRIME + Arrays.deepHashCode(this.getTags());
- return result;
- }
-
- @java.lang.Override
- public String toString() {
- return "ValueExample(name=" + getName() + ", age=" + getAge() + ", score=" + getScore() + ", tags=" + Arrays.deepToString(getTags()) + ")";
- }
-
- ValueExample withAge(int age) {
- return this.age == age ? this : new ValueExample(name, age, score, tags);
- }
-
- public static final class Exercise {
- private final String name;
- private final T value;
-
- private Exercise(String name, T value) {
- this.name = name;
- this.value = value;
- }
-
- public static Exercise of(String name, T value) {
- return new Exercise(name, value);
- }
-
- public String getName() {
- return this.name;
- }
-
- public T getValue() {
- return this.value;
- }
-
- @java.lang.Override
- public boolean equals(Object o) {
- if (o == this) return true;
- if (!(o instanceof ValueExample.Exercise)) return false;
- final Exercise> other = (Exercise>)o;
- final Object this$name = this.getName();
- final Object other$name = other.getName();
- if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
- final Object this$value = this.getValue();
- final Object other$value = other.getValue();
- if (this$value == null ? other$value != null : !this$value.equals(other$value)) return false;
- return true;
- }
-
- @java.lang.Override
- public int hashCode() {
- final int PRIME = 31;
- int result = 1;
- final Object $name = this.getName();
- result = result * PRIME + ($name == null ? 0 : $name.hashCode());
- final Object $value = this.getValue();
- result = result * PRIME + ($value == null ? 0 : $value.hashCode());
- return result;
- }
-
- @java.lang.Override
- public String toString() {
- return "ValueExample.Exercise(name=" + getName() + ", value=" + getValue() + ")";
- }
- }
-}
\ No newline at end of file
diff --git a/usage_examples/experimental/ValueExample_pre.jpage b/usage_examples/experimental/ValueExample_pre.jpage
deleted file mode 100644
index d9550c25..00000000
--- a/usage_examples/experimental/ValueExample_pre.jpage
+++ /dev/null
@@ -1,19 +0,0 @@
-import lombok.AccessLevel;
-import lombok.experimental.NonFinal;
-import lombok.experimental.Value;
-import lombok.experimental.Wither;
-import lombok.ToString;
-
-@Value public class ValueExample {
- String name;
- @Wither(AccessLevel.PACKAGE) @NonFinal int age;
- double score;
- protected String[] tags;
-
- @ToString(includeFieldNames=true)
- @Value(staticConstructor="of")
- public static class Exercise {
- String name;
- T value;
- }
-}
diff --git a/website/features/experimental/Wither.html b/website/features/experimental/Wither.html
index 9cbcd5ed..b334cd7c 100644
--- a/website/features/experimental/Wither.html
+++ b/website/features/experimental/Wither.html
@@ -46,6 +46,8 @@
a 'wither' is generated for each field (even non-final fields).
To put annotations on the generated method, you can use onMethod=@_({@AnnotationsHere}); to put annotations on the only parameter of a generated wither method, you can use onParam=@_({@AnnotationsHere}). Be careful though! This is an experimental feature. For more details see the documentation on the onX feature.
+
+ NEW in lombok v1.12.0: javadoc on the field will now be copied to generated withers. Normally, all text is copied, and @param is moved to the wither, whilst @return 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 WITHER. 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, @return and @param stripping / copying for that section is no longer done (move the @param line into the section).