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

The following examples show how to use groovy.lang.GroovyObject#getMetaClass() . 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: AbstractCallSite.java    From groovy with Apache License 2.0 6 votes vote down vote up
private CallSite createPogoMetaClassGetPropertySite(final GroovyObject receiver) {
    MetaClass metaClass = receiver.getMetaClass();

    CallSite site;
    if (metaClass.getClass() != MetaClassImpl.class || GroovyCategorySupport.hasCategoryInCurrentThread()) {
        site = new PogoMetaClassGetPropertySite(this, metaClass);
    } else {
        final MetaProperty effective = ((MetaClassImpl) metaClass).getEffectiveGetMetaProperty(this.array.owner, receiver, name, false);
        if (effective != null) {
            if (effective instanceof CachedField)
                site = new GetEffectivePogoFieldSite(this, metaClass, (CachedField) effective);
            else
                site = new GetEffectivePogoPropertySite(this, metaClass, effective);
        } else {
            site = new PogoMetaClassGetPropertySite(this, metaClass);
        }
    }

    array.array[index] = site;
    return site;
}
 
Example 5
Source File: CallSiteArray.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static CallSite createCallCurrentSite(CallSite callSite, GroovyObject receiver, Object[] args, Class sender) {
    CallSite site;
    if (receiver instanceof GroovyInterceptable) {
        site = new PogoInterceptableSite(callSite);
    } else {
        MetaClass metaClass = receiver.getMetaClass();
        Class theClass = metaClass.getTheClass();
        if (receiver.getClass() != theClass && !theClass.isInterface()) {
            site = new PogoInterceptableSite(callSite);
        } else if (metaClass instanceof MetaClassImpl) {
            site = ((MetaClassImpl) metaClass).createPogoCallCurrentSite(callSite, sender, args);
        } else {
            site = new PogoMetaClassSite(callSite, metaClass);
        }
    }

    replaceCallSite(callSite, site);
    return site;
}
 
Example 6
Source File: ScriptBytecodeAdapter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Object invokeMethodOnSuperN(Class senderClass, GroovyObject receiver, String messageName, Object[] messageArguments) throws Throwable {
    MetaClass metaClass = receiver.getMetaClass();
    // ignore interception and missing method fallback
    Object result = null;
    try {
        result = metaClass.invokeMethod(senderClass, receiver, messageName, messageArguments, true, true);
    } catch (GroovyRuntimeException gre) {
        throw unwrap(gre);
    }
    return result;
}
 
Example 7
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
private MetaClass lookupObjectMetaClass(Object object) {
    if (object instanceof GroovyObject) {
        GroovyObject go = (GroovyObject) object;
        return go.getMetaClass();
    }
    Class ownerClass = object.getClass();
    if (ownerClass == Class.class) {
        ownerClass = (Class) object;
        return registry.getMetaClass(ownerClass);
    }
    MetaClass metaClass = InvokerHelper.getMetaClass(object);
    return metaClass;
}
 
Example 8
Source File: GroovyClassLoaderTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testCompile() throws Exception {
    Class groovyClass = loader.parseClass(new File("src/test/org/codehaus/groovy/classgen/Main.groovy"));

    System.out.println("Invoking main...");

    GroovyObject object = (GroovyObject) groovyClass.getDeclaredConstructor().newInstance();

    assertTrue(object != null);

    MetaClass metaClass = object.getMetaClass();
    System.out.println("Metaclass: " + metaClass);

    Class type = object.getClass();
    System.out.println("Type: " + type);

    // invoke via metaclass
    metaClass.invokeMethod(object, "main", null);

    // invoke directly
    object.invokeMethod("main", null);
}