Generates 'Proxy' objects which implement interfaces, maps of closures and/or extend classes/delegates.
| 49 | * extend classes/delegates. |
| 50 | */ |
| 51 | @SuppressWarnings({"rawtypes", "serial"}) |
| 52 | public class ProxyGenerator { |
| 53 | private static final Map<Object,Object> EMPTY_CLOSURE_MAP = Collections.emptyMap(); |
| 54 | |
| 55 | static { |
| 56 | // wrap the standard MetaClass with the delegate |
| 57 | setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(ProxyGenerator.class)); |
| 58 | } |
| 59 | |
| 60 | /** Shared proxy generator instance used by Groovy's coercion helpers. */ |
| 61 | public static final ProxyGenerator INSTANCE = new ProxyGenerator(); |
| 62 | |
| 63 | /** |
| 64 | * Caches proxy classes. When, for example, a call like: <code>map as MyClass</code> |
| 65 | * is found, then a lookup is made into the cache to find if a suitable adapter |
| 66 | * exists already. If so, it is reused instead of generating a new one. |
| 67 | */ |
| 68 | private final Map<CacheKey, ProxyGeneratorAdapter> adapterCache; |
| 69 | private static final int CACHE_SIZE = Integer.getInteger("groovy.adapter.cache.default.size", 64); |
| 70 | private static final float LOAD_FACTOR = 0.75f; |
| 71 | private static final int INIT_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) + 1; |
| 72 | { |
| 73 | adapterCache = new LinkedHashMap<>(INIT_CAPACITY, LOAD_FACTOR, true) { |
| 74 | /** {@inheritDoc} */ |
| 75 | @Override |
| 76 | protected boolean removeEldestEntry(Map.Entry<CacheKey, ProxyGeneratorAdapter> entry) { |
| 77 | return size() > CACHE_SIZE; |
| 78 | } |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | private boolean debug; |
| 83 | private boolean emptyMethods; |
| 84 | private ClassLoader override; |
| 85 | |
| 86 | // private ProxyGenerator() {} // TODO: Should we make ProxyGenerator singleton? |
| 87 | |
| 88 | /** |
| 89 | * Reports whether generated source is echoed for debugging. |
| 90 | * |
| 91 | * @return {@code true} when debug output is enabled |
| 92 | */ |
| 93 | public boolean getDebug() { |
| 94 | return debug; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Instructs <code>ProxyGenerator</code> to dump generated Groovy |
| 99 | * source code to standard output during construction. This is useful |
| 100 | * for debugging purposes but should be turned off in production. |
| 101 | * |
| 102 | * @param debug true if you want generated source to be printed |
| 103 | */ |
| 104 | public void setDebug(boolean debug) { |
| 105 | this.debug = debug; |
| 106 | } |
| 107 | |
| 108 | /** |
nothing calls this directly
no test coverage detected
searching dependent graphs…