@Override public void customize(GroovyObject goo) { DelegatingMetaClass dmc = new DelegatingMetaClass(goo.getMetaClass()) { @Override public Object invokeMethod(Object arg0, String mName, Object[] arg2) { if (mName.contains("Missing")) { throw new IllegalStateException("Gotcha"); } else { return super.invokeMethod(arg0, mName, arg2); } } }; dmc.initialize(); goo.setMetaClass(dmc); }
/** * Adds a "metaClass" property to all class objects so you can use the syntax * <code>String.metaClass.myMethod = { println "foo" }</code> * * @param c The java.lang.Class instance * @return An MetaClass instance * @since 1.5.0 */ public static MetaClass getMetaClass(Class c) { MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry(); MetaClass mc = metaClassRegistry.getMetaClass(c); if (mc instanceof ExpandoMetaClass || mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee() instanceof ExpandoMetaClass) return mc; else { return new HandleMetaClass(mc); } }
private static void setMetaClass(final MetaClass metaClass) { final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) { @Override public Object invokeStaticMethod(Object object, String methodName, Object[] arguments) { return InvokerHelper.invokeMethod(INSTANCE, methodName, arguments); } }; GroovySystem.getMetaClassRegistry().setMetaClass(ProxyGenerator.class, newMetaClass); }