| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Linq; |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.BuildTools; |
| | | 10 | | |
| | | 11 | | internal static class MethodSymbolExtensions |
| | | 12 | | { |
| | | 13 | | public static bool? IsGeneratedCode(this IMethodSymbol methodSymbol) |
| | | 14 | | => methodSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath.EndsWith(".g.cs"); |
| | | 15 | | |
| | | 16 | | public static bool IsMatchingUserProvidedMethod(this IMethodSymbol methodSymbol, IMethodSymbol userProvidedMethodSym |
| | | 17 | | { |
| | | 18 | | int parameterFound = 0; |
| | | 19 | | if (methodSymbol.Name != userProvidedMethodSymbol.Name) |
| | | 20 | | { |
| | | 21 | | return false; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | var parameters = methodSymbol.Parameters; |
| | | 25 | | |
| | | 26 | | for (int i = 0,j = 0; i < userProvidedMethodSymbol.Parameters.Length; i++) |
| | | 27 | | { |
| | | 28 | | IParameterSymbol parameterSymbol = userProvidedMethodSymbol.Parameters[i]; |
| | | 29 | | if (parameterSymbol.GetOneAttributeOf(coreWCFInjectedAttribute, fromServicesAttribute) is not null) |
| | | 30 | | { |
| | | 31 | | continue; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | if (parameterSymbol.IsMatchingParameter(parameters[j])) |
| | | 35 | | { |
| | | 36 | | j++; |
| | | 37 | | parameterFound++; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | return parameterFound == parameters.Length; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal static class ParameterSymbolExtensions |
| | | 46 | | { |
| | | 47 | | public static bool IsMatchingParameter(this IParameterSymbol symbol, IParameterSymbol parameterSymbol) |
| | | 48 | | => SymbolEqualityComparer.Default.Equals(symbol.Type, parameterSymbol.Type); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static class SymbolExtensions |
| | | 52 | | { |
| | | 53 | | public static AttributeData? GetOneAttributeOf(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols) |
| | | 54 | | { |
| | 3112 | 55 | | if (attributeTypeSymbols.Length == 0) |
| | | 56 | | { |
| | 0 | 57 | | return null; |
| | | 58 | | } |
| | | 59 | | |
| | 8492 | 60 | | foreach (var attribute in symbol.GetAttributes()) |
| | | 61 | | { |
| | 12438 | 62 | | foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null)) |
| | | 63 | | { |
| | 3390 | 64 | | if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, namedTypeSymbol)) |
| | | 65 | | { |
| | 2268 | 66 | | return attribute; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | 844 | 71 | | return null; |
| | 2268 | 72 | | } |
| | | 73 | | |
| | | 74 | | public static bool HasOneAttributeInheritFrom(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols) |
| | | 75 | | { |
| | 162 | 76 | | if (attributeTypeSymbols.Length == 0) |
| | | 77 | | { |
| | 0 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | 562 | 81 | | foreach (var attribute in symbol.GetAttributes()) |
| | | 82 | | { |
| | 350 | 83 | | foreach (var @interface in attribute.AttributeClass!.AllInterfaces) |
| | | 84 | | { |
| | 250 | 85 | | foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null)) |
| | | 86 | | { |
| | 56 | 87 | | if (SymbolEqualityComparer.Default.Equals(@interface, namedTypeSymbol)) |
| | | 88 | | { |
| | 30 | 89 | | return true; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 132 | 95 | | return false; |
| | 30 | 96 | | } |
| | | 97 | | |
| | | 98 | | public static bool IsPrivate(this ISymbol symbol) |
| | | 99 | | { |
| | 216 | 100 | | bool result = symbol.DeclaredAccessibility == Accessibility.Private; |
| | 216 | 101 | | if (!result && symbol.ContainingType != null) |
| | | 102 | | { |
| | 68 | 103 | | return symbol.ContainingType.IsPrivate(); |
| | | 104 | | } |
| | 148 | 105 | | return result; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | internal static class NamedTypeSymbolExtensions |
| | | 110 | | { |
| | | 111 | | public static bool IsPartial(this INamedTypeSymbol namedTypeSymbol, out INamedTypeSymbol parentType) |
| | | 112 | | { |
| | | 113 | | bool result = namedTypeSymbol.DeclaringSyntaxReferences.Select(static s => s.GetSyntax()).OfType<ClassDeclar |
| | | 114 | | if (result && namedTypeSymbol.ContainingType != null) |
| | | 115 | | { |
| | | 116 | | return namedTypeSymbol.ContainingType.IsPartial(out parentType); |
| | | 117 | | } |
| | | 118 | | parentType = namedTypeSymbol; |
| | | 119 | | return result; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | internal static class TypedConstantExtensions |
| | | 124 | | { |
| | | 125 | | public static string ToSafeCSharpString(this TypedConstant typedConstant) |
| | | 126 | | { |
| | | 127 | | if (typedConstant.Kind == TypedConstantKind.Array) |
| | | 128 | | { |
| | | 129 | | return $"new [] {typedConstant.ToCSharpString()}"; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | return typedConstant.ToCSharpString(); |
| | | 133 | | |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | internal static class StringExtensions |
| | | 138 | | { |
| | | 139 | | public static string ToMessagePropertyVariableName(this string propertyName) |
| | | 140 | | { |
| | | 141 | | string[] parts = propertyName.Split('.'); |
| | | 142 | | return parts.Last().ToCamelCase(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private static string ToCamelCase(this string propertyName) |
| | | 146 | | { |
| | | 147 | | return $"{char.ToLowerInvariant(propertyName[0])}{propertyName.Substring(1)}"; |
| | | 148 | | } |
| | | 149 | | } |