ObjCObjectTypeConverter implements TypeConverter { public T fromNative(Object nativeValue, FromNativeContext context) { Number nativeValueAsNumber = (Number) nativeValue; if (nativeValueAsNumber == null) { return null; } ID id = ID.fromLong(nativeValueAsNumber.longValue()); if (id.isNull()) { return null; } boolean shouldRetain = shouldRetainFor(context); return Rococoa.wrap(id, javaType, shouldRetain); } ObjCObjectTypeConverter(Class<T> javaType); Class<?> nativeType(); T fromNative(Object nativeValue, FromNativeContext context); Object toNative(Object value, ToNativeContext context);  }
@Test public void convertsReturnedIDToNSObjectSubclass() { FromNativeConverter converter = new ObjCObjectTypeConverter(NSNumber.class); NSNumber number = Rococoa.create("NSNumber", NSNumber.class, "numberWithInt:", 45); Number nativeValue = new Long(number.id().longValue()); NSNumber converted = (NSNumber) converter.fromNative(nativeValue, null); assertEquals(converted.id(), number.id()); assertEquals(45, converted.intValue()); if (NativeLong.SIZE == 4) { nativeValue = new Integer(number.id().intValue()); converted = (NSNumber) converter.fromNative(nativeValue, null); assertEquals(45, converted.intValue()); } }
@Test public void convertsReturnedNilToNull() { FromNativeConverter converter = new ObjCObjectTypeConverter(NSNumber.class); Number nativeValue = new Long(0); assertNull(converter.fromNative(nativeValue, null)); }
@Test public void returnedNSObjectIsNormallyRetained() { FromNativeConverter converter = new ObjCObjectTypeConverter(NSNumber.class); NSNumber number = Rococoa.create("NSNumber", NSNumber.class, "numberWithInt:", 45); assertRetainCount(2, number); NSNumber converted = (NSNumber) converter.fromNative(new Long(number.id().longValue()), null); assertRetainCount(3, converted); assertRetainCount(3, number); }