Search in sources :

Example 16 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap 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 17 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap 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 18 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy-core by groovy.

the class MetaClassImpl method copyNonPrivateFields.

private void copyNonPrivateFields(SingleKeyHashMap from, SingleKeyHashMap to) {
    for (ComplexKeyHashMap.EntryIterator iter = from.getEntrySetIterator(); iter.hasNext(); ) {
        SingleKeyHashMap.Entry entry = (SingleKeyHashMap.Entry) iter.next();
        CachedField mfp = (CachedField) entry.getValue();
        if (!Modifier.isPublic(mfp.getModifiers()) && !Modifier.isProtected(mfp.getModifiers()))
            continue;
        to.put(entry.getKey(), mfp);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 19 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy-core by groovy.

the class MetaClassImpl method getMetaProperty.

private MetaProperty getMetaProperty(String name, boolean useStatic) {
    CachedClass clazz = theCachedClass;
    SingleKeyHashMap propertyMap;
    if (useStatic) {
        propertyMap = staticPropertyIndex;
    } else {
        propertyMap = classPropertyIndex.getNullable(clazz);
    }
    if (propertyMap == null) {
        return null;
    }
    return (MetaProperty) propertyMap.get(name);
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)

Example 20 with SingleKeyHashMap

use of org.codehaus.groovy.util.SingleKeyHashMap in project groovy-core by groovy.

the class MetaClassImpl method setupProperties.

/**
 * This will build up the property map (Map of MetaProperty objects, keyed on
 * property name).
 *
 * @param propertyDescriptors
 */
@SuppressWarnings("unchecked")
private void setupProperties(PropertyDescriptor[] propertyDescriptors) {
    if (theCachedClass.isInterface) {
        LinkedList<CachedClass> superClasses = new LinkedList<CachedClass>();
        superClasses.add(ReflectionCache.OBJECT_CLASS);
        Set interfaces = theCachedClass.getInterfaces();
        LinkedList<CachedClass> superInterfaces = new LinkedList<CachedClass>(interfaces);
        // ambiguous fields (class implementing two interfaces using the same field)
        if (superInterfaces.size() > 1) {
            Collections.sort(superInterfaces, CACHED_CLASS_NAME_COMPARATOR);
        }
        SingleKeyHashMap iPropertyIndex = classPropertyIndex.getNotNull(theCachedClass);
        for (CachedClass iclass : superInterfaces) {
            SingleKeyHashMap sPropertyIndex = classPropertyIndex.getNotNull(iclass);
            copyNonPrivateFields(sPropertyIndex, iPropertyIndex);
            addFields(iclass, iPropertyIndex);
        }
        addFields(theCachedClass, iPropertyIndex);
        applyPropertyDescriptors(propertyDescriptors);
        applyStrayPropertyMethods(superClasses, classPropertyIndex, true);
        makeStaticPropertyIndex();
    } else {
        LinkedList<CachedClass> superClasses = getSuperClasses();
        LinkedList<CachedClass> interfaces = new LinkedList<CachedClass>(theCachedClass.getInterfaces());
        // ambiguous fields (class implementing two interfaces using the same field)
        if (interfaces.size() > 1) {
            Collections.sort(interfaces, CACHED_CLASS_NAME_COMPARATOR);
        }
        // if this an Array, then add the special read-only "length" property
        if (theCachedClass.isArray) {
            SingleKeyHashMap map = new SingleKeyHashMap();
            map.put("length", arrayLengthProperty);
            classPropertyIndex.put(theCachedClass, map);
        }
        inheritStaticInterfaceFields(superClasses, new LinkedHashSet(interfaces));
        inheritFields(superClasses);
        applyPropertyDescriptors(propertyDescriptors);
        applyStrayPropertyMethods(superClasses, classPropertyIndex, true);
        applyStrayPropertyMethods(superClasses, classPropertyIndexForSuper, false);
        copyClassPropertyIndexForSuper(classPropertyIndexForSuper);
        makeStaticPropertyIndex();
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)20 CachedClass (org.codehaus.groovy.reflection.CachedClass)10 CachedField (org.codehaus.groovy.reflection.CachedField)8 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)8 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)8 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)8 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)4 MultipleSetterProperty (org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MetaMethodIndex (org.codehaus.groovy.runtime.metaclass.MetaMethodIndex)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