From 9ad2bd563b001c0742d767fea9ddaaeb60400ec7 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 8 Jan 2020 01:06:35 +0100 Subject: [fixes #788] lombok generated equals method plus a non-null-by-default annotation no longer clash. --- .../eclipse/handlers/EclipseHandlerUtil.java | 14 ++++++++++ .../eclipse/handlers/HandleEqualsAndHashCode.java | 32 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) (limited to 'src/core/lombok/eclipse/handlers') 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 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> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List 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