mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 20:17:25 +00:00
Fixes #3120
This commit is contained in:
1 parent
560ded5012
commit
22444b78e2
5 files changed
+125
-9
No files matched your search
@@ -2608,6 +2608,13 @@ public class EclipseHandlerUtil {
|
||||
applyAnnotationToVarDecl(typeNode, arg, lib.getNullableAnnotation(), lib.isTypeUse());
|
||||
}
|
||||
|
||||
public static void createRelevantNullableAnnotation(EclipseNode typeNode, Argument arg, List<NullAnnotationLibrary> applied) {
|
||||
NullAnnotationLibrary lib = typeNode.getAst().readConfiguration(ConfigurationKeys.ADD_NULL_ANNOTATIONS);
|
||||
if (lib == null || applied.contains(lib)) return;
|
||||
|
||||
applyAnnotationToVarDecl(typeNode, arg, lib.getNullableAnnotation(), lib.isTypeUse());
|
||||
}
|
||||
|
||||
public static void createRelevantNonNullAnnotation(EclipseNode typeNode, Argument arg) {
|
||||
NullAnnotationLibrary lib = typeNode.getAst().readConfiguration(ConfigurationKeys.ADD_NULL_ANNOTATIONS);
|
||||
if (lib == null) return;
|
||||
|
||||
@@ -44,6 +44,7 @@ import lombok.core.handlers.InclusionExclusionUtils.Included;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.configuration.CallSuperType;
|
||||
import lombok.core.configuration.CheckerFrameworkVersion;
|
||||
import lombok.core.configuration.NullAnnotationLibrary;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseNode;
|
||||
@@ -606,19 +607,20 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
|
||||
int pS = source.sourceStart; int pE = source.sourceEnd;
|
||||
long p = (long) pS << 32 | pE;
|
||||
|
||||
Annotation[] onParamType = null;
|
||||
List<NullAnnotationLibrary> applied = new ArrayList<NullAnnotationLibrary>();
|
||||
|
||||
Annotation[] onParamNullable = 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);
|
||||
onParamNullable = new Annotation[] { new MarkerAnnotation(generateQualifiedTypeRef(source, JAVAX_ANNOTATION_NULLABLE), 0) };
|
||||
applied.add(NullAnnotationLibrary.JAVAX);
|
||||
}
|
||||
|
||||
Annotation[] onParamTypeNullable = null;
|
||||
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};
|
||||
onParamTypeNullable = new Annotation[] { new MarkerAnnotation(generateQualifiedTypeRef(source, ORG_ECLIPSE_JDT_ANNOTATION_NULLABLE), 0) };
|
||||
applied.add(NullAnnotationLibrary.ECLIPSE);
|
||||
}
|
||||
|
||||
MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
|
||||
@@ -640,12 +642,13 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
|
||||
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
|
||||
QualifiedTypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
|
||||
if (onParamType != null) objectRef.annotations = new Annotation[][] {null, null, onParamType};
|
||||
if (onParamTypeNullable != null) objectRef.annotations = new Annotation[][] {null, null, onParamTypeNullable};
|
||||
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;
|
||||
if (!onParam.isEmpty()) method.arguments[0].annotations = onParam.toArray(new Annotation[0]);
|
||||
EclipseHandlerUtil.createRelevantNullableAnnotation(type, method.arguments[0]);
|
||||
if (!onParam.isEmpty() || onParamNullable != null)
|
||||
method.arguments[0].annotations = copyAnnotations(source, onParam.toArray(new Annotation[0]), onParamNullable);
|
||||
EclipseHandlerUtil.createRelevantNullableAnnotation(type, method.arguments[0], applied);
|
||||
setGeneratedBy(method.arguments[0], source);
|
||||
|
||||
List<Statement> statements = new ArrayList<Statement>();
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@ParametersAreNonnullByDefault
|
||||
class EqualsAndHashCodeWithNonNullByDefault {
|
||||
int x;
|
||||
boolean[] y;
|
||||
Object[] z;
|
||||
String a;
|
||||
String b;
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public boolean equals(@javax.annotation.Nullable final java.lang.Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof EqualsAndHashCodeWithNonNullByDefault)) return false;
|
||||
final EqualsAndHashCodeWithNonNullByDefault other = (EqualsAndHashCodeWithNonNullByDefault) o;
|
||||
if (!other.canEqual((java.lang.Object) this)) return false;
|
||||
if (this.x != other.x) return false;
|
||||
if (!java.util.Arrays.equals(this.y, other.y)) return false;
|
||||
if (!java.util.Arrays.deepEquals(this.z, other.z)) return false;
|
||||
final java.lang.Object this$a = this.a;
|
||||
final java.lang.Object other$a = other.a;
|
||||
if (this$a == null ? other$a != null : !this$a.equals(other$a)) return false;
|
||||
final java.lang.Object this$b = this.b;
|
||||
final java.lang.Object other$b = other.b;
|
||||
if (this$b == null ? other$b != null : !this$b.equals(other$b)) return false;
|
||||
return true;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
protected boolean canEqual(@javax.annotation.Nullable final java.lang.Object other) {
|
||||
return other instanceof EqualsAndHashCodeWithNonNullByDefault;
|
||||
}
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public int hashCode() {
|
||||
final int PRIME = 59;
|
||||
int result = 1;
|
||||
result = result * PRIME + this.x;
|
||||
result = result * PRIME + java.util.Arrays.hashCode(this.y);
|
||||
result = result * PRIME + java.util.Arrays.deepHashCode(this.z);
|
||||
final java.lang.Object $a = this.a;
|
||||
result = result * PRIME + ($a == null ? 43 : $a.hashCode());
|
||||
final java.lang.Object $b = this.b;
|
||||
result = result * PRIME + ($b == null ? 43 : $b.hashCode());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@lombok.EqualsAndHashCode @ParametersAreNonnullByDefault class EqualsAndHashCodeWithNonNullByDefault {
|
||||
int x;
|
||||
boolean[] y;
|
||||
Object[] z;
|
||||
String a;
|
||||
String b;
|
||||
EqualsAndHashCodeWithNonNullByDefault() {
|
||||
super();
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") boolean equals(final @javax.annotation.Nullable java.lang.Object o) {
|
||||
if ((o == this))
|
||||
return true;
|
||||
if ((! (o instanceof EqualsAndHashCodeWithNonNullByDefault)))
|
||||
return false;
|
||||
final EqualsAndHashCodeWithNonNullByDefault other = (EqualsAndHashCodeWithNonNullByDefault) o;
|
||||
if ((! other.canEqual((java.lang.Object) this)))
|
||||
return false;
|
||||
if ((this.x != other.x))
|
||||
return false;
|
||||
if ((! java.util.Arrays.equals(this.y, other.y)))
|
||||
return false;
|
||||
if ((! java.util.Arrays.deepEquals(this.z, other.z)))
|
||||
return false;
|
||||
final java.lang.Object this$a = this.a;
|
||||
final java.lang.Object other$a = other.a;
|
||||
if (((this$a == null) ? (other$a != null) : (! this$a.equals(other$a))))
|
||||
return false;
|
||||
final java.lang.Object this$b = this.b;
|
||||
final java.lang.Object other$b = other.b;
|
||||
if (((this$b == null) ? (other$b != null) : (! this$b.equals(other$b))))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
protected @java.lang.SuppressWarnings("all") boolean canEqual(final @javax.annotation.Nullable java.lang.Object other) {
|
||||
return (other instanceof EqualsAndHashCodeWithNonNullByDefault);
|
||||
}
|
||||
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
|
||||
final int PRIME = 59;
|
||||
int result = 1;
|
||||
result = ((result * PRIME) + this.x);
|
||||
result = ((result * PRIME) + java.util.Arrays.hashCode(this.y));
|
||||
result = ((result * PRIME) + java.util.Arrays.deepHashCode(this.z));
|
||||
final java.lang.Object $a = this.a;
|
||||
result = ((result * PRIME) + (($a == null) ? 43 : $a.hashCode()));
|
||||
final java.lang.Object $b = this.b;
|
||||
result = ((result * PRIME) + (($b == null) ? 43 : $b.hashCode()));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//CONF: lombok.addNullAnnotations = javax
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@lombok.EqualsAndHashCode
|
||||
@ParametersAreNonnullByDefault
|
||||
class EqualsAndHashCodeWithNonNullByDefault {
|
||||
int x;
|
||||
boolean[] y;
|
||||
Object[] z;
|
||||
String a;
|
||||
String b;
|
||||
}
|
||||
Reference in new issue
Block a user