@Override
public Object getValue(Symbolic origin) throws GuidanceException {
final com.sun.jdi.Value val = (com.sun.jdi.Value) getJDIValue(origin);
if (val instanceof IntegerValue) {
return this.calc.valInt(((IntegerValue) val).intValue());
} else if (val instanceof BooleanValue) {
return this.calc.valBoolean(((BooleanValue) val).booleanValue());
} else if (val instanceof CharValue) {
return this.calc.valChar(((CharValue) val).charValue());
} else if (val instanceof ByteValue) {
return this.calc.valByte(((ByteValue) val).byteValue());
} else if (val instanceof DoubleValue) {
return this.calc.valDouble(((DoubleValue) val).doubleValue());
} else if (val instanceof FloatValue) {
return this.calc.valFloat(((FloatValue) val).floatValue());
} else if (val instanceof LongValue) {
return this.calc.valLong(((LongValue) val).longValue());
} else if (val instanceof ShortValue) {
return this.calc.valShort(((ShortValue) val).shortValue());
} else if (val instanceof ObjectReference) {
return val;
} else { //val instanceof VoidValue || val == null
return null;
}
}
private static ArrayReference createTargetBytes(VirtualMachine vm, byte[] bytes,
ByteValue[] mirrorBytesCache) throws InvalidTypeException,
ClassNotLoadedException,
InternalExceptionWrapper,
VMDisconnectedExceptionWrapper,
ObjectCollectedExceptionWrapper,
UnsupportedOperationExceptionWrapper {
ArrayType bytesArrayClass = getArrayClass(vm, "byte[]");
ArrayReference array = null;
boolean disabledCollection = false;
while (!disabledCollection) {
array = ArrayTypeWrapper.newInstance(bytesArrayClass, bytes.length);
try {
ObjectReferenceWrapper.disableCollection(array);
disabledCollection = true;
} catch (ObjectCollectedExceptionWrapper ocex) {
// Collected too soon, try again...
}
}
List<Value> values = new ArrayList<Value>(bytes.length);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
ByteValue mb = mirrorBytesCache[128 + b];
if (mb == null) {
mb = VirtualMachineWrapper.mirrorOf(vm, b);
mirrorBytesCache[128 + b] = mb;
}
values.add(mb);
}
ArrayReferenceWrapper.setValues(array, values);
return array;
}
private static ArrayReference createTargetBytes(VirtualMachine vm, byte[] bytes,
ByteValue[] mirrorBytesCache) throws InvalidTypeException,
ClassNotLoadedException,
InternalExceptionWrapper,
VMDisconnectedExceptionWrapper,
ObjectCollectedExceptionWrapper,
UnsupportedOperationExceptionWrapper {
ArrayType bytesArrayClass = getArrayClass(vm, "byte[]");
ArrayReference array = null;
boolean disabledCollection = false;
while (!disabledCollection) {
array = ArrayTypeWrapper.newInstance(bytesArrayClass, bytes.length);
try {
ObjectReferenceWrapper.disableCollection(array);
disabledCollection = true;
} catch (ObjectCollectedExceptionWrapper ocex) {
// Collected too soon, try again...
}
}
List<Value> values = new ArrayList<Value>(bytes.length);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
ByteValue mb = mirrorBytesCache[128 + b];
if (mb == null) {
mb = VirtualMachineWrapper.mirrorOf(vm, b);
mirrorBytesCache[128 + b] = mb;
}
values.add(mb);
}
ArrayReferenceWrapper.setValues(array, values);
return array;
}
private static ArrayReference createTargetBytes(VirtualMachine vm, byte[] bytes,
ByteValue[] mirrorBytesCache) throws InvalidTypeException,
ClassNotLoadedException,
InternalExceptionWrapper,
VMDisconnectedExceptionWrapper,
ObjectCollectedExceptionWrapper {
ArrayType bytesArrayClass = getArrayClass(vm, "byte[]");
ArrayReference array = null;
boolean disabledCollection = false;
while (!disabledCollection) {
array = ArrayTypeWrapper.newInstance(bytesArrayClass, bytes.length);
try {
ObjectReferenceWrapper.disableCollection(array);
disabledCollection = true;
} catch (ObjectCollectedExceptionWrapper ocex) {
// Collected too soon, try again...
} catch (UnsupportedOperationExceptionWrapper uex) {
// Hope it will not be GC'ed...
disabledCollection = true;
}
}
List<Value> values = new ArrayList<Value>(bytes.length);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
ByteValue mb = mirrorBytesCache[128 + b];
if (mb == null) {
mb = VirtualMachineWrapper.mirrorOf(vm, b);
mirrorBytesCache[128 + b] = mb;
}
values.add(mb);
}
ArrayReferenceWrapper.setValues(array, values);
return array;
}
@Test
public void testValueOf() throws Exception {
Value i = this.getLocalValue("i");
Map<String, Object> options = formatter.getDefaultOptions();
Value newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options);
assertNotNull("NumericFormatter should be able to create integer by string.", newValue);
assertTrue("Should create an integer value.", newValue instanceof IntegerValue);
assertEquals("Should create an integer with right value.", "111", newValue.toString());
options.put(NUMERIC_FORMAT_OPTION, NumericFormatEnum.HEX);
newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options);
assertNotNull("NumericFormatter should be able to create integer by string.", newValue);
assertTrue("Should create an integer value.", newValue instanceof IntegerValue);
assertEquals("Should create an integer with right value.", "111", newValue.toString());
options.put(NUMERIC_FORMAT_OPTION, NumericFormatEnum.OCT);
newValue = formatter.valueOf(formatter.toString(i, options), i.type(), options);
assertNotNull("NumericFormatter should be able to create integer by string.", newValue);
assertTrue("Should create an integer value.", newValue instanceof IntegerValue);
assertEquals("Should create an integer with right value.", "111", newValue.toString());
newValue = formatter.valueOf("-12121212", i.type(), options);
assertNotNull("NumericFormatter should be able to create integer by string.", newValue);
assertTrue("Should create an integer value.", newValue instanceof IntegerValue);
assertEquals("Should create an integer with right value.", "-12121212", newValue.toString());
newValue = formatter.valueOf("0", i.type(), options);
assertNotNull("NumericFormatter should be able to create integer by string.", newValue);
assertTrue("Should create an integer value.", newValue instanceof IntegerValue);
assertEquals("Should create an integer with right value.", "0", newValue.toString());
VirtualMachine vm = getVM();
newValue = formatter.valueOf("0", vm.mirrorOf(10.0f).type(), options);
assertNotNull("NumericFormatter should be able to create float by string.", newValue);
assertTrue("Should create an float value.", newValue instanceof FloatValue);
assertEquals("Should create an float with right value.", "0.0", newValue.toString());
newValue = formatter.valueOf("10.0", vm.mirrorOf(10.0).type(), options);
assertNotNull("NumericFormatter should be able to create double by string.", newValue);
assertTrue("Should create an double value.", newValue instanceof DoubleValue);
assertEquals("Should create an double with right value.", "10.0", newValue.toString());
newValue = formatter.valueOf("10", vm.mirrorOf((short)10).type(), options);
assertNotNull("NumericFormatter should be able to create short by string.", newValue);
assertTrue("Should create an short value.", newValue instanceof ShortValue);
assertEquals("Should create an short with right value.", "10", newValue.toString());
newValue = formatter.valueOf("10", vm.mirrorOf(10L).type(), options);
assertNotNull("NumericFormatter should be able to create long by string.", newValue);
assertTrue("Should create an long value.", newValue instanceof LongValue);
assertEquals("Should create an long with right value.", "10", newValue.toString());
newValue = formatter.valueOf("10", vm.mirrorOf((byte) 10).type(), options);
assertNotNull("NumericFormatter should be able to create byte by string.", newValue);
assertTrue("Should create an byte value.", newValue instanceof ByteValue);
assertEquals("Should create an byte with right value.", "10", newValue.toString());
}