aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRawi01 <Rawi01@users.noreply.github.com>2020-06-30 17:22:38 +0200
committerRoel Spilker <r.spilker@gmail.com>2020-07-02 23:15:14 +0200
commit84556ebb00af1817d521bc011333a0ed02162cc4 (patch)
tree85ffc41181c0fb576e04d1808c49e3e358883105
parent9425e99b49d1a203c692fd2001ff3fd3d1612303 (diff)
downloadlombok-84556ebb00af1817d521bc011333a0ed02162cc4.tar.gz
lombok-84556ebb00af1817d521bc011333a0ed02162cc4.tar.bz2
lombok-84556ebb00af1817d521bc011333a0ed02162cc4.zip
[fixes #2433] Add config key to turn off @SuppressWarnings("all")
-rw-r--r--src/core/lombok/ConfigurationKeys.java7
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java6
-rw-r--r--src/core/lombok/eclipse/handlers/HandleGetter.java9
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java7
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java20
-rw-r--r--test/transform/resource/after-delombok/SkipSuppressWarnings.java24
-rw-r--r--test/transform/resource/after-ecj/SkipSuppressWarnings.java27
-rw-r--r--test/transform/resource/before/SkipSuppressWarnings.java9
-rw-r--r--test/transform/resource/messages-ecj/SkipSuppressWarnings.java.messages1
9 files changed, 96 insertions, 14 deletions
diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java
index 46cf7412..60b98a94 100644
--- a/src/core/lombok/ConfigurationKeys.java
+++ b/src/core/lombok/ConfigurationKeys.java
@@ -90,6 +90,13 @@ public class ConfigurationKeys {
public static final ConfigurationKey<Boolean> ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS = new ConfigurationKey<Boolean>("lombok.extern.findbugs.addSuppressFBWarnings", "Generate @edu.umd.cs.findbugs.annotations.SuppressFBWarnings on all generated code (default: false).") {};
/**
+ * lombok configuration: {@code lombok.addSuppressWarnings} = {@code true} | {@code false}.
+ *
+ * If {@code true}, lombok generates {@code @java.lang.SuppressWarnings("all")} on all fields, methods, and types that are generated.
+ */
+ public static final ConfigurationKey<Boolean> ADD_SUPPRESSWARNINGS_ANNOTATIONS = new ConfigurationKey<Boolean>("lombok.addSuppressWarnings", "Generate @java.lang.SuppressWarnings(\"all\") on all generated code (default: true).") {};
+
+ /**
* lombok configuration: {@code lombok.addNullAnnotations = }one of: [{@code none}, {@code javax}, {@code eclipse}, {@code jetbrains}, {@code netbeans}, {@code androidx}, {@code android.support}, {@code checkerframework}, {@code findbugs}, {@code spring}, {@code JML}, or a custom set of fully qualified annotation types].
*
* Lombok generally copies relevant nullity annotations from your source code to the right places. However, sometimes lombok generates code where the nullability of some node is not dependent on something in your source code. You can configure lombok to add an appropriate nullity annotation in this case.<ul>
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index e674409c..de76e32d 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -1962,7 +1962,11 @@ public class EclipseHandlerUtil {
private static final char[][] EDU_UMD_CS_FINDBUGS_ANNOTATIONS_SUPPRESSFBWARNINGS = Eclipse.fromQualifiedName("edu.umd.cs.findbugs.annotations.SuppressFBWarnings");
public static Annotation[] addSuppressWarningsAll(EclipseNode node, ASTNode source, Annotation[] originalAnnotationArray) {
- Annotation[] anns = addAnnotation(source, originalAnnotationArray, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, new StringLiteral(ALL, 0, 0, 0));
+ Annotation[] anns = originalAnnotationArray;
+
+ if (!Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS))) {
+ anns = addAnnotation(source, anns, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, new StringLiteral(ALL, 0, 0, 0));
+ }
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
MemberValuePair mvp = new MemberValuePair(JUSTIFICATION, 0, 0, new StringLiteral(GENERATED_CODE, 0, 0, 0));
diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java
index 30b4a699..9cd1e2a1 100644
--- a/src/core/lombok/eclipse/handlers/HandleGetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleGetter.java
@@ -285,10 +285,13 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> {
}
if (addSuppressWarningsUnchecked) {
+ List<Expression> suppressions = new ArrayList<Expression>(2);
+ if (!Boolean.FALSE.equals(fieldNode.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS))) {
+ suppressions.add(new StringLiteral(ALL, 0, 0, 0));
+ }
+ suppressions.add(new StringLiteral(UNCHECKED, 0, 0, 0));
ArrayInitializer arr = new ArrayInitializer();
- arr.expressions = new Expression[2];
- arr.expressions[0] = new StringLiteral(ALL, 0, 0, 0);
- arr.expressions[1] = new StringLiteral(UNCHECKED, 0, 0, 0);
+ arr.expressions = suppressions.toArray(new Expression[0]);
method.annotations = addAnnotation(source, method.annotations, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, arr);
}
diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index 985873c1..22f43d61 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -265,7 +265,12 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
decl.mods.annotations = decl.mods.annotations.appendList(delegates);
if (addSuppressWarningsUnchecked) {
- addAnnotation(decl.mods, field, source.pos, source, field.getContext(), "java.lang.SuppressWarnings", treeMaker.NewArray(null, List.<JCExpression>nil(), List.<JCExpression>of(treeMaker.Literal("all"), treeMaker.Literal("unchecked"))));
+ ListBuffer<JCExpression> suppressions = new ListBuffer<JCExpression>();
+ if (!Boolean.FALSE.equals(field.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS))) {
+ suppressions.add(treeMaker.Literal("all"));
+ }
+ suppressions.add(treeMaker.Literal("unchecked"));
+ addAnnotation(decl.mods, field, source.pos, source, field.getContext(), "java.lang.SuppressWarnings", treeMaker.NewArray(null, List.<JCExpression>nil(), suppressions.toList()));
}
copyJavadoc(field, decl, CopyJavadoc.GETTER);
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index e0f6276f..3655e680 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1277,15 +1277,17 @@ public class JavacHandlerUtil {
public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) {
if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return;
- boolean addJLSuppress = true;
-
- for (JCAnnotation ann : mods.annotations) {
- JCTree type = ann.getAnnotationType();
- Name n = null;
- if (type instanceof JCIdent) n = ((JCIdent) type).name;
- else if (type instanceof JCFieldAccess) n = ((JCFieldAccess) type).name;
- if (n != null && n.contentEquals("SuppressWarnings")) {
- addJLSuppress = false;
+ boolean addJLSuppress = !Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS));
+
+ if (addJLSuppress) {
+ for (JCAnnotation ann : mods.annotations) {
+ JCTree type = ann.getAnnotationType();
+ Name n = null;
+ if (type instanceof JCIdent) n = ((JCIdent) type).name;
+ else if (type instanceof JCFieldAccess) n = ((JCFieldAccess) type).name;
+ if (n != null && n.contentEquals("SuppressWarnings")) {
+ addJLSuppress = false;
+ }
}
}
if (addJLSuppress) addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));
diff --git a/test/transform/resource/after-delombok/SkipSuppressWarnings.java b/test/transform/resource/after-delombok/SkipSuppressWarnings.java
new file mode 100644
index 00000000..151d4e17
--- /dev/null
+++ b/test/transform/resource/after-delombok/SkipSuppressWarnings.java
@@ -0,0 +1,24 @@
+class SkipSuppressWarnings {
+ private String field = "";
+ private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field2 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
+
+ public String getField() {
+ return this.field;
+ }
+
+ @java.lang.SuppressWarnings({"unchecked"})
+ public String getField2() {
+ java.lang.Object value = this.field2.get();
+ if (value == null) {
+ synchronized (this.field2) {
+ value = this.field2.get();
+ if (value == null) {
+ final String actualValue = "";
+ value = actualValue == null ? this.field2 : actualValue;
+ this.field2.set(value);
+ }
+ }
+ }
+ return (String) (value == this.field2 ? null : value);
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/after-ecj/SkipSuppressWarnings.java b/test/transform/resource/after-ecj/SkipSuppressWarnings.java
new file mode 100644
index 00000000..53032519
--- /dev/null
+++ b/test/transform/resource/after-ecj/SkipSuppressWarnings.java
@@ -0,0 +1,27 @@
+class SkipSuppressWarnings {
+ private @lombok.Getter String field = "";
+ private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference<java.lang.Object> field2 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
+ SkipSuppressWarnings() {
+ super();
+ }
+ public String getField() {
+ return this.field;
+ }
+ public @java.lang.SuppressWarnings({"unchecked"}) String getField2() {
+ java.lang.Object value = this.field2.get();
+ if ((value == null))
+ {
+ synchronized (this.field2)
+ {
+ value = this.field2.get();
+ if ((value == null))
+ {
+ final String actualValue = "";
+ value = ((actualValue == null) ? this.field2 : actualValue);
+ this.field2.set(value);
+ }
+ }
+ }
+ return (String) ((value == this.field2) ? null : value);
+ }
+} \ No newline at end of file
diff --git a/test/transform/resource/before/SkipSuppressWarnings.java b/test/transform/resource/before/SkipSuppressWarnings.java
new file mode 100644
index 00000000..ed36eeb9
--- /dev/null
+++ b/test/transform/resource/before/SkipSuppressWarnings.java
@@ -0,0 +1,9 @@
+//CONF: lombok.addSuppressWarnings = false
+
+class SkipSuppressWarnings {
+ @lombok.Getter
+ private String field = "";
+
+ @lombok.Getter(lazy=true)
+ private final String field2 = "";
+} \ No newline at end of file
diff --git a/test/transform/resource/messages-ecj/SkipSuppressWarnings.java.messages b/test/transform/resource/messages-ecj/SkipSuppressWarnings.java.messages
new file mode 100644
index 00000000..5e7d759c
--- /dev/null
+++ b/test/transform/resource/messages-ecj/SkipSuppressWarnings.java.messages
@@ -0,0 +1 @@
+7 Unnecessary @SuppressWarnings("unchecked") \ No newline at end of file