/** * Call this method to register the {@link Callback} array to use before * creating a new instance of the generated class via reflection. If you are using * an instance of <code>Enhancer</code> or the {@link Factory} interface to create * new instances, this method is unnecessary. Its primary use is for when you want to * cache and reuse a generated class yourself, and the generated class does * <i>not</i> implement the {@link Factory} interface. * <p> * Note that this method only registers the callbacks on the current thread. * If you want to register callbacks for instances created by multiple threads, * use {@link #registerStaticCallbacks}. * <p> * The registered callbacks are overwritten and subsequently cleared * when calling any of the <code>create</code> methods (such as * {@link #create}), or any {@link Factory} <code>newInstance</code> method. * Otherwise they are <i>not</i> cleared, and you should be careful to set them * back to <code>null</code> after creating new instances via reflection if * memory leakage is a concern. * @param generatedClass a class previously created by {@link Enhancer} * @param callbacks the array of callbacks to use when instances of the generated * class are created * @see #setUseFactory */ public static void registerCallbacks(Class generatedClass, Callback[] callbacks) { setThreadCallbacks(generatedClass, callbacks); }
private Object createUsingReflection(Class type) { setThreadCallbacks(type, callbacks); try{ if (argumentTypes != null) { return ReflectUtils.newInstance(type, argumentTypes, arguments); } else { return ReflectUtils.newInstance(type); } }finally{ // clear thread callbacks to allow them to be gc'd setThreadCallbacks(type, null); } }
/** * Call this method to register the {@link Callback} array to use before * creating a new instance of the generated class via reflection. If you are using * an instance of <code>Enhancer</code> or the {@link Factory} interface to create * new instances, this method is unnecessary. Its primary use is for when you want to * cache and reuse a generated class yourself, and the generated class does * <i>not</i> implement the {@link Factory} interface. * <p> * Note that this method only registers the callbacks on the current thread. * If you want to register callbacks for instances created by multiple threads, * use {@link #registerStaticCallbacks}. * <p> * The registered callbacks are overwritten and subsequently cleared * when calling any of the <code>create</code> methods (such as * {@link #create}), or any {@link Factory} <code>newInstance</code> method. * Otherwise they are <i>not</i> cleared, and you should be careful to set them * back to <code>null</code> after creating new instances via reflection if * memory leakage is a concern. * @param generatedClass a class previously created by {@link Enhancer} * @param callbacks the array of callbacks to use when instances of the generated * class are created * @see #setUseFactory */ public static void registerCallbacks(Class generatedClass, Callback[] callbacks) { setThreadCallbacks(generatedClass, callbacks); }
private Object createUsingReflection(Class type) { setThreadCallbacks(type, callbacks); try{ if (argumentTypes != null) { return ReflectUtils.newInstance(type, argumentTypes, arguments); } else { return ReflectUtils.newInstance(type); } }finally{ // clear thread callbacks to allow them to be gc'd setThreadCallbacks(type, null); } }