* If maxChars is negative, the returned string is not truncated.
*/
public static String asString(Instance inst, int maxChars) {
- if (!isInstanceOfClass(inst, "java.lang.String")) {
- return null;
+ // The inst object could either be a java.lang.String or a char[]. If it
+ // is a char[], use that directly as the value, otherwise use the value
+ // field of the string object. The field accesses for count and offset
+ // later on will work okay regardless of what type the inst object is.
+ Object value = inst;
+ if (isInstanceOfClass(inst, "java.lang.String")) {
+ value = getField(inst, "value");
}
- Object value = getField(inst, "value");
if (!(value instanceof ArrayInstance)) {
return null;
}
}
@Test
+ public void asStringCharArray() throws IOException {
+ TestDump dump = TestDump.getTestDump();
+ Instance str = (Instance)dump.getDumpedThing("charArray");
+ assertEquals("char thing", InstanceUtils.asString(str));
+ }
+
+ @Test
public void asStringTruncated() throws IOException {
TestDump dump = TestDump.getTestDump();
Instance str = (Instance)dump.getDumpedThing("basicString");
}
@Test
+ public void asStringCharArrayTruncated() throws IOException {
+ TestDump dump = TestDump.getTestDump();
+ Instance str = (Instance)dump.getDumpedThing("charArray");
+ assertEquals("char ", InstanceUtils.asString(str, 5));
+ }
+
+ @Test
public void asStringExactMax() throws IOException {
TestDump dump = TestDump.getTestDump();
Instance str = (Instance)dump.getDumpedThing("basicString");
}
@Test
+ public void asStringCharArrayExactMax() throws IOException {
+ TestDump dump = TestDump.getTestDump();
+ Instance str = (Instance)dump.getDumpedThing("charArray");
+ assertEquals("char thing", InstanceUtils.asString(str, 10));
+ }
+
+ @Test
public void asStringNotTruncated() throws IOException {
TestDump dump = TestDump.getTestDump();
Instance str = (Instance)dump.getDumpedThing("basicString");
}
@Test
+ public void asStringCharArrayNotTruncated() throws IOException {
+ TestDump dump = TestDump.getTestDump();
+ Instance str = (Instance)dump.getDumpedThing("charArray");
+ assertEquals("char thing", InstanceUtils.asString(str, 50));
+ }
+
+ @Test
public void asStringNegativeMax() throws IOException {
TestDump dump = TestDump.getTestDump();
Instance str = (Instance)dump.getDumpedThing("basicString");
}
@Test
+ public void asStringCharArrayNegativeMax() throws IOException {
+ TestDump dump = TestDump.getTestDump();
+ Instance str = (Instance)dump.getDumpedThing("charArray");
+ assertEquals("char thing", InstanceUtils.asString(str, -3));
+ }
+
+ @Test
public void asStringNull() throws IOException {
TestDump dump = TestDump.getTestDump();
Instance obj = (Instance)dump.getDumpedThing("nullString");