@Override protected Object wrapCachedClass(Class klass) { Class[] argumentTypes = this.argumentTypes; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; } EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly); Field factoryDataField = null; try { // The subsequent dance is performed just once for each class, // so it does not matter much how fast it goes factoryDataField = klass.getField(FACTORY_DATA_FIELD); factoryDataField.set(null, factoryData); Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD); callbackFilterField.setAccessible(true); callbackFilterField.set(null, this.filter); } catch (NoSuchFieldException e) { throw new CodeGenerationException(e); } catch (IllegalAccessException e) { throw new CodeGenerationException(e); } return new WeakReference<EnhancerFactoryData>(factoryData); }
/** * Creates proxy instance for given argument types, and assigns the callbacks. * Ideally, for each proxy class, just one set of argument types should be used, * otherwise it would have to spend time on constructor lookup. * Technically, it is a re-implementation of {@link Enhancer#createUsingReflection(Class)}, * with "cache {@link #setThreadCallbacks} and {@link #primaryConstructor}" * @param argumentTypes constructor argument types * @param arguments constructor arguments * @param callbacks callbacks to set for the new instance * @return newly created proxy * @see #createUsingReflection(Class) */ public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) { setThreadCallbacks(callbacks); try { // Explicit reference equality is added here just in case Arrays.equals does not have one if (primaryConstructorArgTypes == argumentTypes || Arrays.equals(primaryConstructorArgTypes, argumentTypes)) { // If we have relevant Constructor instance at hand, just call it // This skips "get constructors" machinery return ReflectUtils.newInstance(primaryConstructor, arguments); } // Take a slow path if observing unexpected argument types return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments); } finally { // clear thread callbacks to allow them to be gc'd setThreadCallbacks(null); } }
/** * Creates proxy instance for given argument types, and assigns the callbacks. * Ideally, for each proxy class, just one set of argument types should be used, * otherwise it would have to spend time on constructor lookup. * Technically, it is a re-implementation of {@link Enhancer#createUsingReflection(Class)}, * with "cache {@link #setThreadCallbacks} and {@link #primaryConstructor}" * @param argumentTypes constructor argument types * @param arguments constructor arguments * @param callbacks callbacks to set for the new instance * @return newly created proxy * @see #createUsingReflection(Class) */ public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) { setThreadCallbacks(callbacks); try { // Explicit reference equality is added here just in case Arrays.equals does not have one if (primaryConstructorArgTypes == argumentTypes || Arrays.equals(primaryConstructorArgTypes, argumentTypes)) { // If we have relevant Constructor instance at hand, just call it // This skips "get constructors" machinery return ReflectUtils.newInstance(primaryConstructor, arguments); } // Take a slow path if observing unexpected argument types return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments); } finally { // clear thread callbacks to allow them to be gc'd setThreadCallbacks(null); } }
@Override protected Object wrapCachedClass(Class klass) { Class[] argumentTypes = this.argumentTypes; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; } EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly); Field factoryDataField = null; try { // The subsequent dance is performed just once for each class, // so it does not matter much how fast it goes factoryDataField = klass.getField(FACTORY_DATA_FIELD); factoryDataField.set(null, factoryData); Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD); callbackFilterField.setAccessible(true); callbackFilterField.set(null, this.filter); } catch (NoSuchFieldException e) { throw new CodeGenerationException(e); } catch (IllegalAccessException e) { throw new CodeGenerationException(e); } return new WeakReference<EnhancerFactoryData>(factoryData); }
@Override protected Object wrapCachedClass(Class klass) { Class[] argumentTypes = this.argumentTypes; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; } EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly); Field factoryDataField = null; try { // The subsequent dance is performed just once for each class, // so it does not matter much how fast it goes factoryDataField = klass.getField(FACTORY_DATA_FIELD); factoryDataField.set(null, factoryData); Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD); callbackFilterField.setAccessible(true); callbackFilterField.set(null, this.filter); } catch (NoSuchFieldException e) { throw new CodeGenerationException(e); } catch (IllegalAccessException e) { throw new CodeGenerationException(e); } return new WeakReference<EnhancerFactoryData>(factoryData); }
protected Object nextInstance(Object instance) { EnhancerFactoryData data = (EnhancerFactoryData) instance; if (classOnly) { return data.generatedClass; } Class[] argumentTypes = this.argumentTypes; Object[] arguments = this.arguments; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; arguments = null; } return data.newInstance(argumentTypes, arguments, callbacks); }
/** * Creates proxy instance for given argument types, and assigns the callbacks. * Ideally, for each proxy class, just one set of argument types should be used, * otherwise it would have to spend time on constructor lookup. * Technically, it is a re-implementation of {@link Enhancer#createUsingReflection(Class)}, * with "cache {@link #setThreadCallbacks} and {@link #primaryConstructor}" * @param argumentTypes constructor argument types * @param arguments constructor arguments * @param callbacks callbacks to set for the new instance * @return newly created proxy * @see #createUsingReflection(Class) */ public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) { setThreadCallbacks(callbacks); try { // Explicit reference equality is added here just in case Arrays.equals does not have one if (primaryConstructorArgTypes == argumentTypes || Arrays.equals(primaryConstructorArgTypes, argumentTypes)) { // If we have relevant Constructor instance at hand, just call it // This skips "get constructors" machinery return ReflectUtils.newInstance(primaryConstructor, arguments); } // Take a slow path if observing unexpected argument types return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments); } finally { // clear thread callbacks to allow them to be gc'd setThreadCallbacks(null); } }
protected Object nextInstance(Object instance) { EnhancerFactoryData data = (EnhancerFactoryData) instance; if (classOnly) { return data.generatedClass; } Class[] argumentTypes = this.argumentTypes; Object[] arguments = this.arguments; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; arguments = null; } return data.newInstance(argumentTypes, arguments, callbacks); }
@Override protected Object wrapCachedClass(Class klass) { Class[] argumentTypes = this.argumentTypes; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; } EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly); Field factoryDataField = null; try { // The subsequent dance is performed just once for each class, // so it does not matter much how fast it goes factoryDataField = klass.getField(FACTORY_DATA_FIELD); factoryDataField.set(null, factoryData); Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD); callbackFilterField.setAccessible(true); callbackFilterField.set(null, this.filter); } catch (NoSuchFieldException e) { throw new CodeGenerationException(e); } catch (IllegalAccessException e) { throw new CodeGenerationException(e); } return new WeakReference<EnhancerFactoryData>(factoryData); }
protected Object nextInstance(Object instance) { EnhancerFactoryData data = (EnhancerFactoryData) instance; if (classOnly) { return data.generatedClass; } Class[] argumentTypes = this.argumentTypes; Object[] arguments = this.arguments; if (argumentTypes == null) { argumentTypes = Constants.EMPTY_CLASS_ARRAY; arguments = null; } return data.newInstance(argumentTypes, arguments, callbacks); }