Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.kubatatami.judonetworking.observers;
  
  import  android.content.Context;
  import  android.support.v4.util.LruCache;
  import  dalvik.system.DexFile;
  
 import java.util.List;

Created by Kuba on 16/02/14.
 
 public class ObservableCache {
     protected final static LruCache<Class<?>, List<DataObserverMethod>> dataObserverMethodsCache = new LruCache<Class<?>, List<DataObserverMethod>>(100);
     protected final static LruCache<Class<?>,  LruCache<String,Field>> fieldsCache = new LruCache<Class<?>, LruCache<String,Field>>(100);
 
     protected static class PreloadRunnable implements Runnable {
 
         Context context;
         String packageName;
 
         public PreloadRunnable(Context contextString packageName) {
             this. = context;
             this. = packageName;
         }
 
         @Override
         public void run() {
             try {
                 android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
                 DexFile df = new DexFile(.getPackageCodePath());
                 for (Enumeration<Stringiter = df.entries(); iter.hasMoreElements(); ) {
                     String className = iter.nextElement();
                     if (className.contains()) {
                         Class<?> clazz = Class.forName(className);
                         .put(clazzgetMethods(clazz));
                     }
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }
 
     protected static Field getDataObserverField(Method methodDataObserver ann) {
 
 
         if (!ann.fieldName().equals("")) {
             return getField(ann.fieldName());
         }
 
         String methodName = method.getName();
         if (methodName.length() > ..length() + 1) {
             String fieldName = methodName.substring(0, methodName.length() - ..length());
 
             return getField(fieldName);
 
         }
         return null;
     }
 
     protected static List<DataObserverMethodgetMethods(Class<?> clazz) {
         List<DataObserverMethodmethodList = new ArrayList<DataObserverMethod>();
         for (final Method method : clazz.getMethods()) {
             DataObserver ann = method.getAnnotation(DataObserver.class);
             if (ann != null) {
                 Field field = getDataObserverField(methodann);
                 if (field != null) {
                     methodList.add(new DataObserverMethod(methodfieldann));
                 }
             }
         }
         return methodList;
     }
 
     public static List<DataObserverMethodgetDataObserverMethods(Class<?> clazz) {
         List<DataObserverMethodmethods = .get(clazz);
         if (methods == null) {
             methods = getMethods(clazz);
             .put(clazzmethods);
         }
         return methods;
     }
 
     public static void clearCache() {
         .evictAll();
     }
 
     public static void preLoad(Context contextString packageName) {
         Thread thread = new Thread(new PreloadRunnable(contextpackageName));
         thread.start();
     }
 
     public static class DataObserverMethod {
         public Method method;
         public Field field;
        public DataObserver dataObserver;
        public DataObserverMethod(Method methodField fieldDataObserver dataObserver) {
            this. = method;
            this. = field;
            this. = dataObserver;
        }
    }
    protected static Field getField(String fieldName) {
        return getField(fieldName.);
    }
    protected static Field getField(String fieldNameClass<?> objectClass) {
        LruCache<String,Fieldfields = .get(objectClass);
        if(fields==null){
            fields=new LruCache<StringField>(10);
            .put(objectClass,fields);
        }
        Field field=fields.get(fieldName);
        if(field==null){
            field=getFieldImplementation(fieldNameobjectClass);
            fields.put(fieldName,field);
        }
        return field;
    }
    protected static Field getFieldImplementation(String fieldNameClass<?> objectClass) {
        Field field = null;
        while (objectClass != null && field == null) {
            try {
                field = objectClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                objectClass = objectClass.getSuperclass();
            }
        }
        if (field != null) {
            field.setAccessible(true);
        } else {
            throw new RuntimeException(new NoSuchFieldException(fieldName));
        }
        return field;
    }
New to GrepCode? Check out our FAQ X