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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations