From bb3e84bd1fd8217fcb319de8a0716e44359e3423 Mon Sep 17 00:00:00 2001 From: Aleksey Shipilev Date: Thu, 21 Mar 2024 12:36:14 +0000 Subject: [PATCH] 8328592: hprof tests fail with -XX:-CompactStrings Reviewed-by: phh, lmesnik, amenkov --- .../jdk/test/lib/hprof/model/JavaObject.java | 9 +++-- .../test/lib/hprof/model/JavaValueArray.java | 35 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/test/lib/jdk/test/lib/hprof/model/JavaObject.java b/test/lib/jdk/test/lib/hprof/model/JavaObject.java index 677c19799b0..6ab2634588f 100644 --- a/test/lib/jdk/test/lib/hprof/model/JavaObject.java +++ b/test/lib/jdk/test/lib/hprof/model/JavaObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, 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 @@ -188,9 +188,14 @@ public class JavaObject extends JavaLazyReadObject { public String toString() { if (getClazz().isString()) { + JavaThing coder = getField("coder"); + boolean compact = false; + if (coder instanceof JavaByte) { + compact = ((JavaByte)coder).value == 0; + } JavaThing value = getField("value"); if (value instanceof JavaValueArray) { - return ((JavaValueArray)value).valueAsString(); + return ((JavaValueArray)value).valueAsString(compact); } else { return "null"; } diff --git a/test/lib/jdk/test/lib/hprof/model/JavaValueArray.java b/test/lib/jdk/test/lib/hprof/model/JavaValueArray.java index 7a76b5f094a..74812701a43 100644 --- a/test/lib/jdk/test/lib/hprof/model/JavaValueArray.java +++ b/test/lib/jdk/test/lib/hprof/model/JavaValueArray.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, 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 @@ -31,6 +31,7 @@ package jdk.test.lib.hprof.model; import java.io.IOException; +import java.nio.ByteOrder; import java.util.Objects; /** @@ -348,15 +349,37 @@ public class JavaValueArray extends JavaLazyReadObject return result.toString(); } + private static final int STRING_HI_BYTE_SHIFT; + private static final int STRING_LO_BYTE_SHIFT; + static { + if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { + STRING_HI_BYTE_SHIFT = 8; + STRING_LO_BYTE_SHIFT = 0; + } else { + STRING_HI_BYTE_SHIFT = 0; + STRING_LO_BYTE_SHIFT = 8; + } + } + // Tries to represent the value as string (used by JavaObject.toString). - public String valueAsString() { + public String valueAsString(boolean compact) { if (getElementType() == 'B') { JavaThing[] things = getValue(); - byte[] bytes = new byte[things.length]; - for (int i = 0; i < things.length; i++) { - bytes[i] = ((JavaByte)things[i]).value; + if (compact) { + byte[] bytes = new byte[things.length]; + for (int i = 0; i < things.length; i++) { + bytes[i] = ((JavaByte)things[i]).value; + } + return new String(bytes); + } else { + char[] chars = new char[things.length / 2]; + for (int i = 0; i < things.length; i += 2) { + int b1 = ((JavaByte)things[i]).value << STRING_HI_BYTE_SHIFT; + int b2 = ((JavaByte)things[i + 1]).value << STRING_LO_BYTE_SHIFT; + chars[i / 2] = (char)(b1 | b2); + } + return new String(chars); } - return new String(bytes); } // fallback return valueString();