Java 类groovy.lang.DelegatingMetaClass 实例源码

项目:spring4-understanding    文件:GroovyScriptFactoryTests.java   
@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);
}
项目:class-guard    文件:GroovyScriptFactoryTests.java   
@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);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * 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);
    }
}
项目:groovy    文件:ProxyGenerator.java   
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);
}