Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package ist.meic.pa.GenericFunctions;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import ist.meic.pa.GenericFunctions.structure.TypeNode;
  9.  
  10. public class MethodCache {
  11.  
  12.     private HashMap<String, Method> methodsMap;
  13.     private HashMap<String, Method[]> beforeMethodsMap;
  14.     private HashMap<String, Method[]> afterMethodsMap;
  15.     private List<String> cachedArgs;
  16.  
  17.     public MethodCache() {
  18.         this.methodsMap = new HashMap<>();
  19.         this.beforeMethodsMap = new HashMap<>();
  20.         this.afterMethodsMap = new HashMap<>();
  21.         this.cachedArgs = new ArrayList<>();
  22.     }
  23.  
  24.     public Method getMethod(Class[] classes) {
  25.  
  26.  
  27.         String key = getKey(classes);
  28.         return methodsMap.get(key);
  29.  
  30.     }
  31.  
  32.     public void cacheMethod(Class[] classes, Method method) {
  33.  
  34.         String key = getKey(classes);
  35.         methodsMap.put(key, method);
  36.         this.registerKey(key);
  37.     }
  38.  
  39.     public Method[] getAfterMethods(Class[] classes) {
  40.  
  41.  
  42.         String key = getKey(classes);
  43.         return afterMethodsMap.get(key);
  44.  
  45.     }
  46.  
  47.     public void cacheAfterMethods(Class[] classes, Method[] methods) {
  48.  
  49.         String key = getKey(classes);
  50.         afterMethodsMap.put(key, methods);
  51.         this.registerKey(key);
  52.     }
  53.  
  54.     public Method[] getBeforeMethods(Class[] classes) {
  55.  
  56.  
  57.         String key = getKey(classes);
  58.         return beforeMethodsMap.get(key);
  59.  
  60.     }
  61.  
  62.     public void cacheBeforeMethods(Class[] classes, Method[] methods) {
  63.  
  64.         String key = getKey(classes);
  65.         beforeMethodsMap.put(key, methods);
  66.         this.registerKey(key);
  67.  
  68.     }
  69.  
  70.     public boolean isCached(Class[] classes) {
  71.         String key = getKey(classes);
  72.  
  73.         return this.cachedArgs.contains(key);
  74.     }
  75.  
  76.     private void registerKey(String key) {
  77.         if (!cachedArgs.contains(key)) {
  78.             cachedArgs.add(key);
  79.         }
  80.     }
  81.  
  82.  
  83.     private String getKey(Class[] classes) {
  84.         String key = "";
  85.  
  86.         for (Class c : classes) {
  87.             key += c.getName();
  88.         }
  89.  
  90.         return key;
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement