public void generateClass(ClassVisitor v) { switch (style) { case STYLE_INTERFACES: new MixinEmitter(v, getClassName(), classes, route); break; case STYLE_BEANS: new MixinBeanEmitter(v, getClassName(), classes); break; case STYLE_EVERYTHING: new MixinEverythingEmitter(v, getClassName(), classes); break; } }
/** * 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 }); }
/** * 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 }); }
public static Class getProxyClass(ClassLoader loader, Class[] interfaces) { Enhancer e = new Enhancer(); e.setSuperclass(ProxyImpl.class); e.setInterfaces(interfaces); e.setCallbackTypes(new Class[]{ InvocationHandler.class, NoOp.class, }); e.setCallbackFilter(BAD_OBJECT_METHOD_FILTER); e.setUseFactory(false); return e.createClass(); }
/** * 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(); }
/** * Helper method to create an interface mixin. For finer control over the * generated instance, use a new instance of <code>Mixin</code> * instead of this static method. * TODO */ public static Mixin create(Class[] interfaces, Object[] delegates) { Generator gen = new Generator(); gen.setClasses(interfaces); gen.setDelegates(delegates); return gen.create(); }
/** * Helper method to create an interface mixin. For finer control over the * generated instance, use a new instance of <code>Mixin</code> * instead of this static method. * TODO */ public static Mixin create(Object[] delegates) { Generator gen = new Generator(); gen.setDelegates(delegates); return gen.create(); }
public MethodProxy getMethodProxy() { if (methodProxy == null) methodProxy = MethodProxy.create(c1, c2, desc, name, superName); return methodProxy; } }
/** * 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(); }
public static Type[] determineTypes(Callback[] callbacks) { Type[] types = new Type[callbacks.length]; for (int i = 0; i < types.length; i++) { types[i] = determineType(callbacks[i]); } return types; }
public static CallbackGenerator[] getGenerators(Type[] callbackTypes) { CallbackGenerator[] generators = new CallbackGenerator[callbackTypes.length]; for (int i = 0; i < generators.length; i++) { generators[i] = getGenerator(callbackTypes[i]); } return generators; }
protected Object firstInstance(Class type) throws Exception { if (classOnly) { return type; } else { return createUsingReflection(type); } }
/** * 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; } }
protected Object nextInstance(Object instance) { return ((Mixin)instance).newInstance(delegates); } }
public ClassLoader getClassLoader() { return Enhancer.this.getClassLoader(); } public int getOriginalModifiers(MethodInfo method) {
/** * 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); }
/** * Return the {@link org.mockito.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; }
public static Mixin createBean(Object[] beans) { return createBean(null, beans); } /**
/** * 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(); }
public static Type[] determineTypes(Class[] callbackTypes) { Type[] types = new Type[callbackTypes.length]; for (int i = 0; i < types.length; i++) { types[i] = determineType(callbackTypes[i]); } return types; }