Skip to content

Commit

Permalink
Support for Java 10 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-reich authored and aragozin committed Jun 28, 2018
1 parent e60adfa commit 0f9fcb9
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,38 @@ else if (steps.length > 0 && steps[steps.length - 1] instanceof ArrayIndexStep)
}

public static String stringValue(Instance obj) {
if (obj == null) return null;

if (!"java.lang.String".equals(obj.getJavaClass().getName()))
throw new IllegalArgumentException("Is not a string: " + obj.getInstanceId() + " (" + obj.getJavaClass().getName() + ")");

Boolean COMPACT_STRINGS = (Boolean) obj.getJavaClass().getValueOfStaticField("COMPACT_STRINGS");
if (COMPACT_STRINGS == null)
return stringValue_java8(obj); // We're pre Java 9

Object valueInstance = obj.getValueOfField("value");
PrimitiveArrayInstance chars = (PrimitiveArrayInstance) valueInstance;

byte UTF16 = 1;
byte coder = COMPACT_STRINGS ? (Byte) obj.getValueOfField("coder") : UTF16;
int len = chars.getLength() >> coder;
char[] text = new char[len];

List<String> values = (List) chars.getValues();
if (coder == UTF16)
for (int i = 0; i < text.length; i++) {
text[i] = (char)
((Integer.parseInt(values.get(i*2+1)) << 8)
| (Integer.parseInt(values.get(i*2)) & 0xFF));
}
else
for (int i = 0; i < text.length; i++)
text[i] = (char) Integer.parseInt(values.get(i));

return new String(text);
}

static String stringValue_java8(Instance obj) {
if (obj == null) {
return null;
}
Expand Down Expand Up @@ -469,4 +501,4 @@ private interface PrimitiveParser {

public Object toValue(String x);
}
}
}

0 comments on commit 0f9fcb9

Please sign in to comment.