[fixes #2433] Add config key to turn off @SuppressWarnings("all")

This commit is contained in:
Rawi01 authored and Roel Spilker committed 2020-07-02 23:15:14 +02:00
1 parent 9425e99b49
commit 84556ebb00
9 files changed
+95 -13

No files matched your search

+7
View File
@@ -89,6 +89,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].
*
@@ -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));
@@ -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);
}
@@ -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);
@@ -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;
boolean addJLSuppress = !Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS));
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) {
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"));
@@ -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);
}
}
@@ -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);
}
}
@@ -0,0 +1,9 @@
//CONF: lombok.addSuppressWarnings = false
class SkipSuppressWarnings {
@lombok.Getter
private String field = "";
@lombok.Getter(lazy=true)
private final String field2 = "";
}
@@ -0,0 +1 @@
7 Unnecessary @SuppressWarnings("unchecked")