Commits

roundcrisis committed e2f5436

removed public and private methods, now they ahve visibility
same idea should apply to fields

Comments (0)

Files changed (6)

src/CodeShapes/ClassAnalyzer.cs

 		public IEnumerable<IClassShape> Analyze<T>()
 		{
 			var shapes = FindAllPrivateMethods(typeof(T))
-				.Concat(FindAllPublicMethods(typeof(T)))
+				.Concat(FindMethods(typeof(T)))
 				.Concat(FindFields(typeof(T))).ToArray();
 
 			var methods = shapes.Where(s => s is MethodShape).Cast<MethodShape>();
 		public IEnumerable<FieldShape> FindFields(Type type)
 		{
 			var fieldInfos = type.GetFields(BindingFlags.Public| BindingFlags.NonPublic| BindingFlags.Static| BindingFlags.Instance);
-			return fieldInfos.Select(fieldInfo => new FieldShape(fieldInfo.Name));
+			IList<FieldShape> fieldShapes = new List<FieldShape>();
+			foreach (var fieldInfo in fieldInfos)
+			{
+				var visibility = fieldInfo.IsPublic ? Visibility.Public : Visibility.NonPublic;
+				fieldShapes.Add(new FieldShape(fieldInfo.Name, visibility));
+			}
+			return fieldShapes.ToArray();
 		}
+		//todo: properties
+		private IEnumerable<IClassShape> FindMethods(Type type)
+		{
+			var privateProtectedInternalMethods = type.GetMethods( BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
+			var publicMethods = type.GetMethods();
 
-		private IEnumerable<IClassShape> FindAllPrivateMethods(Type type)
-		{
-			var methodInfos = type.GetMethods( BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
-			return methodInfos.Select(methodInfo => new PrivateMethodShape(methodInfo, methodInfo.Name));
-		}
-
-		private IEnumerable<IClassShape> FindAllPublicMethods(Type type)
-		{
-			return type.GetMethods().Select(methodInfo => new PublicMethodShape(methodInfo, methodInfo.Name)).ToList();
+			List<MethodShape> result = privateProtectedInternalMethods.Select(methodInfo => new MethodShape(methodInfo, methodInfo.Name, Visibility.NonPublic)).ToList();
+			result.AddRange(publicMethods.Select(methodInfo => new MethodShape(methodInfo, methodInfo.Name, Visibility.Public)));
+			return result;
 		}
 	}
 }

src/CodeShapes/Shapes/FieldShape.cs

 {
 	public class FieldShape : ClassShape
 	{
-		public FieldShape(string fieldName) : base(fieldName)
+		public Visibility Visibility { get; private set; }
+
+		public FieldShape(string fieldName, Visibility visibility) : base(fieldName)
 		{
+			Visibility = visibility;
 		}
 	}
 }

src/CodeShapes/Shapes/MethodShape.cs

 {
 	public class MethodShape : ClassShape
 	{
-		public MethodShape(MethodBase methodBase, string methodName)
+		public MethodShape(MethodBase methodBase, string methodName, MethodVisibility methodVisibility)
 			: base(methodName)
 		{
 			MethodBase = methodBase;
 			: base(methodName)
 		{}
 
-		public MethodBase MethodBase { get; set; }
+		public MethodBase MethodBase { get; private set; }
+
+		public MethodVisibility MethodVisibility { get; private set; }
 	}
+
+	public enum MethodVisibility
+	{
+		PublicMethod,
+		NonPublicMethod,
+		ProtectedMethod,
+		InternalMethod
+	}
+
 }

src/CodeShapes/Shapes/PrivateMethodShape.cs

-using System.Reflection;
-
-namespace CodeShapes.Shapes
-{
-	public class PrivateMethodShape : MethodShape
-	{
-		public PrivateMethodShape(MethodBase methodBase, string methodName)
-			: base(methodBase, methodName)
-		{
-		}
-	}
-}

src/CodeShapes/Shapes/PublicMethodShape.cs

-using System.Reflection;
-
-namespace CodeShapes.Shapes
-{
-	public class PublicMethodShape : MethodShape
-	{
-		public PublicMethodShape(MethodBase methodBase, string fieldName)
-			: base(methodBase, fieldName)
-		{
-		}
-	}
-}

src/CodeShapesTests/ClassAnalizerTests.cs

 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 
-			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(PublicMethodShape))));
+			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodShape)) && ((MethodShape)x).MethodVisibility == MethodVisibility.PublicMethod));
 		}
 
 		[Fact]
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 
-			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(PublicMethodShape))));
+			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodShape)) && ((MethodShape)x).MethodVisibility == MethodVisibility.NonPublicMethod));
 		}
 
 		[Fact]