aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/eclipse/handlers
diff options
context:
space:
mode:
authorReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-01-31 06:33:33 +0100
committerReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-01-31 06:33:33 +0100
commitc42bfbae39990b365a5f05eb23895da6203023bc (patch)
tree6bbebbff773571debc5709072e735f2f42469d46 /src/core/lombok/eclipse/handlers
parentd31182c4383a5ccc499862d2a8035d49e5202319 (diff)
downloadlombok-c42bfbae39990b365a5f05eb23895da6203023bc.tar.gz
lombok-c42bfbae39990b365a5f05eb23895da6203023bc.tar.bz2
lombok-c42bfbae39990b365a5f05eb23895da6203023bc.zip
[issue #2221] simplified configuration for `@Singular`-generated plural form nullchecks.
Diffstat (limited to 'src/core/lombok/eclipse/handlers')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java10
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java52
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/HandleBuilder.java15
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/HandleConstructor.java2
-rw-r--r--src/core/lombok/eclipse/handlers/HandleNonNull.java2
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSetter.java2
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/HandleSuperBuilder.java6
-rw-r--r--src/core/lombok/eclipse/handlers/HandleWith.java2
8 files changed, 23 insertions, 68 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index bd0ad23b..bdb88299 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -1992,7 +1992,7 @@ public class EclipseHandlerUtil {
* Generates a new statement that checks if the given local variable is null, and if so, throws a specified exception with the
* variable name as message.
*/
- public static Statement generateNullCheck(TypeReference type, char[] variable, EclipseNode sourceNode) {
+ public static Statement generateNullCheck(TypeReference type, char[] variable, EclipseNode sourceNode, String customMessage) {
NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
@@ -2001,11 +2001,11 @@ public class EclipseHandlerUtil {
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
- if (isPrimitive(type)) return null;
+ if (type != null && isPrimitive(type)) return null;
SingleNameReference varName = new SingleNameReference(variable, p);
setGeneratedBy(varName, source);
- StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable)).toCharArray(), pS, pE, 0);
+ StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable), customMessage).toCharArray(), pS, pE, 0);
setGeneratedBy(message, source);
LombokImmutableList<String> method = exceptionType.getMethod();
@@ -2071,8 +2071,8 @@ public class EclipseHandlerUtil {
*
* @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
- public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode) {
- return generateNullCheck(variable.type, variable.name, sourceNode);
+ public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode, String customMessage) {
+ return generateNullCheck(variable.type, variable.name, sourceNode, customMessage);
}
/**
diff --git a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java
index cbbd4cc8..85243ec1 100755
--- a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java
+++ b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java
@@ -33,7 +33,6 @@ import java.util.Map;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
-import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.Block;
@@ -49,16 +48,13 @@ import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
-import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Reference;
import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
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.ThisReference;
-import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
@@ -67,12 +63,10 @@ import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import lombok.AccessLevel;
-import lombok.Singular.NullCollectionBehavior;
import lombok.core.LombokImmutableList;
import lombok.core.SpiLoadUtil;
import lombok.core.TypeLibrary;
import lombok.core.configuration.CheckerFrameworkVersion;
-import lombok.eclipse.Eclipse;
import lombok.eclipse.EclipseNode;
public class EclipseSingularsRecipes {
@@ -135,14 +129,14 @@ public class EclipseSingularsRecipes {
private final List<TypeReference> typeArgs;
private final String targetFqn;
private final EclipseSingularizer singularizer;
- private final NullCollectionBehavior nullCollectionBehavior;
+ private final boolean ignoreNullCollections;
private final ASTNode source;
- public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List<TypeReference> typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, NullCollectionBehavior nullCollectionBehavior) {
- this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, source, nullCollectionBehavior, new char[0]);
+ public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List<TypeReference> typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, boolean ignoreNullCollections) {
+ this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, source, ignoreNullCollections, new char[0]);
}
- public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List<TypeReference> typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, NullCollectionBehavior nullCollectionBehavior, char[] setterPrefix) {
+ public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List<TypeReference> typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, boolean ignoreNullCollections, char[] setterPrefix) {
this.annotation = annotation;
this.singularName = singularName;
this.pluralName = pluralName;
@@ -150,7 +144,7 @@ public class EclipseSingularsRecipes {
this.targetFqn = targetFqn;
this.singularizer = singularizer;
this.source = source;
- this.nullCollectionBehavior = nullCollectionBehavior;
+ this.ignoreNullCollections = ignoreNullCollections;
this.setterPrefix = setterPrefix;
}
@@ -198,8 +192,8 @@ public class EclipseSingularsRecipes {
return singularizer;
}
- public NullCollectionBehavior getNullCollectionBehavior() {
- return nullCollectionBehavior;
+ public boolean isIgnoreNullCollections() {
+ return ignoreNullCollections;
}
public String getTargetSimpleType() {
@@ -439,9 +433,9 @@ public class EclipseSingularsRecipes {
}
protected void nullBehaviorize(EclipseNode typeNode, SingularData data, List<Statement> statements, Argument arg) {
- NullCollectionBehavior behavior = data.getNullCollectionBehavior();
+ boolean ignoreNullCollections = data.isIgnoreNullCollections();
- if (behavior == NullCollectionBehavior.IGNORE) {
+ if (ignoreNullCollections) {
Expression isNotNull = new EqualExpression(new SingleNameReference(data.getPluralName(), 0L), new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
Block b = new Block(0);
b.statements = statements.toArray(new Statement[statements.size()]);
@@ -452,32 +446,8 @@ public class EclipseSingularsRecipes {
}
EclipseHandlerUtil.createRelevantNonNullAnnotation(typeNode, arg);
-
- String exceptionTypeStr = behavior.getExceptionType();
- StringLiteral message = new StringLiteral(behavior.toExceptionMessage(new String(data.getPluralName())).toCharArray(), 0, 0, 0);
- if (exceptionTypeStr != null) {
- Expression isNull = new EqualExpression(new SingleNameReference(data.getPluralName(), 0L), new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
- int partCount = 1;
- for (int i = 0; i < exceptionTypeStr.length(); i++) if (exceptionTypeStr.charAt(i) == '.') partCount++;
- long[] ps = new long[partCount];
- Arrays.fill(ps, 0L);
- AllocationExpression alloc = new AllocationExpression();
- alloc.type = new QualifiedTypeReference(Eclipse.fromQualifiedName(exceptionTypeStr), ps);
- alloc.arguments = new Expression[] {message};
- Statement t = new ThrowStatement(alloc, 0, 0);
- statements.add(0, new IfStatement(isNull, t, 0, 0));
- return;
- }
-
- MessageSend invoke = new MessageSend();
- LombokImmutableList<String> method = behavior.getMethod();
- char[][] utilityTypeName = new char[method.size() - 1][];
- for (int i = 0; i < method.size() - 1; i++) utilityTypeName[i] = method.get(i).toCharArray();
-
- invoke.receiver = new QualifiedNameReference(utilityTypeName, new long[method.size() - 1], 0, 0);
- invoke.selector = method.get(method.size() - 1).toCharArray();
- invoke.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L), message};
- statements.add(0, invoke);
+ Statement nullCheck = EclipseHandlerUtil.generateNullCheck(null, data.getPluralName(), typeNode, "%s cannot be null");
+ statements.add(0, nullCheck);
}
protected abstract char[][] getEmptyMakerReceiver(String targetFqn);
diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java
index 578fa2a3..19388eda 100755
--- a/src/core/lombok/eclipse/handlers/HandleBuilder.java
+++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java
@@ -79,7 +79,6 @@ import org.mangosdk.spi.ProviderFor;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Builder.ObtainVia;
-import lombok.Singular.NullCollectionBehavior;
import lombok.ConfigurationKeys;
import lombok.Singular;
import lombok.ToString;
@@ -1036,21 +1035,9 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
return null;
}
- NullCollectionBehavior behavior = getNullBehaviorFor(ann, singularInstance, node);
- return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, behavior, setterPrefix.toCharArray());
+ return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, singularInstance.ignoreNullCollections(), setterPrefix.toCharArray());
}
return null;
}
-
- static NullCollectionBehavior getNullBehaviorFor(AnnotationValues<Singular> ann, Singular singularInstance, EclipseNode node) {
- NullCollectionBehavior behavior;
- if (ann.isExplicit("nullBehavior")) {
- behavior = singularInstance.nullBehavior();
- } else {
- behavior = node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_NULL_COLLECTIONS);
- }
- if (behavior == null) return NullCollectionBehavior.NULL_POINTER_EXCEPTION;
- return behavior;
- }
}
diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java
index a5716d9c..f69a2d1c 100755
--- a/src/core/lombok/eclipse/handlers/HandleConstructor.java
+++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java
@@ -411,7 +411,7 @@ public class HandleConstructor {
Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL);
Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode);
if (hasNonNullAnnotations(fieldNode)) {
- Statement nullCheck = generateNullCheck(parameter, sourceNode);
+ Statement nullCheck = generateNullCheck(parameter, sourceNode, null);
if (nullCheck != null) nullChecks.add(nullCheck);
}
parameter.annotations = copyAnnotations(source, copyableAnnotations);
diff --git a/src/core/lombok/eclipse/handlers/HandleNonNull.java b/src/core/lombok/eclipse/handlers/HandleNonNull.java
index c61ce02d..c4d42c2e 100644
--- a/src/core/lombok/eclipse/handlers/HandleNonNull.java
+++ b/src/core/lombok/eclipse/handlers/HandleNonNull.java
@@ -145,7 +145,7 @@ public class HandleNonNull extends EclipseAnnotationHandler<NonNull> {
// and if they exist, create a new method in the class: 'private static <T> 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(param, annotationNode);
+ Statement nullCheck = generateNullCheck(param, annotationNode, null);
if (nullCheck == null) {
// @NonNull applied to a primitive. Kinda pointless. Let's generate a warning.
diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java
index eda2c271..8ee54d0f 100644
--- a/src/core/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/core/lombok/eclipse/handlers/HandleSetter.java
@@ -245,7 +245,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> {
if (!hasNonNullAnnotations(fieldNode) && !hasNonNullAnnotations(fieldNode, onParam)) {
statements.add(assignment);
} else {
- Statement nullCheck = generateNullCheck(field.type, paramName, sourceNode);
+ Statement nullCheck = generateNullCheck(field.type, paramName, sourceNode, null);
if (nullCheck != null) statements.add(nullCheck);
statements.add(assignment);
}
diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
index 4b450a07..0a5acd56 100755
--- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
+++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
@@ -79,7 +79,6 @@ import org.mangosdk.spi.ProviderFor;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Builder.ObtainVia;
-import lombok.Singular.NullCollectionBehavior;
import lombok.ConfigurationKeys;
import lombok.Singular;
import lombok.ToString;
@@ -567,7 +566,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
}
if (hasNonNullAnnotations(fieldNode.originalFieldNode)) {
- Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode);
+ Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode, null);
if (nullCheck != null) statements.add(nullCheck);
}
}
@@ -1044,8 +1043,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
return null;
}
- NullCollectionBehavior behavior = HandleBuilder.getNullBehaviorFor(ann, singularInstance, node);
- return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, behavior);
+ return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, singularInstance.ignoreNullCollections());
}
return null;
diff --git a/src/core/lombok/eclipse/handlers/HandleWith.java b/src/core/lombok/eclipse/handlers/HandleWith.java
index 4771818d..5cdc02df 100644
--- a/src/core/lombok/eclipse/handlers/HandleWith.java
+++ b/src/core/lombok/eclipse/handlers/HandleWith.java
@@ -278,7 +278,7 @@ public class HandleWith extends EclipseAnnotationHandler<With> {
List<Statement> statements = new ArrayList<Statement>(5);
if (hasNonNullAnnotations(fieldNode)) {
- Statement nullCheck = generateNullCheck(field, sourceNode);
+ Statement nullCheck = generateNullCheck(field, sourceNode, null);
if (nullCheck != null) statements.add(nullCheck);
}
statements.add(returnStatement);