/** * Return the {@link org.springframework.cglib.reflect.FastClass} method index * for the method used by {@link #invokeSuper}. This index uniquely * identifies the method within the generated proxy, and therefore * can be useful to reference external metadata. * @see #getSuperName */ public int getSuperIndex() { init(); return fastClassInfo.i2; }
/** * Generate a new class if necessary and return it without creating a new instance. * This ignores any callbacks that have been set. * To create a new instance you will have to use reflection, and methods * called during the constructor will not be intercepted. To avoid this problem, * use the multi-arg <code>create</code> method. * @see #create(Class[], Object[]) */ public Class createClass() { classOnly = true; return (Class) createHelper(); }
/** * Similar to {@link #registerCallbacks}, but suitable for use * when multiple threads will be creating instances of the generated class. * The thread-level callbacks will always override the static callbacks. * Static callbacks are never cleared. * @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 */ public static void registerStaticCallbacks(Class generatedClass, Callback[] callbacks) { setCallbacksHelper(generatedClass, callbacks, SET_STATIC_CALLBACKS_NAME); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param callback the callback to use for all methods */ public static Object create(Class type, Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(type); e.setCallback(callback); return e.create(); }
/** * Creates the CGLIB {@link Enhancer}. Subclasses may wish to override this to return a custom * {@link Enhancer} implementation. */ protected Enhancer createEnhancer() { return new Enhancer(); }
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) { enhancer.setInterceptDuringConstruction(false); enhancer.setCallbacks(callbacks); return (this.constructorArgs != null && this.constructorArgTypes != null ? enhancer.create(this.constructorArgTypes, this.constructorArgs) : enhancer.create()); }
/** * Uses enhancer to generate a subclass of superclass, * ensuring that callbacks are registered for the new subclass. */ private Class<?> createClass(Enhancer enhancer) { Class<?> subclass = enhancer.createClass(); // Registering callbacks statically (as opposed to thread-local) // is critical for usage in an OSGi environment (SPR-5932)... Enhancer.registerStaticCallbacks(subclass, CALLBACKS); return subclass; }
/** * Set the single type of {@link Callback} to use. * This may be used instead of {@link #setCallback} when calling * {@link #createClass}, since it may not be possible to have * an array of actual callback instances. * @param callbackType the type of callback to use for all methods * @see #setCallbackTypes */ public void setCallbackType(Class callbackType) { setCallbackTypes(new Class[]{callbackType}); }
/** * Set the single {@link Callback} to use. * Ignored if you use {@link #createClass}. * @param callback the callback to use for all methods * @see #setCallbacks */ public void setCallback(final Callback callback) { setCallbacks(new Callback[]{callback}); }
/** * For internal use by {@link Enhancer} only; see the {@link org.springframework.cglib.reflect.FastMethod} class * for similar functionality. */ public static MethodProxy create(Class c1, Class c2, String desc, String name1, String name2) { MethodProxy proxy = new MethodProxy(); proxy.sig1 = new Signature(name1, desc); proxy.sig2 = new Signature(name2, desc); proxy.createInfo = new CreateInfo(c1, c2); return proxy; }
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); }
/** * Determine if a class was generated using <code>Enhancer</code>. * @param type any class * @return whether the class was generated using <code>Enhancer</code> */ public static boolean isEnhanced(Class type) { try { getCallbacksSetter(type, SET_THREAD_CALLBACKS_NAME); return true; } catch (NoSuchMethodException e) { return false; } }
public ClassLoader getClassLoader() { return Enhancer.this.getClassLoader(); }
/** * Finds all of the methods that will be extended by an * Enhancer-generated class using the specified superclass and * interfaces. This can be useful in building a list of Callback * objects. The methods are added to the end of the given list. Due * to the subclassing nature of the classes generated by Enhancer, * the methods are guaranteed to be non-static, non-final, and * non-private. Each method signature will only occur once, even if * it occurs in multiple classes. * @param superclass the class that will be extended, or null * @param interfaces the list of interfaces that will be implemented, or null * @param methods the list into which to copy the applicable methods */ public static void getMethods(Class superclass, Class[] interfaces, List methods) { getMethods(superclass, interfaces, methods, null, null); }
/** * This method should not be called in regular flow. * Technically speaking {@link #wrapCachedClass(Class)} uses {@link EnhancerFactoryData} as a cache value, * and the latter enables faster instantiation than plain old reflection lookup and invoke. * This method is left intact for backward compatibility reasons: just in case it was ever used. * @param type class to instantiate * @return newly created proxy instance * @throws Exception if something goes wrong */ protected Object firstInstance(Class type) throws Exception { if (classOnly) { return type; } else { return createUsingReflection(type); } }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param superclass class to extend or interface to implement * @param interfaces array of interfaces to implement, or null * @param callback the callback to use for all methods */ public static Object create(Class superclass, Class interfaces[], Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(superclass); e.setInterfaces(interfaces); e.setCallback(callback); return e.create(); }
FastClass getSuperFastClass() { init(); return fastClassInfo.f2; }
/** * Generate a new class if necessary and uses the specified * callbacks (if any) to create a new object instance. * Uses the no-arg constructor of the superclass. * @return a new instance */ public Object create() { classOnly = false; argumentTypes = null; return createHelper(); }
private static void setThreadCallbacks(Class type, Callback[] callbacks) { setCallbacksHelper(type, callbacks, SET_THREAD_CALLBACKS_NAME); }
FastClass getFastClass() { init(); return fastClassInfo.f1; }