/// <summary> /// Get the <see cref="Spring.Objects.Support.ISortDefinition"/>'s property /// value for the given object. /// </summary> /// <param name="obj">The object to get the property value for.</param> /// <returns>The property value.</returns> private object GetPropertyValue(object obj) { object propertyValue = null; if (obj != null) { IObjectWrapper ow = (IObjectWrapper)this.cachedObjectWrappers[obj]; if (ow == null) { ow = new ObjectWrapper(obj); this.cachedObjectWrappers.Add(obj, ow); } try { propertyValue = ow.GetPropertyValue(this.sortDefinition.Property); } catch (InvalidPropertyException) { // the property doesn't exist in the first place, so let exception through... throw; } catch (ObjectsException ex) { // if a nested property cannot be read, simply return null... if (logger.IsDebugEnabled) { logger.Debug("Could not access property - treating as null for sorting.", ex); } } } return(propertyValue); }
public ObjectWrapperFactory(SerializationContext serializationContext) { this._serializationContext = serializationContext; this._defaultWrapper = (IObjectWrapper) new BasicObjectWrapper(serializationContext); this._wrappers[typeof(AsObject)] = (IObjectWrapper) new AsObjectWrapper(serializationContext); this._wrappers[typeof(IExternalizable)] = (IObjectWrapper) new ExternalizableWrapper(serializationContext); this._wrappers[typeof(Exception)] = (IObjectWrapper) new ExceptionWrapper(serializationContext); }
/// <summary> /// Create new empty PropertyAccessExceptionsException. /// We'll add errors to it as we attempt to bind properties. /// </summary> public PropertyAccessExceptionsException( IObjectWrapper objectWrapper, PropertyAccessException[] propertyAccessExceptions) : base(string.Empty) { _objectWrapper = objectWrapper; _propertyAccessExceptions = propertyAccessExceptions ?? EmptyPropertyAccessExceptions; }
protected override object ResolveDependency(string objectName, IObjectDefinition objectDefinition, IObjectWrapper objectWrapper, PropertyInfo propertyInfo) { if (!propertyInfo.PropertyType.IsInterface) { return null; } Type mockType = typeof(Mock).MakeGenericType(propertyInfo.PropertyType); Mock mock = (Mock)Activator.CreateInstance(mockType, defaultMockBehavior); return mock.Object; }
public ObjectWrapperFactory(SerializationContext context) { this.context = context; defaultWrapper = new BasicObjectWrapper(context); wrappers[typeof(AsObject)] = new AsObjectWrapper(context); wrappers[typeof(IExternalizable)] = new ExternalizableWrapper(context); wrappers[typeof(Exception)] = new ExceptionWrapper(context); }
public ObjectWrapperFactory(SerializationContext serializationContext) { this.serializationContext = serializationContext; defaultWrapper = new BasicObjectWrapper(serializationContext); wrappers[typeof(AsObject)] = new AsObjectWrapper(serializationContext); wrappers[typeof(IExternalizable)] = new ExternalizableWrapper(serializationContext); wrappers[typeof(Exception)] = new ExceptionWrapper(serializationContext); }
/// <summary> /// Return an instance (possibly shared or independent) of the object /// managed by this factory. /// </summary> /// <returns> /// An instance (possibly shared or independent) of the object managed by /// this factory. /// </returns> /// <see cref="Spring.Objects.Factory.IFactoryObject.GetObject()"/> public object GetObject() { IObjectWrapper target = this.targetObjectWrapper; if (target == null) { // fetch the prototype object object... target = new ObjectWrapper(this.objectFactory[this.targetObjectName]); } object value = target.GetPropertyValue(this.propertyPath); if (value == null) { throw new FatalObjectException("PropertyPathFactoryObject is not allowed to return null, " + "but property value for path '" + this.propertyPath + "' is null."); } return(value); }
public IObjectWrapper Get(string id) { IObjectWrapper wrapper = null; _list.TryGetValue(id, out wrapper); Log.Debug("[GetElement] objectList.ContainsKey? " + _list.ContainsKey(id) + ", objectList.Count=" + _list.Count); if (wrapper == null) { wrapper = _list.FirstOrDefault(kv => (string)kv.Value.GetPropertyValue("AutomationId") == id).Value; } if (wrapper != null && wrapper.IsShown) { return(wrapper); } return(null); }
/// <summary> /// Populate the object instance in the given /// <see cref="Spring.Objects.IObjectWrapper"/> with the property values from the /// object definition. /// </summary> /// <param name="name"> /// The name of the object. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object. /// </param> protected void PopulateObject(string name, RootObjectDefinition definition, IObjectWrapper wrapper) { // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. bool continueWithPropertyPopulation = true; if (HasInstantiationAwareBeanPostProcessors) { foreach (IObjectPostProcessor processor in ObjectPostProcessors) { IInstantiationAwareObjectPostProcessor inProc = processor as IInstantiationAwareObjectPostProcessor; if (inProc != null) { if (!inProc.PostProcessAfterInstantiation(wrapper.WrappedInstance, name)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } IPropertyValues properties = definition.PropertyValues; if (wrapper == null) { if (properties.PropertyValues.Length > 0) { throw new ObjectCreationException(definition.ResourceDescription, name, "Cannot apply property values to null instance."); } // skip property population phase for null instance return; } if (definition.ResolvedAutowireMode == AutoWiringMode.ByName || definition.ResolvedAutowireMode == AutoWiringMode.ByType) { MutablePropertyValues mpvs = new MutablePropertyValues(properties); // add property values based on autowire by name if it's applied if (definition.ResolvedAutowireMode == AutoWiringMode.ByName) { AutowireByName(name, definition, wrapper, mpvs); } // add property values based on autowire by type if it's applied if (definition.ResolvedAutowireMode == AutoWiringMode.ByType) { AutowireByType(name, definition, wrapper, mpvs); } properties = mpvs; } //DependencyCheck(name, definition, wrapper, properties); bool hasInstAwareOpps = HasInstantiationAwareBeanPostProcessors; bool needsDepCheck = (definition.DependencyCheck != DependencyCheckingMode.None); if (hasInstAwareOpps || needsDepCheck) { PropertyInfo[] filteredPropInfo = FilterPropertyInfoForDependencyCheck(wrapper); if (hasInstAwareOpps) { foreach (IObjectPostProcessor processor in ObjectPostProcessors) { IInstantiationAwareObjectPostProcessor instantiationAwareObjectPostProcessor = processor as IInstantiationAwareObjectPostProcessor; if (instantiationAwareObjectPostProcessor != null) { properties = instantiationAwareObjectPostProcessor.PostProcessPropertyValues(properties, filteredPropInfo, wrapper.WrappedInstance, name); if (properties == null) { return; } } } } if (needsDepCheck) { CheckDependencies(name, definition, filteredPropInfo, properties); } } ApplyPropertyValues(name, definition, wrapper, properties); }
/// <summary> /// Return an array of object-type property names that are unsatisfied. /// </summary> /// <remarks> /// <p> /// These are probably unsatisfied references to other objects in the /// factory. Does not include simple properties like primitives or /// <see cref="System.String"/>s. /// </p> /// </remarks> /// <returns> /// An array of object-type property names that are unsatisfied. /// </returns> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object. /// </param> protected string[] UnsatisfiedNonSimpleProperties(RootObjectDefinition definition, IObjectWrapper wrapper) { ListSet results = new ListSet(); IPropertyValues pvs = definition.PropertyValues; PropertyInfo[] properties = wrapper.GetPropertyInfos(); foreach (PropertyInfo property in properties) { string name = property.Name; if (property.CanWrite && !IsExcludedFromDependencyCheck(property) && !pvs.Contains(name) && !ObjectUtils.IsSimpleProperty(property.PropertyType)) { results.Add(name); } } return (string[])CollectionUtils.ToArray(results, typeof(string)); }
/// <summary> /// Apply the given property values, resolving any runtime references /// to other objects in this object factory. /// </summary> /// <param name="name"> /// The object name passed for better exception information. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object. /// </param> /// <param name="properties"> /// The new property values. /// </param> /// <remarks> /// <p> /// Must use deep copy, so that we don't permanently modify this property. /// </p> /// </remarks> protected void ApplyPropertyValues(string name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) { if (properties == null || properties.PropertyValues.Length == 0) { return; } ObjectDefinitionValueResolver valueResolver = CreateValueResolver(); MutablePropertyValues deepCopy = new MutablePropertyValues(properties); PropertyValue[] copiedProperties = deepCopy.PropertyValues; for (int i = 0; i < copiedProperties.Length; ++i) { PropertyValue copiedProperty = copiedProperties[i]; //(string name, RootObjectDefinition definition, string argumentName, object argumentValue) object value = valueResolver.ResolveValueIfNecessary(name, definition, copiedProperty.Name, copiedProperty.Value); // object value = ResolveValueIfNecessary(name, definition, copiedProperty.Name, copiedProperty.Value); PropertyValue propertyValue = new PropertyValue(copiedProperty.Name, value, copiedProperty.Expression); // update mutable copy... deepCopy.SetPropertyValueAt(propertyValue, i); } // set the (possibly resolved) deep copy properties... try { wrapper.SetPropertyValues(deepCopy); } catch (ObjectsException ex) { // improve the message by showing the context... throw new ObjectCreationException(definition.ResourceDescription, name, "Error setting property values: " + ex.Message, ex); } }
public void receiveGift(IObjectWrapper o, IFarmerWrapper giver, bool updateGiftLimitInfo = true, float friendshipChangeMultiplier = 1, bool showResponse = true) { }
public bool canThisBeAttached(IObjectWrapper o) => false;
/// <summary> /// Configures object instance by injecting dependencies, satisfying Spring lifecycle /// interfaces and applying object post-processors. /// </summary> /// <param name="name"> /// The name of the object definition expressing the dependencies that are to /// be injected into the supplied <parameref name="target"/> instance. /// </param> /// <param name="definition"> /// An object definition that should be used to configure object. /// </param> /// <param name="wrapper"> /// A wrapped object instance that is to be so configured. /// </param> /// <seealso cref="Spring.Objects.Factory.IObjectFactory.ConfigureObject(object, string)"/> protected override object ConfigureObject(string name, RootObjectDefinition definition, IObjectWrapper wrapper) { // always configure object relative to contextPath using (new HttpContextSwitch(contextPath)) { return base.ConfigureObject(name, definition, wrapper); } }
/// <summary> /// Defines "autowire by type" (object properties by type) behavior. /// </summary> /// <remarks> /// <p> /// This is like PicoContainer default, in which there must be exactly one object /// of the property type in the object factory. This makes object factories simple /// to configure for small namespaces, but doesn't work as well as standard Spring /// behavior for bigger applications. /// </p> /// </remarks> /// <param name="name"> /// The object name to be autowired by <see cref="System.Type"/>. /// </param> /// <param name="definition"> /// The definition of the named object to update through autowiring. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object (and /// from which we can rip out information concerning the object). /// </param> /// <param name="properties"> /// The property values to register wired objects with. /// </param> protected void AutowireByType(string name, RootObjectDefinition definition, IObjectWrapper wrapper, MutablePropertyValues properties) { string[] propertyNames = UnsatisfiedNonSimpleProperties(definition, wrapper); foreach (string propertyName in propertyNames) { // look for a matching type Type requiredType = wrapper.GetPropertyType(propertyName); IDictionary matchingObjects = FindMatchingObjects(requiredType); if (matchingObjects != null && matchingObjects.Count == 1) { properties.Add(propertyName, ObjectUtils.EnumerateFirstElement(matchingObjects.Values)); #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Autowiring by type from object name '{0}' via property " + "'{1}' to object named '{2}'.", name, propertyName, ObjectUtils.EnumerateFirstElement(matchingObjects.Keys))); } #endregion } else if (matchingObjects != null && matchingObjects.Count > 1) { throw new UnsatisfiedDependencyException(string.Empty, name, propertyName, string.Format(CultureInfo.InvariantCulture, "There are {0} objects of Type [{1}] for autowire by " + "type, when there should have been just 1 to be able to " + "autowire property '{2}' of object '{3}'.", matchingObjects.Count, requiredType, propertyName, name)); } else { #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Not autowiring property '{0}' of object '{1}': no matching object found.", propertyName, name)); } #endregion } } }
/// <summary> /// Create new empty PropertyAccessExceptionsException. /// We'll add errors to it as we attempt to bind properties. /// </summary> public PropertyAccessExceptionsException( IObjectWrapper objectWrapper, PropertyAccessException[] propertyAccessExceptions) : base(string.Empty) { _objectWrapper = objectWrapper; _propertyAccessExceptions = propertyAccessExceptions == null ? EmptyPropertyAccessExceptions : propertyAccessExceptions; }
public bool collideWith(IObjectWrapper o) => false;
public void grabObject(IObjectWrapper obj) { }
public void eatObject(IObjectWrapper o, bool overrideFullness = false) { }
public void onGiftGiven(INPCWrapper npc, IObjectWrapper item) { }
public void revealGiftTaste(INPCWrapper npc, IObjectWrapper item) { }
public void makeThisTheActiveObject(IObjectWrapper o) { }
/// <summary> /// Wires up any exposed events in the object instance in the given /// <see cref="Spring.Objects.IObjectWrapper"/> with any event handler /// values from the <paramref name="definition"/>. /// </summary> /// <param name="name"> /// The name of the object. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object. /// </param> protected void WireEvents(string name, IConfigurableObjectDefinition definition, IObjectWrapper wrapper) { foreach (string eventName in definition.EventHandlerValues.Events) { foreach (IEventHandlerValue handlerValue in definition.EventHandlerValues[eventName]) { object handler = null; if (handlerValue.Source is RuntimeObjectReference) { RuntimeObjectReference roref = (RuntimeObjectReference)handlerValue.Source; handler = ResolveReference(definition, name, eventName, roref); } else if (handlerValue.Source is Type) { // a static Type event is being wired up; simply pass on the Type handler = handlerValue.Source; } else if (handlerValue.Source is string) { // a static Type event is being wired up; we need to resolve the Type handler = TypeResolutionUtils.ResolveType(handlerValue.Source as string); } else { throw new FatalObjectException("Currently, only references to other objects and Types are " + "supported as event sources."); } handlerValue.Wire(handler, wrapper.WrappedInstance); } } }
/// <summary> /// Fills in any missing property values with references to /// other objects in this factory if autowire is set to /// <see cref="Spring.Objects.Factory.Config.AutoWiringMode.ByName"/>. /// </summary> /// <param name="name"> /// The object name to be autowired by <see cref="System.Type"/>. /// </param> /// <param name="definition"> /// The definition of the named object to update through autowiring. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object (and /// from which we can rip out information concerning the object). /// </param> /// <param name="properties"> /// The property values to register wired objects with. /// </param> protected void AutowireByName(string name, RootObjectDefinition definition, IObjectWrapper wrapper, MutablePropertyValues properties) { string[] propertyNames = UnsatisfiedNonSimpleProperties(definition, wrapper); foreach (string propertyName in propertyNames) { // look for a matching type if (ContainsObject(propertyName)) { object o = GetObject(propertyName); properties.Add(propertyName, o); #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Added autowiring by name from object name '{0}' via " + "property '{1}' to object named '{1}'.", name, propertyName)); } #endregion } else { #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Not autowiring property '{0}' of object '{1}' by name: " + "no matching object found.", propertyName, name)); } #endregion } } }
public IObjectWrapper attach(IObjectWrapper o) => null;
/// <summary> /// Perform a dependency check that all properties exposed have been set, if desired. /// </summary> /// <remarks> /// <p> /// Dependency checks can be objects (collaborating objects), simple (primitives /// and <see cref="System.String"/>), or all (both). /// </p> /// </remarks> /// <param name="name"> /// The name of the object. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="wrapper"> /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object. /// </param> /// <param name="properties"> /// The property values to be checked. /// </param> /// <exception cref="Spring.Objects.Factory.UnsatisfiedDependencyException"> /// If all of the checked dependencies were not satisfied. /// </exception> protected void DependencyCheck(string name, IConfigurableObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) { DependencyCheckingMode dependencyCheck = definition.DependencyCheck; if (dependencyCheck == DependencyCheckingMode.None) { return; } PropertyInfo[] filteredPropInfo = FilterPropertyInfoForDependencyCheck(wrapper); if (HasInstantiationAwareBeanPostProcessors) { foreach (IObjectPostProcessor processor in ObjectPostProcessors) { IInstantiationAwareObjectPostProcessor inProc = processor as IInstantiationAwareObjectPostProcessor; if (inProc != null) { properties = inProc.PostProcessPropertyValues(properties, filteredPropInfo, wrapper.WrappedInstance, name); if (properties == null) { return; } } } } CheckDependencies(name, definition, filteredPropInfo, properties); }
/// <summary> /// Extract a filtered set of PropertyInfos from the given IObjectWrapper, excluding /// ignored dependency types. /// </summary> /// <param name="wrapper">The object wrapper the object was created with.</param> /// <returns>The filtered PropertyInfos</returns> private PropertyInfo[] FilterPropertyInfoForDependencyCheck(IObjectWrapper wrapper) { lock (filteredPropertyDescriptorsCache) { PropertyInfo[] filtered = (PropertyInfo[])filteredPropertyDescriptorsCache[wrapper.WrappedType]; if (filtered == null) { ArrayList list = new ArrayList(wrapper.GetPropertyInfos()); for (int i = list.Count - 1; i >= 0; i--) { PropertyInfo pi = (PropertyInfo)list[i]; if (IsExcludedFromDependencyCheck(pi)) { list.RemoveAt(i); } } filtered = (PropertyInfo[])list.ToArray(typeof(PropertyInfo)); filteredPropertyDescriptorsCache.Add(wrapper.WrappedType, filtered); } return filtered; } }
/// <summary> /// Configures object instance by injecting dependencies, satisfying Spring lifecycle /// interfaces and applying object post-processors. /// </summary> /// <param name="name"> /// The name of the object definition expressing the dependencies that are to /// be injected into the supplied <parameref name="target"/> instance. /// </param> /// <param name="definition"> /// An object definition that should be used to configure object. /// </param> /// <param name="wrapper"> /// A wrapped object instance that is to be so configured. /// </param> /// <seealso cref="Spring.Objects.Factory.IObjectFactory.ConfigureObject(object, string)"/> protected override object ConfigureObject(string name, RootObjectDefinition definition, IObjectWrapper wrapper) { // always configure object relative to contextPath using (new HttpContextSwitch(contextPath)) { return(base.ConfigureObject(name, definition, wrapper)); } }
/// <summary> /// Configures object instance by injecting dependencies, satisfying Spring lifecycle /// interfaces and applying object post-processors. /// </summary> /// <param name="name"> /// The name of the object definition expressing the dependencies that are to /// be injected into the supplied <parameref name="target"/> instance. /// </param> /// <param name="definition"> /// An object definition that should be used to configure object. /// </param> /// <param name="wrapper"> /// A wrapped object instance that is to be so configured. /// </param> /// <seealso cref="Spring.Objects.Factory.IObjectFactory.ConfigureObject(object, string)"/> protected virtual object ConfigureObject(string name, RootObjectDefinition definition, IObjectWrapper wrapper) { object instance = wrapper.WrappedInstance; #region Instrumentation if (log.IsDebugEnabled) { log.Debug(string.Format("Configuring object using definition '{1}'", instance, name)); } #endregion PopulateObject(name, definition, wrapper); WireEvents(name, definition, wrapper); if (ObjectUtils.IsAssignableAndNotTransparentProxy(typeof(IObjectNameAware), instance)) { #region Instrumentation if (log.IsDebugEnabled) { log.Debug(string.Format(CultureInfo.InvariantCulture, "Setting the name property on the IObjectNameAware object '{0}'.", name)); } #endregion ((IObjectNameAware)instance).ObjectName = name; } if (ObjectUtils.IsAssignableAndNotTransparentProxy(typeof(IObjectFactoryAware), instance)) { #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Setting the ObjectFactory property on the IObjectFactoryAware object '{0}'.", name)); } #endregion ((IObjectFactoryAware)instance).ObjectFactory = this; } instance = ApplyObjectPostProcessorsBeforeInitialization(instance, name); InvokeInitMethods(instance, name, definition); instance = ApplyObjectPostProcessorsAfterInitialization(instance, name); return instance; }
/// <summary> /// Extract a filtered set of PropertyInfos from the given IObjectWrapper, excluding /// ignored dependency types. /// </summary> /// <param name="wrapper">The object wrapper the object was created with.</param> /// <returns>The filtered PropertyInfos</returns> private IList<PropertyInfo> FilterPropertyInfoForDependencyCheck(IObjectWrapper wrapper) { lock (filteredPropertyDescriptorsCache) { IList<PropertyInfo> filtered; if (!filteredPropertyDescriptorsCache.TryGetValue(wrapper.WrappedType, out filtered)) { List<PropertyInfo> list = new List<PropertyInfo>(wrapper.GetPropertyInfos()); for (int i = list.Count - 1; i >= 0; i--) { PropertyInfo pi = list[i]; if (IsExcludedFromDependencyCheck(pi)) { list.RemoveAt(i); } } filtered = list; filteredPropertyDescriptorsCache.Add(wrapper.WrappedType, filtered); } return filtered; } }
static void AddWrapperForType(Type t, string name, IObjectWrapper wrapper) { try { MembersByType[t].Add(name, wrapper); CaseInsensitiveMembersByType[t].Add(name, wrapper); } catch (ArgumentException exc) { throw new ArgumentException("The type \"" + t.FullName + "\" has multiple members named \"" + name + "\" (case insensitive). Self accessing dynamic-objects cannot support this.", exc); } }
protected abstract object ResolveDependency(string objectName, IObjectDefinition objectDefinition, IObjectWrapper objectWrapper, PropertyInfo propertyInfo);
/// <summary> /// Converts the instance to connection string /// </summary> /// <param name="instance"></param> /// <returns></returns> public string InstanceToString(IObjectWrapper <T> instance) { return(string.Join(Delimiter, instance.Flatten().Select(x => $"{x.Key}={x.Value}"))); }