Search in sources :

Example 16 with MetaClassImpl

use of groovy.lang.MetaClassImpl in project groovy-core by groovy.

the class CallSiteArray method createCallConstructorSite.

private static CallSite createCallConstructorSite(CallSite callSite, Class receiver, Object[] args) {
    MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
    CallSite site;
    if (metaClass instanceof MetaClassImpl) {
        site = ((MetaClassImpl) metaClass).createConstructorSite(callSite, args);
    } else
        site = new MetaClassConstructorSite(callSite, metaClass);
    replaceCallSite(callSite, site);
    return site;
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 17 with MetaClassImpl

use of groovy.lang.MetaClassImpl in project groovy-core by groovy.

the class Selector method getMetaClassImpl.

/**
 * Returns the MetaClassImpl if the given MetaClass is one of
 * MetaClassImpl, AdaptingMetaClass or ClosureMetaClass. If
 * none of these cases matches, this method returns null.
 */
private static MetaClassImpl getMetaClassImpl(MetaClass mc, boolean includeEMC) {
    Class mcc = mc.getClass();
    boolean valid = mcc == MetaClassImpl.class || mcc == AdaptingMetaClass.class || mcc == ClosureMetaClass.class || (includeEMC && mcc == ExpandoMetaClass.class);
    if (!valid) {
        if (LOG_ENABLED)
            LOG.info("meta class is neither MetaClassImpl, nor AdoptingMetaClass, nor ClosureMetaClass, normal method selection path disabled.");
        return null;
    }
    if (LOG_ENABLED)
        LOG.info("meta class is a recognized MetaClassImpl");
    return (MetaClassImpl) mc;
}
Also used : CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl) AdaptingMetaClass(groovy.lang.AdaptingMetaClass)

Example 18 with MetaClassImpl

use of groovy.lang.MetaClassImpl in project groovity by disney.

the class TestModel method testMeta.

@SuppressWarnings("rawtypes")
@Test
public void testMeta() throws Exception {
    ModelCheck mc = new ModelCheck();
    mc.setFoo("bar");
    Map mc1 = (Map) Model.mapObject(mc);
    Assert.assertEquals(1, mc1.size());
    Assert.assertEquals("bar", mc1.get("foo"));
    AtomicReference<String> abcref = new AtomicReference<String>(null);
    MetaClassImpl meta = new MetaClassImpl(ModelCheck.class);
    meta.initialize();
    MetaBeanProperty abc = new MetaBeanProperty("abc", String.class, new MetaMethod() {

        @Override
        public Object invoke(Object arg0, Object[] arg1) {
            return abcref.get();
        }

        @Override
        public Class getReturnType() {
            return String.class;
        }

        @Override
        public String getName() {
            return "getAbc";
        }

        @Override
        public int getModifiers() {
            return Modifier.PUBLIC;
        }

        @Override
        public CachedClass getDeclaringClass() {
            return ReflectionCache.getCachedClass(ModelCheck.class);
        }
    }, new MetaMethod() {

        @Override
        public Object invoke(Object arg0, Object[] arg1) {
            abcref.set((String) arg1[0]);
            return null;
        }

        @Override
        public Class getReturnType() {
            return null;
        }

        @Override
        public String getName() {
            return "setAbc";
        }

        @Override
        public int getModifiers() {
            return Modifier.PUBLIC;
        }

        @Override
        public CachedClass getDeclaringClass() {
            return ReflectionCache.getCachedClass(ModelCheck.class);
        }
    });
    meta.addMetaBeanProperty(abc);
    GroovySystem.getMetaClassRegistry().setMetaClass(ModelCheck.class, meta);
    Map mc2 = (Map) Model.mapObject(mc);
    Assert.assertEquals(2, mc2.size());
    Assert.assertNull(mc2.get("abc"));
    Model.put(mc, "abc", "xyz");
    Map mc3 = (Map) Model.mapObject(mc);
    Assert.assertEquals("xyz", mc3.get("abc"));
    MetaClassImpl meta2 = new MetaClassImpl(ModelCheck.class);
    meta2.initialize();
    GroovySystem.getMetaClassRegistry().setMetaClass(ModelCheck.class, meta2);
    Map mc4 = (Map) Model.mapObject(mc);
    Assert.assertEquals(1, mc4.size());
}
Also used : MetaMethod(groovy.lang.MetaMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) MetaBeanProperty(groovy.lang.MetaBeanProperty) CachedClass(org.codehaus.groovy.reflection.CachedClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetaClassImpl(groovy.lang.MetaClassImpl) Test(org.junit.Test)

Aggregations

MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaClass (groovy.lang.MetaClass)17 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)4 ClosureMetaClass (org.codehaus.groovy.runtime.metaclass.ClosureMetaClass)4 GroovyInterceptable (groovy.lang.GroovyInterceptable)3 MetaMethod (groovy.lang.MetaMethod)3 ProxyMetaClass (groovy.lang.ProxyMetaClass)3 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MetaProperty (groovy.lang.MetaProperty)2 CachedField (org.codehaus.groovy.reflection.CachedField)2 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)2 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)2 GroovyObject (groovy.lang.GroovyObject)1 MetaBeanProperty (groovy.lang.MetaBeanProperty)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1