Skip to content

Commit

Permalink
[fixes #880] get rid of an unchecked cast warning for `@Getter(lazy=t…
Browse files Browse the repository at this point in the history
…rue)`
  • Loading branch information
rzwitserloot committed Feb 6, 2020
1 parent 2e06cb3 commit 8943b49
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 39 deletions.
5 changes: 3 additions & 2 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ public static EclipseNode injectType(final EclipseNode typeNode, final TypeDecla
return typeNode.add(type, Kind.TYPE);
}

private static final char[] ALL = "all".toCharArray();
static final char[] ALL = "all".toCharArray();
static final char[] UNCHECKED = "unchecked".toCharArray();
private static final char[] JUSTIFICATION = "justification".toCharArray();
private static final char[] GENERATED_CODE = "generated code".toCharArray();
private static final char[] LOMBOK = "lombok".toCharArray();
Expand Down Expand Up @@ -1934,7 +1935,7 @@ public static Annotation[] addGenerated(EclipseNode node, ASTNode source, Annota
return result;
}

private static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) {
static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) {
char[] simpleName = annotationTypeFqn[annotationTypeFqn.length - 1];

if (originalAnnotationArray != null) for (Annotation ann : originalAnnotationArray) {
Expand Down
14 changes: 13 additions & 1 deletion src/core/lombok/eclipse/handlers/HandleGetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2019 The Project Lombok Authors.
* Copyright (C) 2009-2020 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
Expand Down Expand Up @@ -46,6 +46,7 @@
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Assignment;
import org.eclipse.jdt.internal.compiler.ast.BinaryExpression;
Expand All @@ -66,6 +67,7 @@
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
import org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
Expand Down Expand Up @@ -241,8 +243,10 @@ public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldN
TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source);

Statement[] statements;
boolean addSuppressWarningsUnchecked = false;
if (lazy) {
statements = createLazyGetterBody(source, fieldNode);
addSuppressWarningsUnchecked = true;
} else {
statements = createSimpleGetterBody(source, fieldNode);
}
Expand Down Expand Up @@ -276,6 +280,14 @@ public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldN
deprecated);
}

if (addSuppressWarningsUnchecked) {
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);
method.annotations = addAnnotation(source, method.annotations, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS, arr);
}

method.traverse(new SetGeneratedByVisitor(source), parent.scope);
return method;
}
Expand Down
13 changes: 10 additions & 3 deletions src/core/lombok/javac/handlers/HandleGetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2019 The Project Lombok Authors.
* Copyright (C) 2009-2020 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
Expand Down Expand Up @@ -38,6 +38,7 @@
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
import lombok.core.configuration.CheckerFrameworkVersion;
import lombok.delombok.LombokOptionsFactory;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.JavacTreeMaker;
Expand Down Expand Up @@ -231,9 +232,11 @@ public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker tr

List<JCStatement> statements;
JCTree toClearOfMarkers = null;
boolean addSuppressWarningsUnchecked = false;
if (lazy && !inNetbeansEditor(field)) {
toClearOfMarkers = fieldNode.init;
statements = createLazyGetterBody(treeMaker, field, source);
addSuppressWarningsUnchecked = LombokOptionsFactory.getDelombokOptions(field.getContext()).getFormatPreferences().generateSuppressWarnings();
} else {
statements = createSimpleGetterBody(treeMaker, field);
}
Expand All @@ -252,11 +255,15 @@ public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker tr
if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));

JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext());
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext());

if (toClearOfMarkers != null) recursiveSetGeneratedBy(toClearOfMarkers, null, null);
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"))));
}

copyJavadoc(field, decl, CopyJavadoc.GETTER);
return decl;
}
Expand Down Expand Up @@ -418,7 +425,7 @@ public List<JCStatement> createLazyGetterBody(JavacTreeMaker maker, JavacNode fi

/* private final java.util.concurrent.atomic.AtomicReference<Object> fieldName = new java.util.concurrent.atomic.AtomicReference<Object>(); */ {
field.vartype = recursiveSetGeneratedBy(
maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(genJavaLangTypeRef(fieldNode, "Object"))), source, fieldNode.getContext());
maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>of(genJavaLangTypeRef(fieldNode, "Object"))), source, fieldNode.getContext());
field.init = recursiveSetGeneratedBy(maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field), NIL_EXPRESSION, null), source, fieldNode.getContext());
}

Expand Down
14 changes: 13 additions & 1 deletion src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,19 @@ public static JCExpression genJavaLangTypeRef(JavacNode node, int pos, String...

public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) {
if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return;
addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));

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;
}
}
if (addJLSuppress) addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));

if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
JavacTreeMaker maker = node.getTreeMaker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ private interface Bar {
void setList(java.util.ArrayList<java.lang.String> list);
int getInt();
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public Bar getBar() {
java.lang.Object value = this.bar.get();
if (value == null) {
Expand Down
2 changes: 1 addition & 1 deletion test/transform/resource/after-delombok/GetterLazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class GetterLazy {
static class ValueType {
}
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> fieldName = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public ValueType getFieldName() {
java.lang.Object value = this.fieldName.get();
if (value == null) {
Expand Down
4 changes: 2 additions & 2 deletions test/transform/resource/after-delombok/GetterLazyBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int hashCode() {
public java.lang.String toString() {
return "GetterLazyBoolean(booleanValue=" + this.isBooleanValue() + ")";
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public boolean isBooleanValue() {
java.lang.Object value = this.booleanValue.get();
if (value == null) {
Expand All @@ -46,7 +46,7 @@ public boolean isBooleanValue() {
}
return (java.lang.Boolean) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public boolean isOtherBooleanValue() {
java.lang.Object value = this.otherBooleanValue.get();
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public java.lang.String toString() {
return "GetterLazyEahcToString(value=" + this.getValue() + ", value2=" + this.value2 + ")";
}

@java.lang.SuppressWarnings("all")


@java.lang.SuppressWarnings({"all", "unchecked"})
public String getValue() {
java.lang.Object value = this.value.get();
if (value == null) {
Expand Down
37 changes: 37 additions & 0 deletions test/transform/resource/after-delombok/GetterLazyGenerics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class GetterLazyGenerics<E> {
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field2 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
public static <E> E getAny() {
return null;
}
@java.lang.SuppressWarnings({"all", "unchecked"})
public E getField() {
java.lang.Object value = this.field.get();
if (value == null) {
synchronized (this.field) {
value = this.field.get();
if (value == null) {
final E actualValue = getAny();
value = actualValue == null ? this.field : actualValue;
this.field.set(value);
}
}
}
return (E) (value == this.field ? null : value);
}
@java.lang.SuppressWarnings({"all", "unchecked"})
public long getField2() {
java.lang.Object value = this.field2.get();
if (value == null) {
synchronized (this.field2) {
value = this.field2.get();
if (value == null) {
final long actualValue = System.currentTimeMillis();
value = actualValue;
this.field2.set(value);
}
}
}
return (java.lang.Long) value;
}
}
18 changes: 9 additions & 9 deletions test/transform/resource/after-delombok/GetterLazyNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GetterLazyNative {
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> doubleField = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> charField = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> intArrayField = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public boolean isBooleanField() {
java.lang.Object value = this.booleanField.get();
if (value == null) {
Expand All @@ -23,7 +23,7 @@ public boolean isBooleanField() {
}
return (java.lang.Boolean) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public byte getByteField() {
java.lang.Object value = this.byteField.get();
if (value == null) {
Expand All @@ -38,7 +38,7 @@ public byte getByteField() {
}
return (java.lang.Byte) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public short getShortField() {
java.lang.Object value = this.shortField.get();
if (value == null) {
Expand All @@ -53,7 +53,7 @@ public short getShortField() {
}
return (java.lang.Short) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public int getIntField() {
java.lang.Object value = this.intField.get();
if (value == null) {
Expand All @@ -68,7 +68,7 @@ public int getIntField() {
}
return (java.lang.Integer) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public long getLongField() {
java.lang.Object value = this.longField.get();
if (value == null) {
Expand All @@ -83,7 +83,7 @@ public long getLongField() {
}
return (java.lang.Long) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public float getFloatField() {
java.lang.Object value = this.floatField.get();
if (value == null) {
Expand All @@ -98,7 +98,7 @@ public float getFloatField() {
}
return (java.lang.Float) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public double getDoubleField() {
java.lang.Object value = this.doubleField.get();
if (value == null) {
Expand All @@ -113,7 +113,7 @@ public double getDoubleField() {
}
return (java.lang.Double) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public char getCharField() {
java.lang.Object value = this.charField.get();
if (value == null) {
Expand All @@ -128,7 +128,7 @@ public char getCharField() {
}
return (java.lang.Character) value;
}
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public int[] getIntArrayField() {
java.lang.Object value = this.intArrayField.get();
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class GetterLazyTransient {
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> nonTransientField = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final transient int transientField = 2;
private final transient int nonLazyTransientField = 3;
@java.lang.SuppressWarnings("all")
@java.lang.SuppressWarnings({"all", "unchecked"})
public int getNonTransientField() {
java.lang.Object value = this.nonTransientField.get();
if (value == null) {
Expand Down
2 changes: 1 addition & 1 deletion test/transform/resource/after-ecj/DelegateOnGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private interface Bar {
DelegateOnGetter() {
super();
}
public @Delegate @java.lang.SuppressWarnings("all") Bar getBar() {
public @Delegate @java.lang.SuppressWarnings({"all", "unchecked"}) Bar getBar() {
java.lang.Object value = this.bar.get();
if ((value == null))
{
Expand Down
2 changes: 1 addition & 1 deletion test/transform/resource/after-ecj/GetterLazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static class ValueType {
GetterLazy() {
super();
}
public @java.lang.SuppressWarnings("all") ValueType getFieldName() {
public @java.lang.SuppressWarnings({"all", "unchecked"}) ValueType getFieldName() {
java.lang.Object value = this.fieldName.get();
if ((value == null))
{
Expand Down
4 changes: 2 additions & 2 deletions test/transform/resource/after-ecj/GetterLazyBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
private static boolean calculateBoolean() {
return true;
}
public @java.lang.SuppressWarnings("all") boolean isBooleanValue() {
public @java.lang.SuppressWarnings({"all", "unchecked"}) boolean isBooleanValue() {
java.lang.Object value = this.booleanValue.get();
if ((value == null))
{
Expand All @@ -24,7 +24,7 @@ private static boolean calculateBoolean() {
}
return (java.lang.Boolean) value;
}
public @java.lang.SuppressWarnings("all") boolean isOtherBooleanValue() {
public @java.lang.SuppressWarnings({"all", "unchecked"}) boolean isOtherBooleanValue() {
java.lang.Object value = this.otherBooleanValue.get();
if ((value == null))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
GetterLazyEahcToString() {
super();
}
public @java.lang.SuppressWarnings("all") String getValue() {
public @java.lang.SuppressWarnings({"all", "unchecked"}) String getValue() {
java.lang.Object value = this.value.get();
if ((value == null))
{
Expand Down
Loading

0 comments on commit 8943b49

Please sign in to comment.