Skip to content

Commit

Permalink
8328592: hprof tests fail with -XX:-CompactStrings
Browse files Browse the repository at this point in the history
Reviewed-by: phh, lmesnik, amenkov
  • Loading branch information
shipilev committed Mar 21, 2024
1 parent ac2f8e5 commit bb3e84b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
9 changes: 7 additions & 2 deletions test/lib/jdk/test/lib/hprof/model/JavaObject.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -188,9 +188,14 @@ public String describeReferenceTo(JavaThing target, Snapshot ss) {

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";
}
Expand Down
35 changes: 29 additions & 6 deletions test/lib/jdk/test/lib/hprof/model/JavaValueArray.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,7 @@
package jdk.test.lib.hprof.model;

import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Objects;

/**
Expand Down Expand Up @@ -348,15 +349,37 @@ public String valueString(boolean bigLimit) {
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();
Expand Down

1 comment on commit bb3e84b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.