Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

100
LINES

< > BotCompany Repo | #1036538 // getOpt_cached (branch with scanning interfaces for fields)

JavaX fragment (include)

1  
//static final Map<Class, HashMap<S, Field>> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init);
2  
3  
sclass getOpt_Map extends WeakHashMap {
4  
  *() {
5  
    if (getOpt_special == null) getOpt_special = new HashMap;
6  
    clear();
7  
  }
8  
  
9  
  public void clear() {
10  
    super.clear();
11  
    //print("getOpt clear");
12  
    put(Class.class, getOpt_special);
13  
    put(S.class, getOpt_special);
14  
  }
15  
}
16  
17  
static final Map<Class, HashMap<S, Field>> getOpt_cache = 
18  
  ifdef AssumeNoClassUnloading
19  
    syncHashMap();
20  
  endifdef
21  
  ifndef AssumeNoClassUnloading
22  
    _registerDangerousWeakMap(synchroMap(new getOpt_Map));
23  
  endifndef
24  
  
25  
static HashMap getOpt_special; // just a marker
26  
27  
/*static void getOpt_special_init(Map map) {
28  
  map.put(Class.class, getOpt_special);
29  
  map.put(S.class, getOpt_special);
30  
}*/
31  
32  
static Map<S, Field> getOpt_getFieldMap(O o) {
33  
  Class c = _getClass(o);
34  
  HashMap<S, Field> map = getOpt_cache.get(c);
35  
  if (map == null)
36  
    map = getOpt_makeCache(c);
37  
  ret map;
38  
}
39  
40  
static Object getOpt_cached(Object o, String field) ctex {
41  
  if (o == null) ret null;
42  
43  
  Map<S, Field> map = getOpt_getFieldMap(o);
44  
45  
  if (map == getOpt_special) {
46  
    if (o instanceof Class)
47  
      return getOpt((Class) o, field);
48  
    /*if (o instanceof S)
49  
      ret getOpt(getBot((S) o), field);*/
50  
    if (o instanceof Map)
51  
      ret ((Map) o).get(field);
52  
  }
53  
    
54  
  Field f = map.get(field);
55  
  if (f != null) ret f.get(o);
56  
  ifclass DynamicObject
57  
    if (o instanceof DynamicObject)
58  
      ret syncMapGet2(((DynamicObject) o).fieldValues, field);
59  
  endif
60  
  ret null;
61  
}
62  
63  
// used internally - we are in synchronized block
64  
static HashMap<S, Field> getOpt_makeCache(Class c) {
65  
  HashMap<S, Field> map;
66  
  if (isSubtypeOf(c, Map.class))
67  
    map = getOpt_special;
68  
  else {
69  
    map = new HashMap;
70  
    new HashSet<Class> interfaces;
71  
    if (!reflection_classesNotToScan().contains(c.getName())) {
72  
      Class _c = c;
73  
      do {
74  
        for (Field f : _c.getDeclaredFields()) {
75  
          makeAccessible(f);
76  
          S name = f.getName();
77  
          if (!map.containsKey(name))
78  
            map.put(name, f);
79  
        }
80  
        for (intf : c.getInterfaces())
81  
          interfaces.add(intf);
82  
        _c = _c.getSuperclass();
83  
      } while (_c != null);
84  
      
85  
      // Scan interfaces for static final fields
86  
      
87  
      for (intf : interfaces) {
88  
        for (Field f : intf.getDeclaredFields()) {
89  
          makeAccessible(f);
90  
          S name = f.getName();
91  
          if (!map.containsKey(name))
92  
            map.put(name, f);
93  
        }
94  
      }
95  
    }
96  
  }
97  
  
98  
  if (getOpt_cache != null) getOpt_cache.put(c, map);
99  
  ret map;
100  
}

Author comment

Began life as a copy of #1005583

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036538
Snippet name: getOpt_cached (branch with scanning interfaces for fields)
Eternal ID of this version: #1036538/1
Text MD5: 52bdf71247db905c8999da3b789d70cf
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-31 19:44:48
Source code size: 2686 bytes / 100 lines
Pitched / IR pitched: No / No
Views / Downloads: 91 / 92
Referenced in: [show references]