Skip to content

Commit c8a9dd3

Browse files
committed
8346609: Improve MemorySegment.toString
Reviewed-by: mcimadamore
1 parent 4d8fb80 commit c8a9dd3

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -486,9 +486,18 @@ public int characteristics() {
486486

487487
@Override
488488
public String toString() {
489-
return "MemorySegment{ " +
490-
heapBase().map(hb -> "heapBase: " + hb + ", ").orElse("") +
491-
"address: " + Utils.toHexString(address()) +
489+
final String kind;
490+
if (this instanceof HeapMemorySegmentImpl) {
491+
kind = "heap";
492+
} else if (this instanceof MappedMemorySegmentImpl) {
493+
kind = "mapped";
494+
} else {
495+
kind = "native";
496+
}
497+
return "MemorySegment{ kind: " +
498+
kind +
499+
heapBase().map(hb -> ", heapBase: " + hb).orElse("") +
500+
", address: " + Utils.toHexString(address()) +
492501
", byteSize: " + length +
493502
" }";
494503
}

test/jdk/java/foreign/TestByteBuffer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -260,6 +260,7 @@ public void testMappedSegmentOperations(MappedSegmentOp mappedBufferOp) throws T
260260
try (FileChannel fileChannel = FileChannel.open(f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
261261
MemorySegment segment = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 8L, arena);
262262
assertTrue(segment.isMapped());
263+
assertTrue(segment.toString().contains("mapped"));
263264
arena.close();
264265
mappedBufferOp.apply(segment);
265266
}

test/jdk/java/foreign/TestSegments.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -234,8 +234,10 @@ public void testToString(Supplier<MemorySegment> segmentSupplier) {
234234
assertTrue(s.contains("byteSize: "));
235235
if (segment.heapBase().isPresent()) {
236236
assertTrue(s.contains("heapBase: ["));
237+
assertFalse(s.contains("native"));
237238
} else {
238239
assertFalse(s.contains("heapBase: "));
240+
assertTrue(s.contains("native"));
239241
}
240242
assertFalse(s.contains("Optional"));
241243
}

0 commit comments

Comments
 (0)