/// <summary> /// Guesses the base type kind basing on the name of the base type. /// </summary> /// <param name="baseTypes"></param> /// <returns></returns> /// <remarks> /// /// </remarks> public static IEnumerable <BaseTypeInfo> SeparateClassAndInterfacesBasedOnNames(IEnumerable <BaseTypeSyntax> baseTypes) { if (!CanSeparateClassAndInterfacesBasedOnNames(baseTypes)) { throw new ArgumentException(nameof(baseTypes), "The provided collection of base types cannot be evaluated via this approach"); } var infos = new List <BaseTypeInfo>(); foreach (var baseType in baseTypes) { var helper = new BaseTypeReference(baseType); var name = helper.Name; var firstLetter = name[0]; if (firstLetter == 'I') { infos.Add(new BaseTypeInfo() { Node = baseType, Kind = Roslyn.TypeKind.Interface }); } else { infos.Add(new BaseTypeInfo() { Node = baseType, Kind = Roslyn.TypeKind.Class }); } } return(infos); }
private static void TestRetrieveTypeName(BaseTypeSyntax baseTypeNode, string expected) { Assert.IsNotNull(baseTypeNode, "Found node should be of type `{0}`!", typeof(BaseTypeSyntax).Name); BaseTypeReference baseTypeReference = new BaseTypeReference(baseTypeNode); string name = baseTypeReference.Name; Assert.IsNotNull(name, "Type name should not be null!"); Assert.AreNotEqual(string.Empty, name, "Type name should not be empty!"); Assert.AreEqual(expected, name, "Type name is not the one in source!"); }
/// <summary> /// Guesses the base type kind basing on name. /// </summary> /// <param name="baseType"></param> /// <returns></returns> public static Roslyn.TypeKind GuessBaseTypeKindFromName(BaseTypeSyntax baseType) { var helper = new BaseTypeReference(baseType); var name = helper.Name; var firstLetter = name[0]; if (firstLetter == 'I') { return(Roslyn.TypeKind.Interface); } return(Roslyn.TypeKind.Class); }
/// <summary> /// Parse the given descriptor into a type reference. /// </summary> private static TypeReference Parse(string descriptor) { if (descriptor.StartsWith("[")) { return(new ArrayTypeReference(Parse(descriptor.Substring(1)))); } if (descriptor.StartsWith("L") && descriptor.EndsWith(";")) { return(Parse(descriptor.Substring(1, descriptor.Length - 2))); } BaseTypeReference baseType; if ((descriptor.Length == 1) && BaseTypeReference.TryGetByCode(descriptor[0], out baseType)) { return(baseType); } return(new ObjectTypeReference(descriptor, null)); }
private static void TestRetrieveBaseClassName(ClassDeclarationSyntax classDeclarationNode, SemanticModel semanticModel, string expected) { Assert.IsNotNull(classDeclarationNode, "Found node should be of type `{0}`!", typeof(ClassDeclarationSyntax).Name); ClassDeclaration classDeclaration = new ClassDeclaration(classDeclarationNode, semanticModel); BaseTypeReference baseClass = classDeclaration.BaseClass; if (expected == null) { Assert.IsNull(baseClass, "Class name should be null!"); return; } string name = classDeclaration.BaseClass.Name; Assert.IsNotNull(name, "Class name should not be null!"); Assert.AreNotEqual(string.Empty, name, "Class name should not be empty!"); Assert.AreEqual(expected, name, "Base class name is not the one in source!"); }
/// <summary> /// Used for assessing whether <see cref="SeparateClassAndInterfacesBasedOnNames"/> can be used for separating class and interfaces. /// </summary> /// <param name="baseTypes"></param> /// <returns></returns> public static bool CanSeparateClassAndInterfacesBasedOnNames(IEnumerable <BaseTypeSyntax> baseTypes) { int nameStartsWithIBaseTypes = 0; int nameDoesNotStartWithIBaseTypes = 0; foreach (var baseType in baseTypes) { var helper = new BaseTypeReference(baseType); var name = helper.Name; var firstLetter = name[0]; if (firstLetter == 'I') { nameStartsWithIBaseTypes++; } else { nameDoesNotStartWithIBaseTypes++; } } return(nameDoesNotStartWithIBaseTypes <= 1); }
/// <summary> /// Guesses the base type kind basing on the name of the base type. /// </summary> /// <param name="baseTypes"></param> /// <returns></returns> public static IEnumerable <BaseTypeInfo> SeparateClassAndInterfacesBasedOnNames(IEnumerable <BaseTypeSyntax> baseTypes) { if (!CanSeparateClassAndInterfacesBasedOnNames(baseTypes)) { var names = baseTypes.Select(baseType => new BaseTypeReference(baseType).Name).Aggregate((i, j) => $"{i}; {j}"); throw new UnableToDiscriminateBasetypeCollectionByNameException( $"The provided collection of base types: [{names}] cannot be evaluated with this approach", new ArgumentException(nameof(baseTypes))); } var infos = new List <BaseTypeInfo>(); foreach (var baseType in baseTypes) { var helper = new BaseTypeReference(baseType); var name = helper.Name; var firstLetter = name[0]; if (firstLetter == 'I') { infos.Add(new BaseTypeInfo() { Node = baseType, Kind = Roslyn.TypeKind.Interface }); } else { infos.Add(new BaseTypeInfo() { Node = baseType, Kind = Roslyn.TypeKind.Class }); } } return(infos); }