SharedPreference映射Java类

时间:2023-03-09 07:55:58
SharedPreference映射Java类
package com.overlook.weagree.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.util.Log; import com.overlook.weagree.entity.CurrentUser; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map; public class CurrentUserUtil { private static final String TAG = "CurrentUserUtil"; private static final String CURRENT_USER = "current-user"; private static final Map<String,Method> SETTER = new HashMap<>(); private static final Map<String,Method> GETTER = new HashMap<>(); static {
if (SETTER.isEmpty() || GETTER.isEmpty()) {
Method[] methods = CurrentUser.class.getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName().toLowerCase(Locale.ENGLISH);
if (methodName.startsWith("set")) {
SETTER.put(methodName.substring(3),method);
}else{
GETTER.put(methodName.substring(3),method);
}
}
}
} public static void exit(Activity activity){
SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE);
if ( sharedPref == null ) {
return;
}
sharedPref.edit().clear().commit();
} public static CurrentUser getCurrentUser(Activity activity,Class mClass){ SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE);
if ( sharedPref == null ) {
return null;
}
CurrentUser currentUser = new CurrentUser(); Field[] fields = CurrentUser.class.getDeclaredFields(); for (Field field : fields) {
String fieldName = field.getName().toLowerCase(Locale.ENGLISH);;
Class type = field.getType();
Object value;
if (type.isAssignableFrom(Long.class)) {
value = sharedPref.getLong(fieldName,0L);
}else if (type.isAssignableFrom(Boolean.class)){
value = sharedPref.getBoolean(fieldName,false);
}else if (type.isAssignableFrom(Integer.class)) {
value = sharedPref.getInt(fieldName,0);
}else{
value = sharedPref.getString(fieldName,null);
} Method method = SETTER.get(fieldName); try {
if ( method == null ) {
Log.d(TAG, "getCurrentUser: "+fieldName);
}else{
method.invoke(currentUser,value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} return currentUser;
} public static Boolean putCurrentUser(Activity activity,CurrentUser currentUser){
SharedPreferences sharedPref = activity.getSharedPreferences(CURRENT_USER,Context.MODE_PRIVATE); Field[] fields = CurrentUser.class.getDeclaredFields(); SharedPreferences.Editor editor = sharedPref.edit(); for (Field field : fields) {
String fieldName = field.getName().toLowerCase(Locale.ENGLISH);
Class type = field.getType();
Method method = GETTER.get(fieldName); try {
Object value = method.invoke(currentUser); if ( value == null ) {
continue;
} if (type.isAssignableFrom(Long.class)) {
Long longVal = (Long) value;
if ( ! longVal.equals(0L)){
editor.putLong(fieldName,(Long)value);
}
}else if (type.isAssignableFrom(Boolean.class)){
editor.putBoolean(fieldName,(Boolean)value);
}else if (type.isAssignableFrom(Integer.class)) {
Integer intVal = (Integer)value;
if ( ! intVal.equals(0)){
editor.putInt(fieldName,(Integer)value);
}
}else{
editor.putString(fieldName,(String)value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return editor.commit();
} }