Java Code Examples for groovy.lang.GroovyObject#setMetaClass()

The following examples show how to use groovy.lang.GroovyObject#setMetaClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: GrailsHibernateUtil.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the meta class is correct for a given class
 *
 * @param target The GroovyObject
 * @param persistentClass The persistent class
 */
@Deprecated
public static void ensureCorrectGroovyMetaClass(Object target, Class<?> persistentClass) {
    if (target instanceof GroovyObject) {
        GroovyObject go = ((GroovyObject)target);
        if (!go.getMetaClass().getTheClass().equals(persistentClass)) {
            go.setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(persistentClass));
        }
    }
}
 
Example 5
Source File: SimpleHibernateProxyHandler.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the meta class is correct for a given class
 *
 * @param target The GroovyObject
 * @param persistentClass The persistent class
 */
private static void ensureCorrectGroovyMetaClass(Object target, Class<?> persistentClass) {
    if (target instanceof GroovyObject) {
        GroovyObject go = ((GroovyObject)target);
        if (!go.getMetaClass().getTheClass().equals(persistentClass)) {
            go.setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(persistentClass));
        }
    }
}
 
Example 6
Source File: MockProxyMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Unlike general impl in superclass, ctors are not intercepted but relayed
 * unless interceptConstruction is set.
 */
public Object invokeConstructor(final Object[] arguments) {
    if (interceptConstruction && null == interceptor)
        throw new RuntimeException("cannot invoke constructor without interceptor");

    if (interceptConstruction) {
        GroovyObject newInstance = (GroovyObject) interceptor.beforeInvoke(null, getTheClass().getSimpleName(), arguments);
        newInstance.setMetaClass(this);
        return newInstance;
    }

    return adaptee.invokeConstructor(arguments);
}