private void ParseQualifierProperties(QualifierAttribute attr, AutowireCandidateQualifier qualifier) { foreach (var property in attr.GetType().GetProperties()) { if (!property.Name.Equals("TypeId") && !property.Name.Equals("Value")) { object value = property.GetValue(attr, null); if (value != null) { var attribute = new ObjectMetadataAttribute(property.Name, value); qualifier.AddMetadataAttribute(attribute); } } } }
private void ParseQualifierAttribute() { var attr = Attribute.GetCustomAttribute(ObjectType, typeof(QualifierAttribute), true) as QualifierAttribute; if (attr != null) { var qualifier = new AutowireCandidateQualifier(attr.GetType()); if (!string.IsNullOrEmpty(attr.Value)) { qualifier.SetAttribute(AutowireCandidateQualifier.VALUE_KEY, attr.Value); } ParseQualifierProperties(attr, qualifier); AddQualifier(qualifier); } }
/// <summary> /// Match the given qualifier attribute against the candidate bean definition. /// </summary> protected bool CheckQualifier(ObjectDefinitionHolder odHolder, Attribute attribute) { Type type = attribute.GetType(); RootObjectDefinition od = (RootObjectDefinition)odHolder.ObjectDefinition; AutowireCandidateQualifier qualifier = od.GetQualifier(type.FullName); if (qualifier == null) { qualifier = od.GetQualifier(type.Name); } if (qualifier == null) { Attribute targetAttribute = null; // TODO: Get the resolved factory method //if (od.GetResolvedFactoryMethod() != null) { // targetAttribute = Attribute.GetCustomAttribute(od.GetResolvedFactoryMethod(), type); //} if (targetAttribute == null) { // look for matching attribute on the target class if (_objectFactory != null) { Type objectType = od.ObjectType; if (objectType != null) { targetAttribute = Attribute.GetCustomAttribute(objectType, type); } } if (targetAttribute == null && od.ObjectType != null) { targetAttribute = Attribute.GetCustomAttribute(od.ObjectType, type); } } if (targetAttribute != null && targetAttribute.Equals(attribute)) { return(true); } } IDictionary <string, object> attributes = AttributeUtils.GetAttributeProperties(attribute); if (attributes.Count == 0 && qualifier == null) { // if no attributes, the qualifier must be present return(false); } foreach (var entry in attributes) { string propertyName = entry.Key; object expectedValue = entry.Value; object actualValue = null; // check qualifier first if (qualifier != null) { actualValue = qualifier.GetAttribute(propertyName); } if (actualValue == null) { // fall back on bean definition attribute actualValue = od.GetAttribute(propertyName); } if (actualValue == null && propertyName.Equals(AutowireCandidateQualifier.VALUE_KEY) && expectedValue is string && odHolder.MatchesName((string)expectedValue)) { // fall back on bean name (or alias) match continue; } if (actualValue == null && qualifier != null) { // fall back on default, but only if the qualifier is present actualValue = AttributeUtils.GetDefaultValue(attribute, propertyName); } if (actualValue != null) { actualValue = TypeConversionUtils.ConvertValueIfNecessary(expectedValue.GetType(), actualValue, null); } if (!expectedValue.Equals(actualValue)) { return(false); } } return(true); }