HectorObjectMapper { <T> T createObject(CFMappingDef<T> cfMapDef, Object pkObj, ColumnSlice<String, byte[]> slice) { if (slice.getColumns().isEmpty()) { return null; } CFMappingDef<? extends T> cfMapDefInstance = determineClassType(cfMapDef, slice); T obj; try { try { obj = cfMapDefInstance.getEffectiveClass().newInstance(); } catch (InstantiationException e) { throw new HectorObjectMapperException(cfMapDefInstance.getEffectiveClass().getName() + ", must have default constructor", e); } setIdIfCan(cfMapDef, obj, pkObj); for (HColumn<String, byte[]> col : slice.getColumns()) { String colName = col.getName(); PropertyMappingDefinition md = cfMapDefInstance.getPropMapByColumnName(colName); if (null != md && null != md.getPropDesc()) { if (!md.isCollectionType()) { setPropertyUsingColumn(obj, col, md); } else { collMapperHelper.instantiateCollection(obj, col, md); } } else if (collMapperHelper.addColumnToCollection(cfMapDefInstance, obj, colName, col.getValue())) { continue; } else if (null != cfMapDef.getDiscColumn() && colName.equals(cfMapDef.getDiscColumn())) { continue; } else { addToExtraIfCan(obj, cfMapDef, col); } } return obj; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } } HectorObjectMapper(ClassCacheMgr cacheMgr); T getObject(Keyspace keyspace, String colFamName, I pkObj); T saveObj(Keyspace keyspace, T obj); Collection<?> saveObjCollection(Keyspace keyspace, Collection<?> objColl); Collection<?> saveObjCollection(Keyspace keyspace, Collection<?> objColl, Mutator<byte[]> m); static Serializer<?> determineSerializer(Class<?> theType); static boolean isSerializable(Class<?> clazz); static boolean isImplementedBy(Class<?> clazz, Class<?> target); void setKeyConcatStrategy(KeyConcatenationStrategy keyConcatStrategy);  }
@Test public void testCreateInstance() { UUID id = UUID.randomUUID(); long longProp1 = 1L; Long longProp2 = 2L; int intProp1 = 3; Integer intProp2 = 4; boolean boolProp1 = false; Boolean boolProp2 = true; String strProp = "a string property"; UUID uuidProp = UUID.randomUUID(); Date dateProp = new Date(); byte[] bytesProp = "somebytes".getBytes(); String extraProp = "extra extra"; Colors color = Colors.RED; MySerial serialProp = new MySerial(1, 2L); ColumnSliceMockImpl slice = new ColumnSliceMockImpl(); slice.add("lp1", LongSerializer.get().toBytes(longProp1)); slice.add("lp2", LongSerializer.get().toBytes(longProp2)); slice.add("ip1", IntegerSerializer.get().toBytes(intProp1)); slice.add("ip2", IntegerSerializer.get().toBytes(intProp2)); slice.add("bp1", BooleanSerializer.get().toBytes(boolProp1)); slice.add("bp2", BooleanSerializer.get().toBytes(boolProp2)); slice.add("sp", StringSerializer.get().toBytes(strProp)); slice.add("up", UUIDSerializer.get().toBytes(uuidProp)); slice.add("dp", DateSerializer.get().toBytes(dateProp)); slice.add("bytes", BytesArraySerializer.get().toBytes(bytesProp)); slice.add("color", StringSerializer.get().toBytes(color.getName())); slice.add("serialProp", ObjectSerializer.get().toBytes(serialProp)); slice.add("extra", StringSerializer.get().toBytes(extraProp)); CFMappingDef<MyTestBean> cfMapDef = cacheMgr.getCfMapDef(MyTestBean.class, true); MyTestBean obj = new HectorObjectMapper(cacheMgr).createObject(cfMapDef, id, slice); assertEquals(id, obj.getBaseId()); assertEquals(longProp1, obj.getLongProp1()); assertEquals(longProp2, obj.getLongProp2()); assertEquals(intProp1, obj.getIntProp1()); assertEquals(intProp2, obj.getIntProp2()); assertFalse(obj.isBoolProp1()); assertTrue(obj.getBoolProp2()); assertEquals(strProp, obj.getStrProp()); assertEquals(uuidProp, obj.getUuidProp()); assertEquals("somebytes", new String(obj.getBytesProp())); assertEquals(dateProp.getTime(), obj.getDateProp().getTime()); assertEquals(serialProp, obj.getSerialProp()); assertEquals(extraProp, obj.getAnonymousProp("extra")); }
@Test public void testCreateInstanceCustomIdType() { Colors id = Colors.GREEN; long longProp1 = 1L; ColumnSliceMockImpl slice = new ColumnSliceMockImpl(); slice.add("lp1", LongSerializer.get().toBytes(longProp1)); CFMappingDef<MyCustomIdBean> cfMapDef = cacheMgr.getCfMapDef(MyCustomIdBean.class, true); MyCustomIdBean obj = new HectorObjectMapper(cacheMgr).createObject(cfMapDef, id, slice); assertEquals(id, obj.getId()); assertEquals(longProp1, obj.getLongProp1()); }