1 package groovy.mock.interceptor; 2 3 import groovy.lang.ProxyMetaClass; 4 import groovy.lang.MetaClassRegistry; 5 import groovy.lang.MetaClass; 6 7 import java.beans.IntrospectionException; 8 9 import org.codehaus.groovy.runtime.InvokerHelper; 10 11 /*** 12 * The ProxyMetaClass for the MockInterceptor. 13 * Instance and class methods are intercepted, but constructors are not to allow mocking of aggregated objects. 14 * @author Dierk Koenig 15 */ 16 17 public class MockProxyMetaClass extends ProxyMetaClass { 18 19 /*** 20 * @param adaptee the MetaClass to decorate with interceptability 21 */ 22 public MockProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) throws IntrospectionException { 23 super(registry, theClass, adaptee); 24 } 25 26 /*** 27 * convenience factory method for the most usual case. 28 */ 29 public static MockProxyMetaClass make(Class theClass) throws IntrospectionException { 30 MetaClassRegistry metaRegistry = InvokerHelper.getInstance().getMetaRegistry(); 31 MetaClass meta = metaRegistry.getMetaClass(theClass); 32 return new MockProxyMetaClass(metaRegistry, theClass, meta); 33 } 34 35 36 public Object invokeMethod(final Object object, final String methodName, final Object[] arguments) { 37 if (null == interceptor) { 38 throw new RuntimeException("cannot invoke without interceptor"); 39 } 40 return interceptor.beforeInvoke(object, methodName, arguments); 41 } 42 43 public Object invokeStaticMethod(final Object object, final String methodName, final Object[] arguments) { 44 if (null == interceptor) { 45 throw new RuntimeException("cannot invoke without interceptor"); 46 } 47 return interceptor.beforeInvoke(object, methodName, arguments); 48 } 49 50 /*** 51 * Unlike general impl in superclass, ctors are not intercepted but relayed 52 */ 53 public Object invokeConstructor(final Object[] arguments) { 54 return adaptee.invokeConstructor(arguments); 55 } 56 57 /*** 58 * Unlike general impl in superclass, ctors are not intercepted but relayed 59 */ 60 public Object invokeConstructorAt(final Class at, final Object[] arguments) { 61 return adaptee.invokeConstructorAt(at, arguments); 62 } 63 64 }