8346609: Improve MemorySegment.toString

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2025-01-07 15:18:02 +00:00
parent 4d8fb80732
commit c8a9dd3a02
3 changed files with 18 additions and 6 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -486,9 +486,18 @@ public abstract sealed class AbstractMemorySegmentImpl
@Override @Override
public String toString() { public String toString() {
return "MemorySegment{ " + final String kind;
heapBase().map(hb -> "heapBase: " + hb + ", ").orElse("") + if (this instanceof HeapMemorySegmentImpl) {
"address: " + Utils.toHexString(address()) + kind = "heap";
} else if (this instanceof MappedMemorySegmentImpl) {
kind = "mapped";
} else {
kind = "native";
}
return "MemorySegment{ kind: " +
kind +
heapBase().map(hb -> ", heapBase: " + hb).orElse("") +
", address: " + Utils.toHexString(address()) +
", byteSize: " + length + ", byteSize: " + length +
" }"; " }";
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -260,6 +260,7 @@ public class TestByteBuffer {
try (FileChannel fileChannel = FileChannel.open(f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) { try (FileChannel fileChannel = FileChannel.open(f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
MemorySegment segment = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 8L, arena); MemorySegment segment = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 8L, arena);
assertTrue(segment.isMapped()); assertTrue(segment.isMapped());
assertTrue(segment.toString().contains("mapped"));
arena.close(); arena.close();
mappedBufferOp.apply(segment); mappedBufferOp.apply(segment);
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -234,8 +234,10 @@ public class TestSegments {
assertTrue(s.contains("byteSize: ")); assertTrue(s.contains("byteSize: "));
if (segment.heapBase().isPresent()) { if (segment.heapBase().isPresent()) {
assertTrue(s.contains("heapBase: [")); assertTrue(s.contains("heapBase: ["));
assertFalse(s.contains("native"));
} else { } else {
assertFalse(s.contains("heapBase: ")); assertFalse(s.contains("heapBase: "));
assertTrue(s.contains("native"));
} }
assertFalse(s.contains("Optional")); assertFalse(s.contains("Optional"));
} }