Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ enum Kind {
* @throws IllegalArgumentException if there is no such member
*/
public static Kind valueOf(int refKind) {
return valueOf(refKind, false);
return valueOf(refKind, refKind == REF_invokeInterface);
}

/**
Expand Down Expand Up @@ -137,13 +137,14 @@ public static Kind valueOf(int refKind, boolean isInterface) {
if (i >= 0 && i < TABLE.length) {
Kind kind = TABLE[i];
if (kind == null) {
throw new IllegalArgumentException(String.format("refKind=%d", refKind));
throw new IllegalArgumentException(String.format("refKind=%d isInterface=%s", refKind, isInterface));
}
if (kind.refKind == refKind && kind.isInterface == isInterface) {
if (kind.refKind == refKind &&
(refKind != REF_invokeStatic || refKind != REF_invokeSpecial || kind.isInterface == isInterface)){
Copy link
Member

Choose a reason for hiding this comment

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

It reads to me that the static initializer tries to set up the table such that valueOf(refKind, isInterface) should find the proper kind to return except this:

// There is not a specific Kind for interfaces
if (kind == VIRTUAL) kind = INTERFACE_VIRTUAL;

This changes the entry for REF_invokeVirtual to kind INTERFACE_VIRTUAL. Do you know why? If this entry is set to VIRTUAL, then each refKind has two entries in the table corresponding to the correct result for valueOf.

return kind;
}
}
throw new IllegalArgumentException(String.format("refKind=%d", refKind));
throw new IllegalArgumentException(String.format("refKind=%d isInterface=%s", refKind, isInterface));
}

private static int tableIndex(int refKind, boolean isInterface) {
Expand Down
16 changes: 16 additions & 0 deletions test/jdk/java/lang/constant/MethodHandleDescTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import static java.lang.constant.ConstantDescs.CD_String;
import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.CD_void;
import static java.lang.invoke.MethodHandleInfo.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -359,7 +361,21 @@ public void testSymbolicDescsConstants() throws ReflectiveOperationException {

public void testKind() {
for (Kind k : Kind.values()) {
assertEquals(Kind.valueOf(k.refKind), Kind.valueOf(k.refKind, k.refKind == MethodHandleInfo.REF_invokeInterface));
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the test does not verify the cases specified by valueOf(int refKind, boolean isInterface).
i.e. For most values of refKind, there is an exact match regardless of the value of isInterface except REF_invokeStatic and REF_invokeSpecial.

Do you mind adding those cases?

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Jun 10, 2021

Choose a reason for hiding this comment

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

Looks like the test does not verify the cases specified by valueOf(int refKind, boolean isInterface).
i.e. For most values of refKind, there is an exact match regardless of the value of isInterface except REF_invokeStatic and REF_invokeSpecial.

Do you mind adding those cases?

hum, the implementation for valueOf(int refKind, boolean isInterface) is incorrect, the behavior does depends on the value of isInterface for example: Kind.valueOf(1, false) returns GETTER while Kind.valueOf(1, true) fails with java.lang.IllegalArgumentException will fix the implementation of valueOf(int refKind, boolean isInterface) for it to match its spec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mlchung I have updated the PR with another commit, thanks for your comments

Copy link
Member

Choose a reason for hiding this comment

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

It may be better to create a new JBS issue to fix this bug as it may take more time to investigate.

assertEquals(Kind.valueOf(k.refKind, k.isInterface), k);
}
// let's now verify those cases for which the value of the isInterface parameter is ignored
int[] isInterfaceIgnored = new int[] {
REF_getField,
REF_getStatic,
REF_putField,
REF_putStatic,
REF_newInvokeSpecial,
REF_invokeInterface
};

for (int refKind : isInterfaceIgnored) {
assertEquals(Kind.valueOf(refKind, false), Kind.valueOf(refKind, true));
}
}
}