Skip to content

Commit

Permalink
8323601: Improve LayoutPath.PathElement::toString
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee
  • Loading branch information
minborg committed Jan 31, 2024
1 parent f7121de commit ec56c72
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,8 @@ sealed interface PathElement permits LayoutPath.PathElementImpl {
static PathElement groupElement(String name) {
Objects.requireNonNull(name);
return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT,
path -> path.groupElement(name));
path -> path.groupElement(name),
"groupElement(\"" + name + "\")");
}

/**
Expand All @@ -879,7 +880,8 @@ static PathElement groupElement(long index) {
throw new IllegalArgumentException("Index < 0");
}
return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT,
path -> path.groupElement(index));
path -> path.groupElement(index),
"groupElement(" + index + ")");
}

/**
Expand All @@ -894,7 +896,8 @@ static PathElement sequenceElement(long index) {
throw new IllegalArgumentException("Index must be positive: " + index);
}
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT_INDEX,
path -> path.sequenceElement(index));
path -> path.sequenceElement(index),
"sequenceElement(" + index + ")");
}

/**
Expand Down Expand Up @@ -927,7 +930,8 @@ static PathElement sequenceElement(long start, long step) {
throw new IllegalArgumentException("Step must be != 0: " + step);
}
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_RANGE,
path -> path.sequenceElement(start, step));
path -> path.sequenceElement(start, step),
"sequenceElement(" + start + ", " + step + ")");
}

/**
Expand All @@ -940,7 +944,8 @@ static PathElement sequenceElement(long start, long step) {
*/
static PathElement sequenceElement() {
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT,
LayoutPath::sequenceElement);
LayoutPath::sequenceElement,
"sequenceElement()");
}

/**
Expand All @@ -949,7 +954,8 @@ static PathElement sequenceElement() {
*/
static PathElement dereferenceElement() {
return new LayoutPath.PathElementImpl(PathKind.DEREF_ELEMENT,
LayoutPath::derefElement);
LayoutPath::derefElement,
"dereferenceElement()");
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/java.base/share/classes/jdk/internal/foreign/LayoutPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,14 @@ public String description() {

final PathKind kind;
final UnaryOperator<LayoutPath> pathOp;
final String stringRepresentation;

public PathElementImpl(PathKind kind, UnaryOperator<LayoutPath> pathOp) {
public PathElementImpl(PathKind kind,
UnaryOperator<LayoutPath> pathOp,
String stringRepresentation) {
this.kind = kind;
this.pathOp = pathOp;
this.stringRepresentation = stringRepresentation;
}

@Override
Expand All @@ -403,5 +407,10 @@ public LayoutPath apply(LayoutPath layoutPath) {
public PathKind kind() {
return kind;
}

@Override
public String toString() {
return stringRepresentation;
}
}
}
36 changes: 36 additions & 0 deletions test/jdk/java/foreign/TestLayoutPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,42 @@ public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, lo
assertEquals(actualByteOffset, expectedByteOffset);
}

@Test
public void testGroupElementIndexToString() {
PathElement e = PathElement.groupElement(2);
assertEquals(e.toString(), "groupElement(2)");
}

@Test
public void testGroupElementNameToString() {
PathElement e = PathElement.groupElement("x");
assertEquals(e.toString(), "groupElement(\"x\")");
}

@Test
public void testSequenceElementToString() {
PathElement e = PathElement.sequenceElement();
assertEquals(e.toString(), "sequenceElement()");
}

@Test
public void testSequenceElementIndexToString() {
PathElement e = PathElement.sequenceElement(2);
assertEquals(e.toString(), "sequenceElement(2)");
}

@Test
public void testSequenceElementRangeToString() {
PathElement e = PathElement.sequenceElement(2, 4);
assertEquals(e.toString(), "sequenceElement(2, 4)");
}

@Test
public void testDerefereceElementToString() {
PathElement e = PathElement.dereferenceElement();
assertEquals(e.toString(), "dereferenceElement()");
}

@DataProvider
public static Object[][] testLayouts() {
List<Object[]> testCases = new ArrayList<>();
Expand Down

1 comment on commit ec56c72

@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.