Search in sources :

Example 6 with CachedField

use of org.codehaus.groovy.reflection.CachedField in project groovy-core by groovy.

the class MetaClassImpl method getProperties.

/**
     * Get all the properties defined for this type
     *
     * @return a list of MetaProperty objects
     */
public List<MetaProperty> getProperties() {
    checkInitalised();
    SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
    if (propertyMap == null) {
        // GROOVY-6903: May happen in some special environment, like under Android, due
        // to classloading issues
        propertyMap = new SingleKeyHashMap();
    }
    // simply return the values of the metaproperty map as a List
    List ret = new ArrayList(propertyMap.size());
    for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
        MetaProperty element = (MetaProperty) ((SingleKeyHashMap.Entry) iter.next()).value;
        if (element instanceof CachedField)
            continue;
        // filter out DGM beans
        if (element instanceof MetaBeanProperty) {
            MetaBeanProperty mp = (MetaBeanProperty) element;
            boolean setter = true;
            boolean getter = true;
            if (mp.getGetter() == null || mp.getGetter() instanceof GeneratedMetaMethod || mp.getGetter() instanceof NewInstanceMetaMethod) {
                getter = false;
            }
            if (mp.getSetter() == null || mp.getSetter() instanceof GeneratedMetaMethod || mp.getSetter() instanceof NewInstanceMetaMethod) {
                setter = false;
            }
            if (!setter && !getter)
                continue;
        //  TODO: I (ait) don't know why these strange tricks needed and comment following as it effects some Grails tests
        //                if (!setter && mp.getSetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), mp.getGetter(), null);
        //                }
        //                if (!getter && mp.getGetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), null, mp.getSetter());
        //                }
        }
        ret.add(element);
    }
    return ret;
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 7 with CachedField

use of org.codehaus.groovy.reflection.CachedField in project groovy-core by groovy.

the class MetaClassImpl method establishStaticMetaProperty.

private MetaProperty establishStaticMetaProperty(MetaProperty mp) {
    MetaBeanProperty mbp = (MetaBeanProperty) mp;
    MetaProperty result = null;
    final MetaMethod getterMethod = mbp.getGetter();
    final MetaMethod setterMethod = mbp.getSetter();
    final CachedField metaField = mbp.getField();
    boolean getter = getterMethod == null || getterMethod.isStatic();
    boolean setter = setterMethod == null || setterMethod.isStatic();
    boolean field = metaField == null || metaField.isStatic();
    if (!getter && !setter && !field) {
        return result;
    } else {
        final String propertyName = mbp.getName();
        final Class propertyType = mbp.getType();
        if (setter && getter) {
            if (field) {
                // nothing to do
                result = mbp;
            } else {
                result = new MetaBeanProperty(propertyName, propertyType, getterMethod, setterMethod);
            }
        } else if (getter && !setter) {
            if (getterMethod == null) {
                result = metaField;
            } else {
                MetaBeanProperty newmp = new MetaBeanProperty(propertyName, propertyType, getterMethod, null);
                if (field)
                    newmp.setField(metaField);
                result = newmp;
            }
        } else if (setter && !getter) {
            if (setterMethod == null) {
                result = metaField;
            } else {
                MetaBeanProperty newmp = new MetaBeanProperty(propertyName, propertyType, null, setterMethod);
                if (field)
                    newmp.setField(metaField);
                result = newmp;
            }
        } else
            result = metaField;
    }
    return result;
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 8 with CachedField

use of org.codehaus.groovy.reflection.CachedField in project groovy-core by groovy.

the class MetaClassImpl method addMetaBeanProperty.

/**
     * Adds a new MetaBeanProperty to this MetaClass
     *
     * @param mp The MetaBeanProperty
     */
public void addMetaBeanProperty(MetaBeanProperty mp) {
    MetaProperty staticProperty = establishStaticMetaProperty(mp);
    if (staticProperty != null) {
        staticPropertyIndex.put(mp.getName(), mp);
    } else {
        SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
        //keep field
        CachedField field;
        MetaProperty old = (MetaProperty) propertyMap.get(mp.getName());
        if (old != null) {
            if (old instanceof MetaBeanProperty) {
                field = ((MetaBeanProperty) old).getField();
            } else if (old instanceof MultipleSetterProperty) {
                field = ((MultipleSetterProperty) old).getField();
            } else {
                field = (CachedField) old;
            }
            mp.setField(field);
        }
        // put it in the list
        // this will overwrite a possible field property
        propertyMap.put(mp.getName(), mp);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) MultipleSetterProperty(org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 9 with CachedField

use of org.codehaus.groovy.reflection.CachedField in project groovy-core by groovy.

the class MetaClassImpl method makeStaticPropertyIndex.

private void makeStaticPropertyIndex() {
    SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
    for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
        SingleKeyHashMap.Entry entry = ((SingleKeyHashMap.Entry) iter.next());
        MetaProperty mp = (MetaProperty) entry.getValue();
        if (mp instanceof CachedField) {
            CachedField mfp = (CachedField) mp;
            if (!mfp.isStatic())
                continue;
        } else if (mp instanceof MetaBeanProperty) {
            MetaProperty result = establishStaticMetaProperty(mp);
            if (result == null)
                continue;
            else {
                mp = result;
            }
        } else if (mp instanceof MultipleSetterProperty) {
            MultipleSetterProperty msp = (MultipleSetterProperty) mp;
            mp = msp.createStaticVersion();
        } else {
            // ignore all other types
            continue;
        }
        staticPropertyIndex.put(entry.getKey(), mp);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) MultipleSetterProperty(org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 10 with CachedField

use of org.codehaus.groovy.reflection.CachedField in project groovy by apache.

the class MetaClassImpl method addMetaBeanProperty.

/**
     * Adds a new MetaBeanProperty to this MetaClass
     *
     * @param mp The MetaBeanProperty
     */
public void addMetaBeanProperty(MetaBeanProperty mp) {
    MetaProperty staticProperty = establishStaticMetaProperty(mp);
    if (staticProperty != null) {
        staticPropertyIndex.put(mp.getName(), mp);
    } else {
        SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
        //keep field
        CachedField field;
        MetaProperty old = (MetaProperty) propertyMap.get(mp.getName());
        if (old != null) {
            if (old instanceof MetaBeanProperty) {
                field = ((MetaBeanProperty) old).getField();
            } else if (old instanceof MultipleSetterProperty) {
                field = ((MultipleSetterProperty) old).getField();
            } else {
                field = (CachedField) old;
            }
            mp.setField(field);
        }
        // put it in the list
        // this will overwrite a possible field property
        propertyMap.put(mp.getName(), mp);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) MultipleSetterProperty(org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) CachedField(org.codehaus.groovy.reflection.CachedField)

Aggregations

CachedField (org.codehaus.groovy.reflection.CachedField)12 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)8 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)8 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)8 MultipleSetterProperty (org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty)6 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)6 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 CachedClass (org.codehaus.groovy.reflection.CachedClass)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2