From ed8ea0d8043cb8df6ae3fb962ab3a2087f4adeb6 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Fri, 19 Jun 2020 00:47:00 +0200 Subject: #1543: First primitives, then primitive wrappers, then other references --- src/core/lombok/EqualsAndHashCode.java | 2 +- src/core/lombok/core/LombokNode.java | 13 ++++++++++++ src/core/lombok/core/handlers/HandlerUtil.java | 21 ++++++++++++++++++++ .../core/handlers/InclusionExclusionUtils.java | 4 ++-- src/core/lombok/eclipse/EclipseNode.java | 23 ++++++++++++++++++++++ src/core/lombok/javac/JavacNode.java | 13 ++++++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index 2f53bdec..6805d214 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -126,7 +126,7 @@ public @interface EqualsAndHashCode { /** * Higher ranks are considered first. Members of the same rank are considered in the order they appear in the source file. * - * If not explicitly set, the {@code default} rank for primitives is 1000. + * If not explicitly set, the {@code default} rank for primitives is 1000, and for primitive wrappers 800. * * @return ordering within the generating {@code equals} and {@code hashCode} methods; higher numbers are considered first. */ diff --git a/src/core/lombok/core/LombokNode.java b/src/core/lombok/core/LombokNode.java index 46054077..abfc66a6 100644 --- a/src/core/lombok/core/LombokNode.java +++ b/src/core/lombok/core/LombokNode.java @@ -292,6 +292,19 @@ public abstract class LombokNode, L extends LombokNode node, ConfigurationKey key, String featureName) { FlagUsageType fut = node.getAst().readConfiguration(key); @@ -742,4 +744,23 @@ public class HandlerUtil { } return b.toString(); } + + /** Matches any of the 8 primitive names, such as {@code boolean}. */ + private static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile("^(?:boolean|byte|short|int|long|float|double|char)$"); + + public static boolean isPrimitive(String typeName) { + return PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches(); + } + + /** Matches any of the 8 primitive wrapper names, such as {@code Boolean}. */ + private static final Pattern PRIMITIVE_WRAPPER_TYPE_NAME_PATTERN = Pattern.compile("^(?:java\\.lang\\.)?(?:Boolean|Byte|Short|Integer|Long|Float|Double|Character)$"); + + public static int defaultEqualsAndHashcodeIncludeRank(String typeName) { + // Modification in this code should be documented + // 1. In the changelog this should be marked as an INPROBABLE BREAKING CHANGE, since the hashcode will change + // 2. In the javadoc of EqualsAndHashcode.Include#rank + if (isPrimitive(typeName)) return 1000; + if (PRIMITIVE_WRAPPER_TYPE_NAME_PATTERN.matcher(typeName).matches()) return 800; + return 0; + } } diff --git a/src/core/lombok/core/handlers/InclusionExclusionUtils.java b/src/core/lombok/core/handlers/InclusionExclusionUtils.java index 609a5e36..e785db3f 100644 --- a/src/core/lombok/core/handlers/InclusionExclusionUtils.java +++ b/src/core/lombok/core/handlers/InclusionExclusionUtils.java @@ -225,8 +225,8 @@ public class InclusionExclusionUtils { Collections.sort(members, new Comparator>() { @Override public int compare(Included a, Included b) { - int ra = a.hasExplicitRank() ? a.getInc().rank() : a.node.isPrimitive() ? 1000 : 0; - int rb = b.hasExplicitRank() ? b.getInc().rank() : b.node.isPrimitive() ? 1000 : 0; + int ra = a.hasExplicitRank() ? a.getInc().rank() : HandlerUtil.defaultEqualsAndHashcodeIncludeRank(a.node.fieldOrMethodBaseType()); + int rb = b.hasExplicitRank() ? b.getInc().rank() : HandlerUtil.defaultEqualsAndHashcodeIncludeRank(b.node.fieldOrMethodBaseType()); return compareRankOrPosition(ra, rb, a.getNode(), b.getNode()); } diff --git a/src/core/lombok/eclipse/EclipseNode.java b/src/core/lombok/eclipse/EclipseNode.java index 5aa29466..12e9ccdb 100644 --- a/src/core/lombok/eclipse/EclipseNode.java +++ b/src/core/lombok/eclipse/EclipseNode.java @@ -275,6 +275,29 @@ public class EclipseNode extends lombok.core.LombokNode