Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/java.base/share/classes/java/text/CompactNumberFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,38 +233,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;

Expand Down Expand Up @@ -348,7 +353,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;

Expand Down Expand Up @@ -1435,7 +1440,7 @@ private void applyPattern(String count, String pattern, int index) {
}
}

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;
Expand Down Expand Up @@ -2364,8 +2369,14 @@ public int hashCode() {
@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;
}

Expand Down
95 changes: 95 additions & 0 deletions test/jdk/java/text/Format/CompactNumberFormat/TestClone.java
Original file line number Diff line number Diff line change
@@ -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));
}
}