8365077: java.awt.font.NumericShaper violates equals/hashCode contract

Reviewed-by: kizune, psadhukhan
This commit is contained in:
Phil Race 2025-10-17 17:57:21 +00:00
parent a3e41ea6c6
commit 0103f21635
2 changed files with 102 additions and 10 deletions

View File

@ -346,6 +346,19 @@ public final class NumericShaper implements java.io.Serializable {
return index < NUM_KEYS ? Range.values()[index] : null;
}
private static int toRangeHash(Set<Range> ranges) {
int m = 0;
for (Range range : ranges) {
int index = range.ordinal();
if (index < NUM_KEYS) {
m |= 1 << index;
} else {
m |= (1 << NUM_KEYS) + index;
}
}
return m;
}
private static int toRangeMask(Set<Range> ranges) {
int m = 0;
for (Range range : ranges) {
@ -576,7 +589,7 @@ public final class NumericShaper implements java.io.Serializable {
// and a linear probe is ok.
private static int ctCache = 0;
private static int ctCacheLimit = contexts.length - 2;
private static final int ctCacheLimit = contexts.length - 2;
// warning, synchronize access to this as it modifies state
private static int getContextKey(char c) {
@ -1510,6 +1523,9 @@ public final class NumericShaper implements java.io.Serializable {
private NumericShaper(int key, int mask) {
this.key = key;
this.mask = mask;
if (((this.mask & ARABIC) != 0) && ((this.mask & EASTERN_ARABIC) != 0)) {
this.mask &= ~ARABIC;
}
}
private NumericShaper(Range defaultContext, Set<Range> ranges) {
@ -1795,15 +1811,7 @@ public final class NumericShaper implements java.io.Serializable {
* @see java.lang.Object#hashCode
*/
public int hashCode() {
int hash = mask;
if (rangeSet != null) {
// Use the CONTEXTUAL_MASK bit only for the enum-based
// NumericShaper. A deserialized NumericShaper might have
// bit masks.
hash &= CONTEXTUAL_MASK;
hash ^= rangeSet.hashCode();
}
return hash;
return (rangeSet != null) ? Range.toRangeHash(rangeSet) : (mask & ~CONTEXTUAL_MASK);
}
/**

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8365077
* @summary confirm that an instance which is created with Enum ranges is
* equal to another instance which is created with equivalent traditional
* ranges, and that in such a case the hashCodes are also equal.
*/
import java.awt.font.NumericShaper;
import java.awt.font.NumericShaper.Range;
import static java.awt.font.NumericShaper.Range.*;
import java.util.EnumSet;
public class NSEqualsTest {
public static void main(String[] args) {
for (Range r1 : Range.values()) {
test(r1);
for (Range r2 : Range.values()) {
test(r1, r2);
}
}
}
static void test(Range r) {
if (r.ordinal() > MONGOLIAN.ordinal()) {
return;
}
int o = 1 << r.ordinal();
NumericShaper nsr = NumericShaper.getContextualShaper(EnumSet.of(r));
NumericShaper nso = NumericShaper.getContextualShaper(o);
printAndCompare(nsr, nso);
}
static void test(Range r1, Range r2) {
if (r1.ordinal() > MONGOLIAN.ordinal() || r2.ordinal() > MONGOLIAN.ordinal()) {
return;
}
int o1 = 1 << r1.ordinal();
int o2 = 1 << r2.ordinal();
NumericShaper nsr = NumericShaper.getContextualShaper(EnumSet.of(r1, r2));
NumericShaper nso = NumericShaper.getContextualShaper(o1 | o2);
printAndCompare(nsr, nso);
}
static void printAndCompare(NumericShaper nsr, NumericShaper nso) {
System.err.println(nsr);
System.err.println(nso);
System.err.println(nsr.hashCode() + " vs " + nso.hashCode() +
" equal: " + nsr.equals(nso));
if (!nsr.equals(nso)) {
throw new RuntimeException("Expected equal");
}
if (nsr.hashCode() != nso.hashCode()) {
throw new RuntimeException("Different hash codes:");
}
}
}