aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/eclipse/handlers
diff options
context:
space:
mode:
authorReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-01-08 01:06:35 +0100
committerReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-01-08 01:06:35 +0100
commit9ad2bd563b001c0742d767fea9ddaaeb60400ec7 (patch)
tree82d9653d555b46b1a42f7c8086e87bd6aefd16a8 /src/core/lombok/eclipse/handlers
parent0b0656ae3c38ae915c86e17c2744d5e3d8fc805c (diff)
downloadlombok-9ad2bd563b001c0742d767fea9ddaaeb60400ec7.tar.gz
lombok-9ad2bd563b001c0742d767fea9ddaaeb60400ec7.tar.bz2
lombok-9ad2bd563b001c0742d767fea9ddaaeb60400ec7.zip
[fixes #788] lombok generated equals method plus a non-null-by-default annotation no longer clash.
Diffstat (limited to 'src/core/lombok/eclipse/handlers')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java14
-rwxr-xr-xsrc/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java32
2 files changed, 43 insertions, 3 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 948902d5..1099afd5 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -745,6 +745,20 @@ public class EclipseHandlerUtil {
}
}
+ public static String scanForNearestAnnotation(EclipseNode node, String... anns) {
+ while (node != null) {
+ for (EclipseNode ann : node.down()) {
+ if (ann.getKind() != Kind.ANNOTATION) continue;
+ Annotation a = (Annotation) ann.get();
+ TypeReference aType = a.type;
+ for (String annToFind : anns) if (typeMatches(annToFind, node, aType)) return annToFind;
+ }
+ node = node.up();
+ }
+
+ return null;
+ }
+
public static boolean hasNonNullAnnotations(EclipseNode node) {
AbstractVariableDeclaration avd = (AbstractVariableDeclaration) node.get();
if (avd.annotations == null) return false;
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index 1bca4767..46474b07 100755
--- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -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
@@ -63,6 +63,7 @@ import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression;
import org.eclipse.jdt.internal.compiler.ast.IntLiteral;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.NameReference;
@@ -504,9 +505,33 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
return arr == null ? 0 : arr.length;
}
+ /*
+ * scan method, then class, then enclosing classes, then package for the first of:
+ * javax.annotation.ParametersAreNonnullByDefault or javax.annotation.ParametersAreNullableByDefault. If it's the NN variant, generate javax.annotation.Nullable _on the type_.
+ * org.eclipse.jdt.annotation.NonNullByDefault -> org.eclipse.jdt.annotation.Nullable
+ */
+
+ private static final char[][] JAVAX_ANNOTATION_NULLABLE = Eclipse.fromQualifiedName("javax.annotation.Nullable");
+ private static final char[][] ORG_ECLIPSE_JDT_ANNOTATION_NULLABLE = Eclipse.fromQualifiedName("org.eclipse.jdt.annotation.Nullable");
+
public MethodDeclaration createEquals(EclipseNode type, Collection<Included<EclipseNode, EqualsAndHashCode.Include>> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List<Annotation> onParam) {
int pS = source.sourceStart; int pE = source.sourceEnd;
- long p = (long)pS << 32 | pE;
+ long p = (long) pS << 32 | pE;
+
+ Annotation[] onParamType = null;
+
+ String nearest = scanForNearestAnnotation(type, "javax.annotation.ParametersAreNullableByDefault", "javax.annotation.ParametersAreNonnullByDefault");
+ if ("javax.annotation.ParametersAreNonnullByDefault".equals(nearest)) {
+ onParamType = new Annotation[1];
+ onParamType[0] = new MarkerAnnotation(generateQualifiedTypeRef(source, JAVAX_ANNOTATION_NULLABLE), 0);
+ }
+
+ nearest = scanForNearestAnnotation(type, "org.eclipse.jdt.annotation.NonNullByDefault");
+ if (nearest != null) {
+ Annotation a = new MarkerAnnotation(generateQualifiedTypeRef(source, ORG_ECLIPSE_JDT_ANNOTATION_NULLABLE), 0);
+ if (onParamType != null) onParamType = new Annotation[] {onParamType[0], a};
+ else onParamType = new Annotation[] {a};
+ }
MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
setGeneratedBy(method, source);
@@ -526,7 +551,8 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
- TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
+ QualifiedTypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
+ if (onParamType != null) objectRef.annotations = new Annotation[][] {null, null, onParamType};
setGeneratedBy(objectRef, source);
method.arguments = new Argument[] {new Argument(new char[] { 'o' }, 0, objectRef, Modifier.FINAL)};
method.arguments[0].sourceStart = pS; method.arguments[0].sourceEnd = pE;