mirror of
https://github.com/openjdk/jdk.git
synced 2026-02-11 19:08:23 +00:00
8368328: CompactNumberFormat.clone does not produce independent instances
Reviewed-by: rgiulietti, jlu
This commit is contained in:
parent
aa6ff45052
commit
bdf6853cfd
@ -250,38 +250,43 @@ public final class CompactNumberFormat extends NumberFormat {
|
||||
|
||||
/**
|
||||
* List of positive prefix patterns of this formatter's
|
||||
* compact number patterns.
|
||||
* compact number patterns. This field is a read-only
|
||||
* constant once initialized.
|
||||
*/
|
||||
private transient List<Patterns> positivePrefixPatterns;
|
||||
|
||||
/**
|
||||
* List of negative prefix patterns of this formatter's
|
||||
* compact number patterns.
|
||||
* compact number patterns. This field is a read-only
|
||||
* constant once initialized.
|
||||
*/
|
||||
private transient List<Patterns> negativePrefixPatterns;
|
||||
|
||||
/**
|
||||
* List of positive suffix patterns of this formatter's
|
||||
* compact number patterns.
|
||||
* compact number patterns. This field is a read-only
|
||||
* constant once initialized.
|
||||
*/
|
||||
private transient List<Patterns> positiveSuffixPatterns;
|
||||
|
||||
/**
|
||||
* List of negative suffix patterns of this formatter's
|
||||
* compact number patterns.
|
||||
* compact number patterns. This field is a read-only
|
||||
* constant once initialized.
|
||||
*/
|
||||
private transient List<Patterns> negativeSuffixPatterns;
|
||||
|
||||
/**
|
||||
* List of divisors of this formatter's compact number patterns.
|
||||
* Divisor can be either Long or BigInteger (if the divisor value goes
|
||||
* beyond long boundary)
|
||||
* beyond long boundary). This field is a read-only constant
|
||||
* once initialized.
|
||||
*/
|
||||
private transient List<Number> divisors;
|
||||
|
||||
/**
|
||||
* List of place holders that represent minimum integer digits at each index
|
||||
* for each count.
|
||||
* for each count. This field is a read-only constant once initialized.
|
||||
*/
|
||||
private transient List<Patterns> placeHolderPatterns;
|
||||
|
||||
@ -374,7 +379,7 @@ public final class CompactNumberFormat extends NumberFormat {
|
||||
|
||||
/**
|
||||
* The map for plural rules that maps LDML defined tags (e.g. "one") to
|
||||
* its rule.
|
||||
* its rule. This field is a read-only constant once initialized.
|
||||
*/
|
||||
private transient Map<String, String> rulesMap;
|
||||
|
||||
@ -1515,7 +1520,7 @@ public final class CompactNumberFormat extends NumberFormat {
|
||||
}
|
||||
}
|
||||
|
||||
private final transient DigitList digitList = new DigitList();
|
||||
private transient DigitList digitList = new DigitList();
|
||||
private static final int STATUS_INFINITE = 0;
|
||||
private static final int STATUS_POSITIVE = 1;
|
||||
private static final int STATUS_LENGTH = 2;
|
||||
@ -2506,8 +2511,14 @@ public final class CompactNumberFormat extends NumberFormat {
|
||||
@Override
|
||||
public CompactNumberFormat clone() {
|
||||
CompactNumberFormat other = (CompactNumberFormat) super.clone();
|
||||
|
||||
// Cloning reference fields. Other fields (e.g., "positivePrefixPatterns")
|
||||
// are not cloned since they are read-only constants after initialization.
|
||||
other.compactPatterns = compactPatterns.clone();
|
||||
other.symbols = (DecimalFormatSymbols) symbols.clone();
|
||||
other.decimalFormat = (DecimalFormat) decimalFormat.clone();
|
||||
other.defaultDecimalFormat = (DecimalFormat) defaultDecimalFormat.clone();
|
||||
other.digitList = (DigitList) digitList.clone();
|
||||
return other;
|
||||
}
|
||||
|
||||
|
||||
95
test/jdk/java/text/Format/CompactNumberFormat/TestClone.java
Normal file
95
test/jdk/java/text/Format/CompactNumberFormat/TestClone.java
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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 8368328
|
||||
* @summary Tests if CompactNumberFormat.clone() creates an independent object
|
||||
* @run junit/othervm --add-opens java.base/java.text=ALL-UNNAMED TestClone
|
||||
*/
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.text.CompactNumberFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
public class TestClone {
|
||||
// Concurrently parse numbers using cloned instances as originally
|
||||
// reported in the bug. This test could produce false negative results,
|
||||
// depending on the testing environment
|
||||
@Test
|
||||
void randomAccessTest() {
|
||||
var original =
|
||||
NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
|
||||
var threads = IntStream.range(0, 10)
|
||||
.mapToObj(num -> new Thread(() -> {
|
||||
var clone = (NumberFormat) original.clone();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
assertDoesNotThrow(() ->
|
||||
assertEquals(num, clone.parse(String.valueOf(num)).intValue()));
|
||||
}
|
||||
})).toList();
|
||||
threads.forEach(Thread::start);
|
||||
threads.forEach(t -> {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException ie) {
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static Stream<Arguments> referenceFields() throws ClassNotFoundException {
|
||||
return Stream.of(
|
||||
Arguments.of("compactPatterns", String[].class),
|
||||
Arguments.of("symbols", DecimalFormatSymbols.class),
|
||||
Arguments.of("decimalFormat", DecimalFormat.class),
|
||||
Arguments.of("defaultDecimalFormat", DecimalFormat.class),
|
||||
Arguments.of("digitList", Class.forName("java.text.DigitList"))
|
||||
);
|
||||
}
|
||||
// Explicitly checks if the cloned object has its own references for
|
||||
// "compactPatterns", "symbols", "decimalFormat", "defaultDecimalFormat",
|
||||
// and "digitList"
|
||||
@ParameterizedTest
|
||||
@MethodSource("referenceFields")
|
||||
void whiteBoxTest(String fieldName, Class<?> type) throws Throwable {
|
||||
var original = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
|
||||
var clone = original.clone();
|
||||
var lookup = MethodHandles.privateLookupIn(CompactNumberFormat.class, MethodHandles.lookup());
|
||||
|
||||
assertNotSame(lookup.findGetter(CompactNumberFormat.class, fieldName, type).invoke(original),
|
||||
lookup.findGetter(CompactNumberFormat.class, fieldName, type).invoke(clone));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user