private MethodModel GetMethod(MethodInfo methodInfo) { var model = new MethodModel(methodInfo) { AllowAnyStatusCodeAttribute = Get <AllowAnyStatusCodeAttribute>(), SerializationMethodsAttribute = Get <SerializationMethodsAttribute>(), IsDisposeMethod = methodInfo == MethodInfos.IDisposable_Dispose, }; model.RequestAttributes.AddRange(GetAll <RequestAttributeBase>()); model.HeaderAttributes.AddRange(GetAll <HeaderAttribute>()); model.Parameters.AddRange(methodInfo.GetParameters().Select(this.GetParameter)); return(model); AttributeModel <T>?Get <T>() where T : Attribute { var attribute = methodInfo.GetCustomAttribute <T>(); return(attribute == null ? null : AttributeModel.Create(attribute, methodInfo)); } IEnumerable <AttributeModel <T> > GetAll <T>() where T : Attribute => methodInfo.GetCustomAttributes <T>().Select(x => AttributeModel.Create(x, methodInfo)); }
private ParameterModel GetParameter(IParameterSymbol parameterSymbol) { var attributes = this.attributeInstantiator.Instantiate(parameterSymbol, this.diagnosticReporter).ToList(); var model = new ParameterModel(parameterSymbol) { HeaderAttribute = Get <HeaderAttribute>(), PathAttribute = Get <PathAttribute>(), QueryAttribute = Get <QueryAttribute>(), HttpRequestMessagePropertyAttribute = Get <HttpRequestMessagePropertyAttribute>(), RawQueryStringAttribute = Get <RawQueryStringAttribute>(), QueryMapAttribute = Get <QueryMapAttribute>(), BodyAttribute = Get <BodyAttribute>(), IsCancellationToken = SymbolEqualityComparer.Default.Equals(parameterSymbol.Type, this.wellKnownSymbols.CancellationToken), IsByRef = parameterSymbol.RefKind != RefKind.None, }; return(model); AttributeModel <T>?Get <T>() where T : Attribute { var(attribute, attributeData, type) = attributes.FirstOrDefault(x => x.attribute is T); return(attribute == null ? null : AttributeModel.Create((T)attribute, attributeData, type)); } }
private MethodModel GetMethod(IMethodSymbol methodSymbol) { var attributes = this.attributeInstantiator.Instantiate(methodSymbol, this.diagnosticReporter).ToList(); var model = new MethodModel(methodSymbol) { RequestAttribute = Get <RequestAttributeBase>(), AllowAnyStatusCodeAttribute = Get <AllowAnyStatusCodeAttribute>(), SerializationMethodsAttribute = Get <SerializationMethodsAttribute>(), IsDisposeMethod = SymbolEqualityComparer.Default.Equals(methodSymbol, this.wellKnownSymbols.IDisposable_Dispose), }; model.HeaderAttributes.AddRange(attributes.Where(x => x.attribute is HeaderAttribute) .Select(x => AttributeModel.Create((HeaderAttribute)x.attribute, x.attributeData, methodSymbol))); model.Parameters.AddRange(methodSymbol.Parameters.Select(this.GetParameter)); return(model); AttributeModel <T>?Get <T>() where T : Attribute { var(attribute, attributeData, type) = attributes.FirstOrDefault(x => x.attribute is T); return(attribute == null ? null : AttributeModel.Create((T)attribute, attributeData, type)); } }
public TypeModel Analyze() { var attributes = this.attributeInstantiator.Instantiate(this.namedTypeSymbol, this.diagnosticReporter).ToList(); var typeAndInterfaceAttributes = attributes.Concat( this.namedTypeSymbol.AllInterfaces.SelectMany(x => this.attributeInstantiator.Instantiate(x, this.diagnosticReporter))).ToList(); var typeModel = new TypeModel(this.namedTypeSymbol) { SerializationMethodsAttribute = Get <SerializationMethodsAttribute>(), BaseAddressAttribute = Get <BaseAddressAttribute>(), BasePathAttribute = Get <BasePathAttribute>(), IsAccessible = this.compilation.IsSymbolAccessibleWithin(this.namedTypeSymbol, this.compilation.Assembly) }; typeModel.HeaderAttributes.AddRange(GetAll <HeaderAttribute>()); typeModel.AllowAnyStatusCodeAttributes.AddRange(GetAll <AllowAnyStatusCodeAttribute>()); foreach (var member in this.InterfaceAndParents().SelectMany(x => x.GetMembers())) { switch (member) { case IPropertySymbol property: typeModel.Properties.Add(this.GetProperty(property)); break; case IMethodSymbol method when method.MethodKind == MethodKind.Ordinary: typeModel.Methods.Add(this.GetMethod(method)); break; case IEventSymbol evt: typeModel.Events.Add(new EventModel(evt)); break; } } return(typeModel); AttributeModel <T>?Get <T>() where T : Attribute { var(attribute, attributeData, type) = attributes.FirstOrDefault(x => x.attribute is T); return(attribute == null ? null : AttributeModel.Create((T)attribute, attributeData, type)); } IEnumerable <AttributeModel <T> > GetAll <T>() where T : Attribute => typeAndInterfaceAttributes.Where(x => x.attribute is T) .Select(x => AttributeModel.Create((T)x.attribute !, x.attributeData, x.declaringSymbol)); }
private PropertyModel GetProperty(PropertyInfo propertyInfo) { var model = new PropertyModel(propertyInfo) { HeaderAttribute = Get <HeaderAttribute>(), PathAttribute = Get <PathAttribute>(), QueryAttribute = Get <QueryAttribute>(), HttpRequestMessagePropertyAttribute = Get <HttpRequestMessagePropertyAttribute>(), IsRequester = propertyInfo.PropertyType == typeof(IRequester), HasGetter = propertyInfo.CanRead, HasSetter = propertyInfo.CanWrite, }; return(model); AttributeModel <T>?Get <T>() where T : Attribute { var attribute = propertyInfo.GetCustomAttribute <T>(); return(attribute == null ? null : AttributeModel.Create(attribute, propertyInfo)); } }
public TypeModel Analyze() { var typeModel = new TypeModel(this.interfaceType) { SerializationMethodsAttribute = Get <SerializationMethodsAttribute>(), BaseAddressAttribute = Get <BaseAddressAttribute>(), BasePathAttribute = Get <BasePathAttribute>(), IsAccessible = IsAccessible(this.interfaceTypeInfo), }; typeModel.HeaderAttributes.AddRange(GetAll <HeaderAttribute>()); typeModel.AllowAnyStatusCodeAttributes.AddRange(GetAll <AllowAnyStatusCodeAttribute>()); typeModel.Events.AddRange(this.InterfaceAndParents(x => x.GetEvents()).Select(x => EventModel.Instance)); typeModel.Properties.AddRange(this.InterfaceAndParents(x => x.GetProperties()).Select(this.GetProperty)); foreach (var methodInfo in this.InterfaceAndParents(x => x.GetMethods())) { // Exclude property getter / setters, etc if (!methodInfo.IsSpecialName) { typeModel.Methods.Add(this.GetMethod(methodInfo)); } } return(typeModel); AttributeModel <T>?Get <T>() where T : Attribute { var attribute = this.interfaceTypeInfo.GetCustomAttribute <T>(); return(attribute == null ? null : AttributeModel.Create(attribute, this.interfaceTypeInfo)); } IEnumerable <AttributeModel <T> > GetAll <T>() where T : Attribute => this.InterfaceAndParents().SelectMany(x => x.GetCustomAttributes <T>() .Select(a => AttributeModel.Create(a, x))); }
private PropertyModel GetProperty(IPropertySymbol propertySymbol) { var attributes = this.attributeInstantiator.Instantiate(propertySymbol, this.diagnosticReporter).ToList(); var model = new PropertyModel(propertySymbol) { HeaderAttribute = Get <HeaderAttribute>(), PathAttribute = Get <PathAttribute>(), QueryAttribute = Get <QueryAttribute>(), HttpRequestMessagePropertyAttribute = Get <HttpRequestMessagePropertyAttribute>(), IsRequester = SymbolEqualityComparer.Default.Equals(propertySymbol.Type, this.wellKnownSymbols.IRequester), HasGetter = propertySymbol.GetMethod != null, HasSetter = propertySymbol.SetMethod != null, }; return(model); AttributeModel <T>?Get <T>() where T : Attribute { var(attribute, attributeData, type) = attributes.FirstOrDefault(x => x.attribute is T); return(attribute == null ? null : AttributeModel.Create((T)attribute, attributeData, type)); } }
private ParameterModel GetParameter(ParameterInfo parameterInfo) { var model = new ParameterModel(parameterInfo) { HeaderAttribute = Get <HeaderAttribute>(), PathAttribute = Get <PathAttribute>(), QueryAttribute = Get <QueryAttribute>(), HttpRequestMessagePropertyAttribute = Get <HttpRequestMessagePropertyAttribute>(), RawQueryStringAttribute = Get <RawQueryStringAttribute>(), QueryMapAttribute = Get <QueryMapAttribute>(), BodyAttribute = Get <BodyAttribute>(), IsCancellationToken = parameterInfo.ParameterType == typeof(CancellationToken), IsByRef = parameterInfo.ParameterType.IsByRef, }; return(model); AttributeModel <T>?Get <T>() where T : Attribute { var attribute = parameterInfo.GetCustomAttribute <T>(); return(attribute == null ? null : AttributeModel.Create(attribute, null)); } }