01: package org.drools.base;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: /**
07: * As class field Extractors have some cost to generate
08: * (inspecting the class, and generating classes via ASM)
09: * it makes sense to cache them.
10: * This is that cache.
11: *
12: * @author Michael Neale
13: *
14: */
15: public class ClassFieldExtractorCache {
16: private static ClassFieldExtractorCache instance = new ClassFieldExtractorCache();
17: private static Map cache;
18:
19: public static ClassFieldExtractorCache getInstance() {
20: return instance;
21: }
22:
23: public synchronized static ClassFieldExtractor getExtractor(
24: final Class clazz, final String fieldName,
25: ClassLoader classLoader) {
26: if (cache == null) {
27: cache = new HashMap();
28: }
29:
30: final String key = clazz.getName() + "|" + fieldName;
31:
32: if (cache.containsKey(key)) {
33: return (ClassFieldExtractor) cache.get(key);
34: } else {
35: final ClassFieldExtractor ex = new ClassFieldExtractor(
36: clazz, fieldName, classLoader);
37: cache.put(key, ex);
38: return ex;
39: }
40: }
41:
42: }
|