Search in sources :

Example 1 with MetaProperty

use of groovy.lang.MetaProperty in project spock by spockframework.

the class CollectionSlotFactory method create.

public ISlot create(Object owner, Type ownerType, String name) {
    String plural = toPluralForm(name);
    MetaProperty property = GroovyRuntimeUtil.getMetaClass(owner).getMetaProperty(plural);
    return property != null && Collection.class.isAssignableFrom(property.getType()) && MopUtil.isReadable(property) ? new CollectionSlot(plural, owner, ownerType, property) : null;
}
Also used : Collection(java.util.Collection) MetaProperty(groovy.lang.MetaProperty)

Example 2 with MetaProperty

use of groovy.lang.MetaProperty in project grails-core by grails.

the class LazyMetaPropertyMap method get.

/**
     * Obtains the value of an object's properties on demand using Groovy's MOP.
     *
     * @param propertyName The name of the property
     * @return The property value or null
     */
public Object get(Object propertyName) {
    if (propertyName instanceof CharSequence) {
        propertyName = propertyName.toString();
    }
    if (propertyName instanceof List) {
        Map submap = new HashMap();
        List propertyNames = (List) propertyName;
        for (Object currentName : propertyNames) {
            if (currentName != null) {
                currentName = currentName.toString();
                if (containsKey(currentName)) {
                    submap.put(currentName, get(currentName));
                }
            }
        }
        return submap;
    }
    if (GrailsDomainConfigurationUtil.isConfigurational(propertyName.toString())) {
        return null;
    }
    Object val = null;
    MetaProperty mp = metaClass.getMetaProperty(propertyName.toString());
    if (mp != null) {
        val = mp.getProperty(instance);
    }
    return val;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) MetaProperty(groovy.lang.MetaProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MetaProperty

use of groovy.lang.MetaProperty in project grails-core by grails.

the class LazyMetaPropertyMap method put.

public Object put(Object propertyName, Object propertyValue) {
    if (propertyName instanceof CharSequence) {
        propertyName = propertyName.toString();
    }
    Object old = null;
    MetaProperty mp = metaClass.getMetaProperty((String) propertyName);
    if (mp != null && !isExcluded(mp)) {
        old = mp.getProperty(instance);
        if (propertyValue instanceof Map) {
            propertyValue = ((Map) propertyValue).get(propertyName);
        }
        mp.setProperty(instance, propertyValue);
    }
    return old;
}
Also used : MetaProperty(groovy.lang.MetaProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MetaProperty

use of groovy.lang.MetaProperty in project grails-core by grails.

the class DefaultGrailsDomainClass method initializePersistentProperties.

private void initializePersistentProperties() {
    if (!propertiesInitialized) {
        verifyContextIsInitialized();
        propertyMap = new LinkedHashMap<>();
        PersistentProperty identity = persistentEntity.getIdentity();
        if (identity != null) {
            identifier = new DefaultGrailsDomainClassProperty(this, persistentEntity, identity);
        }
        // First go through the properties of the class and create domain properties
        // populating into a map
        populateDomainClassProperties();
        // set properties from map values
        persistentProperties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
        // if no identifier property throw exception
        if (identifier == null) {
            throw new GrailsDomainException("Identity property not found, but required in domain class [" + getFullName() + "]");
        } else {
            propertyMap.put(identifier.getName(), identifier);
        }
        // if no version property throw exception
        if (version != null) {
            propertyMap.put(version.getName(), version);
        }
        List<MetaProperty> properties = getMetaClass().getProperties();
        for (MetaProperty property : properties) {
            String name = property.getName();
            if (!propertyMap.containsKey(name) && !NameUtils.isConfigurational(name) && !Modifier.isStatic(property.getModifiers())) {
                propertyMap.put(name, new MetaGrailsDomainClassProperty(this, property));
            }
        }
        this.properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
    }
    propertiesInitialized = true;
}
Also used : GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) PersistentProperty(org.grails.datastore.mapping.model.PersistentProperty) MetaProperty(groovy.lang.MetaProperty) GrailsDomainException(org.grails.core.exceptions.GrailsDomainException)

Example 5 with MetaProperty

use of groovy.lang.MetaProperty in project grails-core by grails.

the class GrailsClassUtils method findPropertyNameForValue.

/**
     * Locates the name of a property for the given value on the target object using Groovy's meta APIs.
     * Note that this method uses the reference so the incorrect result could be returned for two properties
     * that refer to the same reference. Use with caution.
     *
     * @param target The target
     * @param obj The property value
     * @return The property name or null
     */
public static String findPropertyNameForValue(Object target, Object obj) {
    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(target.getClass());
    List<MetaProperty> metaProperties = mc.getProperties();
    for (MetaProperty metaProperty : metaProperties) {
        if (isAssignableOrConvertibleFrom(metaProperty.getType(), obj.getClass())) {
            Object val = metaProperty.getProperty(target);
            if (val != null && val.equals(obj)) {
                return metaProperty.getName();
            }
        }
    }
    return null;
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) ConfigObject(groovy.util.ConfigObject) MetaProperty(groovy.lang.MetaProperty)

Aggregations

MetaProperty (groovy.lang.MetaProperty)12 ArrayList (java.util.ArrayList)5 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)4 MetaClass (groovy.lang.MetaClass)4 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)2 GString (groovy.lang.GString)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MetaMethod (groovy.lang.MetaMethod)2 MissingPropertyException (groovy.lang.MissingPropertyException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)2 HandleMetaClass (org.codehaus.groovy.runtime.HandleMetaClass)2 MixedInMetaClass (org.codehaus.groovy.runtime.metaclass.MixedInMetaClass)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 MixinInstanceMetaProperty (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)1 GroovyObject (groovy.lang.GroovyObject)1