I'm working on a code base which uses JNI techniques for modeling native methods.
Here is the segment of the native peer method used for java.lang.String#equals(Object)
@MJI
public boolean equals__Ljava_lang_Object_2__Z (MJIEnv env, int objRef, int argRef) {
ElementInfo s1 = heap.get(objRef); // this
ElementInfo s2 = heap.get(argRef);
Fields f1 = heap.get(s1.getField("value")).getFields();
Fields f2 = heap.get(s2.getField("value")).getFields();
char[] c1 = ((CharArrayFields) f1).asCharArray();
char[] c2 = ((CharArrayFields) f2).asCharArray();
This works fine on Java 8. But in Java 9 and later, the value returned for the value
field of the String, is either char[]
or byte[]
I expect it to return a byte[] array since the changes made in JEP 254: Compact Strings
So for instance:
char[] chars = new char[] {'a','b','c', 'd'};
String str1 = new String(chars))
"str2".equals(str1);
here I get a char array for str1
and byte array for "str2"
in the peer method. Is this because that Strings are stored differently in heap?
FYI:
Here is the code that I'm actually working on. I'm trying to make it work with Java 9 and later:
jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_lang_String.java#L166-L200
As you may see the valueField
there is cast to CharArrayFields. But when running on Java "10.0.1", valueField is sometimes a CharArrayFields, and sometimes a ByteArrayFields.