diff options
author | samukce <samuel.p.araujo@gmail.com> | 2020-06-04 22:59:13 -0700 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2020-06-11 21:47:27 +0200 |
commit | 0fd663bb9974cb1994ea5b0339ad0343b665864d (patch) | |
tree | 9d0d226a06c5b4715b5e86273ef426eff1138740 /src/core | |
parent | a2941041d4d4b3db2a12a38212226f36c11219ab (diff) | |
download | lombok-0fd663bb9974cb1994ea5b0339ad0343b665864d.tar.gz lombok-0fd663bb9974cb1994ea5b0339ad0343b665864d.tar.bz2 lombok-0fd663bb9974cb1994ea5b0339ad0343b665864d.zip |
Oder equals/hashcode fields by rank or position
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lombok/EqualsAndHashCode.java | 9 | ||||
-rw-r--r-- | src/core/lombok/core/handlers/InclusionExclusionUtils.java | 39 |
2 files changed, 35 insertions, 13 deletions
diff --git a/src/core/lombok/EqualsAndHashCode.java b/src/core/lombok/EqualsAndHashCode.java index e752165c..02596f24 100644 --- a/src/core/lombok/EqualsAndHashCode.java +++ b/src/core/lombok/EqualsAndHashCode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 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 @@ -122,5 +122,12 @@ public @interface EqualsAndHashCode { * @return If present, this method serves as replacement for the named field. */ String replaces() default ""; + + /** + * Higher ranks are considered first. Members of the same rank are considered in the order they appear in the source file. + * + * @return ordering within the generating {@code equals} and {@code hashCode} methods; higher numbers are considered first. + */ + int rank() default 0; } } diff --git a/src/core/lombok/core/handlers/InclusionExclusionUtils.java b/src/core/lombok/core/handlers/InclusionExclusionUtils.java index 368b51fc..c50da1cc 100644 --- a/src/core/lombok/core/handlers/InclusionExclusionUtils.java +++ b/src/core/lombok/core/handlers/InclusionExclusionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 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 @@ -207,22 +207,37 @@ public class InclusionExclusionUtils { @Override public int compare(Included<L, ToString.Include> a, Included<L, ToString.Include> b) { int ra = a.getInc() == null ? 0 : a.getInc().rank(); int rb = b.getInc() == null ? 0 : b.getInc().rank(); - if (ra < rb) return +1; - if (ra > rb) return -1; - - int pa = a.getNode().getStartPos(); - int pb = b.getNode().getStartPos(); - - if (pa < pb) return -1; - if (pa > pb) return +1; - - return 0; + + return compareRankOrPosition(ra, rb, a.getNode(), b.getNode()); } }); return members; } public static <A extends AST<A, L, N>, L extends LombokNode<A, L, N>, N> List<Included<L, EqualsAndHashCode.Include>> handleEqualsAndHashCodeMarking(LombokNode<A, L, N> typeNode, AnnotationValues<EqualsAndHashCode> annotation, LombokNode<A, L, N> annotationNode) { - return handleIncludeExcludeMarking(EqualsAndHashCode.Include.class, "replaces", EqualsAndHashCode.Exclude.class, typeNode, annotation, annotationNode, false); + List<Included<L, EqualsAndHashCode.Include>> members = handleIncludeExcludeMarking(EqualsAndHashCode.Include.class, "replaces", EqualsAndHashCode.Exclude.class, typeNode, annotation, annotationNode, false); + + Collections.sort(members, new Comparator<Included<L, EqualsAndHashCode.Include>>() { + @Override public int compare(Included<L, EqualsAndHashCode.Include> a, Included<L, EqualsAndHashCode.Include> b) { + int ra = a.getInc() == null ? 0 : a.getInc().rank(); + int rb = b.getInc() == null ? 0 : b.getInc().rank(); + + return compareRankOrPosition(ra, rb, a.getNode(), b.getNode()); + } + }); + return members; + } + + private static <A extends AST<A, L, N>, L extends LombokNode<A, L, N>, N> int compareRankOrPosition(int ra, int rb, LombokNode<A, L, N> nodeA, LombokNode<A, L, N> nodeB) { + if (ra < rb) return +1; + if (ra > rb) return -1; + + int pa = nodeA.getStartPos(); + int pb = nodeB.getStartPos(); + + if (pa < pb) return -1; + if (pa > pb) return +1; + + return 0; } } |