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();
        }
    
    }
    

      

  • 相关阅读:
    Linux2.6X内核中文件相关结构体总结
    Linux 内核文件系统与设备操作流程分析
    在linux下删除的共享文件怎么恢复
    fedora17的U盘安装和硬盘安装
    cakephp
    【25.00%】【vijos P1907】飞扬的小鸟
    【14.36%】【codeforces 614C】Peter and Snow Blower
    【14.67%】【codeforces 615D】Multipliers
    【records】10.24..10.30
    【非常高%】【codeforces 733A】Grasshopper And the String
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/10560683.html
Copyright © 2011-2022 走看看