Skip to content

Commit

Permalink
Add support for static class Annotation
Browse files Browse the repository at this point in the history
Close #2
  • Loading branch information
vbfox committed Sep 21, 2015
1 parent a796baa commit a3b0772
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -42,12 +43,38 @@ public void AnnotateType<TType>(Action<ITypeAnnotator<TType>> annotationActions)

public void AnnotateStatic(Expression<Action> expression)
{
throw new NotImplementedException();
AnnotateStaticCore(expression);
}

public void AnnotateStatic<TResult>(Expression<Func<TResult>> expression)
{
throw new NotImplementedException();
AnnotateStaticCore(expression);
}

private MemberAnnotations GetMemberAnnotations(MemberInfo member)
{
Debug.Assert(member != null);
Debug.Assert(member.DeclaringType != null);

var assemblyAnnotations = GetAssemblyAnnotations(member.DeclaringType.Assembly);
var existing = assemblyAnnotations.FirstOrDefault(m => m.Member == member);
if (existing != null)
{
return existing;
}

var newAnnotations = new MemberAnnotations(member);
assemblyAnnotations.Add(newAnnotations);
return newAnnotations;
}

private void AnnotateStaticCore(LambdaExpression expression)
{
var result = ExpressionParser.Parse(expression);
Debug.Assert(result.Member.DeclaringType != null,
"Expression parser shouldn't succeed for members without declaring type");

GetMemberAnnotations(result.Member).AddAnnotationsFromParsing(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ExternalAnnotationsGenerator.Core.Construction
{
internal class TypeAnnotationsBuilder<TClass> : ITypeAnnotator<TClass>
internal class TypeAnnotationsBuilder<TType> : ITypeAnnotator<TType>
{
private readonly List<MemberAnnotations> membersAnnotations = new List<MemberAnnotations>();

Expand All @@ -29,23 +29,20 @@ private MemberAnnotations GetMemberAnnotations(MemberInfo member)
return newAnnotations;
}

public void Annotate<TResult>(Expression<Func<TClass, TResult>> expression)
public void Annotate<TResult>(Expression<Func<TType, TResult>> expression)
{
AnnotateCore(expression);
}

public void Annotate(Expression<Action<TClass>> expression)
public void Annotate(Expression<Action<TType>> expression)
{
AnnotateCore(expression);
}

private void AnnotateCore(LambdaExpression expression)
{
var result = ExpressionParser.Parse(expression);
var memberAnnotations = GetMemberAnnotations(result.Member);

memberAnnotations.Annotations.AddRange(result.Annotations);
memberAnnotations.ParameterAnnotations.AddRange(result.ParameterAnnotations);
GetMemberAnnotations(result.Member).AddAnnotationsFromParsing(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public AssemblyAnnotations([NotNull] Assembly assembly)
Assembly = assembly;
}

public void Add([NotNull] MemberAnnotations memberAnnotation)
{
if (memberAnnotation == null) throw new ArgumentNullException(nameof(memberAnnotation));

membersAnnotations.Add(memberAnnotation);
}

public void AddRange([NotNull] IEnumerable<MemberAnnotations> memberAnnotations)
{
if (memberAnnotations == null) throw new ArgumentNullException(nameof(memberAnnotations));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using ExternalAnnotationsGenerator.Core.Construction;
using JetBrains.Annotations;

namespace ExternalAnnotationsGenerator.Core.Model
Expand All @@ -17,5 +18,13 @@ public MemberAnnotations([NotNull] MemberInfo member)

Member = member;
}

internal void AddAnnotationsFromParsing([NotNull] ExpressionParsingResult result)
{
if (result == null) throw new ArgumentNullException(nameof(result));

Annotations.AddRange(result.Annotations);
ParameterAnnotations.AddRange(result.ParameterAnnotations);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void CanAnnotateStaticGetStringResult()
Assert.That(memberAnnotations, Is.Not.Null);
Assert.That(memberAnnotations.Member.Name, Is.EqualTo("GetString"));
Assert.That(resultInfo, Is.Not.Null);
Assert.That(resultInfo.IsNotNull, Is.False);
Assert.That(resultInfo.IsNotNull, Is.True);
Assert.That(resultInfo.CanBeNull, Is.False);
}

Expand All @@ -282,12 +282,12 @@ public void CanAnnotateStaticGetStringParameters()

var annotations = ((AnnotationsBuilder)annotator).GetAnnotations().First();
var memberAnnotations = annotations.FirstOrDefault();
var resultInfo = memberAnnotations?.Annotations.FirstOrDefault();
var resultInfo = memberAnnotations?.ParameterAnnotations.FirstOrDefault();

Assert.That(memberAnnotations, Is.Not.Null);
Assert.That(memberAnnotations.Member.Name, Is.EqualTo("GetString"));
Assert.That(resultInfo, Is.Not.Null);
Assert.That(resultInfo.IsNotNull, Is.False);
Assert.That(resultInfo.IsNotNull, Is.True);
Assert.That(resultInfo.CanBeNull, Is.False);
}

Expand Down

0 comments on commit a3b0772

Please sign in to comment.