zoukankan      html  css  js  c++  java
  • 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();
        }
    
    }
    

      

  • 相关阅读:
    Linux 实用指令(5)--组管理和权限管理
    Linux 实用指令(4)
    Linux用户管理 (3)
    Oracle如何用单字段或多字段进行查重
    SQL中的左连接与右连接,内连接有什么不同
    Markdown语法--整理
    应用程序无法正常启动0xc000007b
    Linux开机、重启和用户登录注销(2)
    Linux vi和vim编辑器(1)
    SecureCRT 64位 破解版和安装,以及解决乱码问题
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/10560683.html
Copyright © 2011-2022 走看看