android 技术交流群 818676923
import android.content.Context;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmModel;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.PrimaryKey;
/**
* Created by YuX on 2018/6/27 10:05
* ^_^ qq:565749553
* 工具类的使用方式:
* 需要创建数据库实体类,实体类以需要存储的数据类型为模板,具有和模板类型相同的字段并且继承RealmObject类
* 工具类支持数据类型, Integer int Double double Boolean Long long boolean String
* save 保存一个对象
* savearr 保存一个ArrayList
* findall 返回 ArrayList<M>
*/
public class RealmUtils {
private Realm infoRealm;
private Context context;
/**
* @param context 当前activity
*/
public void init(Context context, String name) {
if (infoRealm == null) {
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder().name(name).build();
infoRealm = Realm.getInstance(config);
} else {
RealmConfiguration config = new RealmConfiguration.Builder().name(name).build();
infoRealm = Realm.getInstance(config);
}
}
/**
* 保存一个Object类型的数据到realm
*
* @param instance 原数据对象
* @param s 指数据存储类型
* @param p 指目标数据库类型
* @param <S> 没有继承RealmObject 的
* @param <P> 数据库存储使用的继承了RealmObject的
* 这个方法用于把fastjson对象保存到realm数据库中
*/
public <S, P extends RealmObject> void save(final Object instance, final Class<S> s, final Class<P> p, Object PkeyVal,String PKeyName) {
infoRealm.beginTransaction();
P pinstance=null;
Field[] field = s.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
if(name.equals(PKeyName)) {
Method methodGet = instance.getClass().getMethod("get" + name);
pinstance=infoRealm.where(p).contains(PKeyName,(String) methodGet.invoke(instance)).findFirst();
if(pinstance==null) {
pinstance = infoRealm.createObject(p, methodGet.invoke(instance));
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
Method methodGet = null;
if (type.equals("class java.lang.Boolean")) {
methodGet = instance.getClass().getMethod("is" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Boolean.class});
mothodSet.invoke(pinstance, (Boolean) methodGet.invoke(instance));
} else if (type.equals("boolean")) {
methodGet = instance.getClass().getMethod("is" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Boolean.TYPE});
mothodSet.invoke(pinstance, (Boolean) methodGet.invoke(instance));
} else if (type.equals("class java.lang.Integer")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Integer.class});
mothodSet.invoke(pinstance, (Integer) methodGet.invoke(instance));
} else if (type.equals("int")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Integer.TYPE});
mothodSet.invoke(pinstance, (Integer) methodGet.invoke(instance));
} else if (type.equals("class java.lang.Double")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Double.class});
mothodSet.invoke(pinstance, (Double) methodGet.invoke(instance));
} else if (type.equals("double")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Double.TYPE});
mothodSet.invoke(pinstance, (Double) methodGet.invoke(instance));
} else if (type.equals("class java.lang.String")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{String.class});
if (name.equals(PKeyName)) {
} else {
mothodSet.invoke(pinstance, (String) methodGet.invoke(instance));
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
infoRealm.copyToRealmOrUpdate(pinstance);
infoRealm.commitTransaction();
}
/**
* 保存一个arraylist到数据库
*
* @param arrayList 需要保存的数组
* @param s 数据源类型,即arraylist中存储数据的类型
* @param p 目标数据库类型
* @param <S>
* @param <P>
*/
public <S, P extends RealmObject> void saveArr(final ArrayList<S> arrayList, final Class<S> s, final Class<P> p,final String PKeyName) {
if(PKeyName==null)
return;
infoRealm.beginTransaction();
P pinstance = null;
for(int a=0;a<arrayList.size();a++) {
S instance=arrayList.get(a);
Field[] field = s.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
if(name.equals(PKeyName)) {
Method methodGet = instance.getClass().getMethod("get" + name);
pinstance=infoRealm.where(p).contains(PKeyName,(String) methodGet.invoke(instance)).findFirst();
if(pinstance==null) {
pinstance = infoRealm.createObject(p, methodGet.invoke(instance));
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
Method methodGet = null;
if (type.equals("class java.lang.Boolean")) {
methodGet = instance.getClass().getMethod("is" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Boolean.class});
mothodSet.invoke(pinstance, (Boolean) methodGet.invoke(instance));
} else if (type.equals("boolean")) {
methodGet = instance.getClass().getMethod("is" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Boolean.TYPE});
mothodSet.invoke(pinstance, (Boolean) methodGet.invoke(instance));
} else if (type.equals("class java.lang.Integer")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Integer.class});
mothodSet.invoke(pinstance, (Integer) methodGet.invoke(instance));
} else if (type.equals("int")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Integer.TYPE});
mothodSet.invoke(pinstance, (Integer) methodGet.invoke(instance));
} else if (type.equals("class java.lang.Double")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Double.class});
mothodSet.invoke(pinstance, (Double) methodGet.invoke(instance));
} else if (type.equals("double")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{Double.TYPE});
mothodSet.invoke(pinstance, (Double) methodGet.invoke(instance));
} else if (type.equals("class java.lang.String")) {
methodGet = instance.getClass().getMethod("get" + name);
Method mothodSet = pinstance.getClass().getMethod("set" + name, new Class[]{String.class});
if (name.equals(PKeyName)) {
} else {
mothodSet.invoke(pinstance, (String) methodGet.invoke(instance));
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
infoRealm.copyToRealmOrUpdate(pinstance);
}
infoRealm.commitTransaction();
}
public <S extends RealmObject>void deleteAll(final Class<S> s){
infoRealm.beginTransaction();
infoRealm.where(s).findAll().deleteAllFromRealm();
infoRealm.commitTransaction();
}
/**
* 查询 指定对象M类型的数据, 实际返回的为ArrayList<M>类型。
*
* @param s 数据源即从realm中查询出的数据类型,是RealmObject子类
* @param m 模板类型。
* @param <M>
* @param <S>
* @return
*/
public <M, S extends RealmObject> ArrayList<M> findAll(final Class<S> s, final Class<M> m) {
Field[] field = m.getDeclaredFields();
RealmResults<S> results =
infoRealm.where(s).findAll();
ArrayList<M> list = new ArrayList<>();
String str = "";
for (S sourse : results) {
M minstance = null;
try {
minstance = m.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method methodGet = null;
Method methodSet = null;
if (type.equals("class java.lang.Boolean")) {
methodGet = s.getMethod("is" + name);
methodSet = m.getMethod("set" + name, new Class[]{Boolean.class});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("boolean")) {
methodGet = s.getMethod("is" + name);
methodSet = m.getMethod("set" + name, new Class[]{Boolean.TYPE});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("class java.lang.Integer")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Integer.class});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("int")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Integer.TYPE});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("class java.lang.Double")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Double.class});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("double")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Double.TYPE});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("class java.lang.Long")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Long.class});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("long")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{Long.TYPE});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
} else if (type.equals("class java.lang.String")) {
methodGet = s.getMethod("get" + name);
methodSet = m.getMethod("set" + name, new Class[]{String.class});
methodSet.invoke(minstance, methodGet.invoke(sourse));
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
list.add(minstance);
}
Log.e("tag", "logend");
return list;
}
/**
* 用于测试,输出数据库中指定目标类型的数据
*
* @param s 数据来源,指从数据库中查询出的对象类型
* @param m 数据模板,指fastjson解析的实体类的类型
* @param <M>
* @param <S>
* @return
*/
public <M, S extends RealmObject> void log(final Class<S> s, final Class<M> m) {
Field[] field = m.getDeclaredFields();
RealmResults<S> results =
infoRealm.where(s).findAll();
String str = "";
for (S sourse : results) {
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method methodGet = null;
if (type.equals("class java.lang.Boolean") || type.equals("boolean")) {
methodGet = s.getMethod("is" + name);
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
str += methodGet.getName() + ":" + methodGet.invoke(sourse) + ",";
} else if (type.equals("class java.lang.Integer") || type.equals("int")) {
methodGet = s.getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
str += methodGet.getName() + ":" + methodGet.invoke(sourse) + ",";
} else if (type.equals("class java.lang.Double") || type.equals("double")) {
methodGet = s.getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
str += methodGet.getName() + ":" + methodGet.invoke(sourse) + ",";
} else if (type.equals("class java.lang.String")) {
methodGet = s.getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(sourse) + "");
str += methodGet.getName() + ":" + methodGet.invoke(sourse) + ",";
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
Log.e("tag", "logend");
}
/**
* 对象转存相同类型的对象之间copy数据
*
* @param obj
* @param t
* @param obj2
* @param <T>
*/
public <T> void saveObj(Object obj, final Class<T> t, Object obj2) {
T instance = (T) obj2;
T sourceInstance = (T) obj;
Field[] field = instance.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method methodGet = null;
if (type.equals("class java.lang.Boolean")) {
methodGet = sourceInstance.getClass().getMethod("is" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Boolean.class});
mothodSet.invoke(instance, (Boolean) methodGet.invoke(sourceInstance));
} else if (type.equals("boolean")) {
methodGet = sourceInstance.getClass().getMethod("is" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Boolean.TYPE});
mothodSet.invoke(instance, (Boolean) methodGet.invoke(sourceInstance));
} else if (type.equals("class java.lang.Integer")) {
methodGet = sourceInstance.getClass().getMethod("get" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Integer.class});
mothodSet.invoke(instance, (Integer) methodGet.invoke(sourceInstance));
} else if (type.equals("int")) {
methodGet = sourceInstance.getClass().getMethod("get" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Integer.TYPE});
mothodSet.invoke(instance, (Integer) methodGet.invoke(sourceInstance));
} else if (type.equals("class java.lang.Double")) {
methodGet = sourceInstance.getClass().getMethod("get" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Double.class});
mothodSet.invoke(instance, (Double) methodGet.invoke(sourceInstance));
} else if (type.equals("double")) {
methodGet = sourceInstance.getClass().getMethod("get" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{Double.TYPE});
mothodSet.invoke(instance, (Double) methodGet.invoke(sourceInstance));
} else if (type.equals("class java.lang.String")) {
methodGet = sourceInstance.getClass().getMethod("get" + name);
Method mothodSet = instance.getClass().getMethod("set" + name, new Class[]{String.class});
mothodSet.invoke(instance, (String) methodGet.invoke(sourceInstance));
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
/**
* 遍历指定类型数据
*
* @param obj
* @param t
* @param <T>
*/
public <T> void logObj(Object obj, final Class<T> t) {
Field[] field = obj.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String type = field[i].getGenericType().toString();
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
try {
Method methodGet = null;
if (type.equals("class java.lang.Boolean") || type.equals("boolean")) {
methodGet = obj.getClass().getMethod("is" + name);
Log.e(methodGet.getName(), methodGet.invoke(obj) + "");
} else if (type.equals("class java.lang.Integer") || type.equals("int")) {
methodGet = obj.getClass().getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(obj) + "");
} else if (type.equals("class java.lang.Double") || type.equals("double")) {
methodGet = obj.getClass().getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(obj) + "");
} else if (type.equals("class java.lang.String")) {
methodGet = obj.getClass().getMethod("get" + name);
Log.e(methodGet.getName(), methodGet.invoke(obj) + "");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}