Abd*_*ery 5 java reflection android abstraction realm
我正在尝试实现对领域的抽象,以便在对数据库使用 CURD 操作时可以节省一些时间。
我构建的抽象是controller数据库操作,以便我可以使用controller它对任何表执行 CURD 操作。
即controller我所说的只是一个 Javaclass有四种方法
create update read delete。
这是create使用反射来创建db objects和绑定传递data object给那个的字段db object
/**
* this method will delete the old data "you can change that"
* of the table then store the passed data array in the table
*
* @param datum the data Object you want to
* save in the database
* @param map this map will contain which field
* value in the data class will be
* binded to which field in the db class
* and will have this form dataFieldName => dbFieldName
* @param callback when the function finish it's work it will
* return a boolean value indicate whether
* the function successfully finish it's work
*/
public void create(
Object datum,
Class dataClass,
HashMap<String, String> map,
SaveDataCallback callback
) {
Realm realm = Realm.getInstance(configuration);
realm.executeTransactionAsync(bgRealm -> {
long id;
Number currentId = bgRealm.where(clacc).max("id");//the clacc object is passed in the constructor of the controller
if (currentId == null)
id = 1;
else
id = currentId.longValue() + 1;
RealmObject dbObject = bgRealm.createObject(clacc, id++);//the clacc object is passed in the constructor of the controller
mapObjects(datum, dataClass, dbObject, clacc, map);
}
, () -> callback.onSavingDataFinished(true)
, error -> callback.onSavingDataFinished(false));
}
private void mapObjects(
Object source,
Class sourceClass,
Object destination,
Class destinationClass,
HashMap<String, String> map) {
String[] sourceFieldNames = map.keySet().toArray(new String[map.size()]);
try {
for (int i = 0; i < map.size(); i++) {
Field sourceField = sourceClass.getDeclaredField(sourceFieldNames[i]);
sourceField.setAccessible(true);
Object sourceValue = sourceField.get(source);
String destinationFieldName = map.get(sourceFieldNames[i]);
Field destinationField = destinationClass.getDeclaredField(destinationFieldName);
destinationField.setAccessible(true);
if (sourceField.getType() == Short.TYPE) {
destinationField.set(destination, Short.parseShort(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Integer.TYPE) {
destinationField.set(destination, Integer.parseInt(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Long.TYPE) {
destinationField.set(destination, Long.parseLong(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Float.TYPE) {
destinationField.set(destination, Float.parseFloat(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Double.TYPE) {
destinationField.set(destination, Double.parseDouble(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Byte.TYPE) {
destinationField.set(destination, Byte.parseByte(sourceValue.toString()));
continue;
}
if (sourceField.getType() == Boolean.TYPE) {
destinationField.set(destination, Boolean.parseBoolean(sourceValue.toString()));
continue;
}
destinationField.set(destination, sourceValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
问题如下:
当我尝试在进程完成后查询数据库以获取对象时,数据库返回我使用此函数创建的对象,但这些对象没有数据 actually the returned data is set to default value to each type i.e. string to null boolean to false etc...
我的问题是:
我的代码中是否有任何问题,或者领域数据库不支持在反射时为对象设置值?