private static bool ShouldMethodBeExcluded(MethodStatementSyntax methodStatement, SemanticModel semanticModel) { if (methodStatement.Modifiers.Any(SyntaxKind.MustOverrideKeyword) || methodStatement.Modifiers.Any(SyntaxKind.OverridableKeyword)) { return(true); } if (IsDllImport(methodStatement)) { return(true); } var methodSymbol = semanticModel.GetDeclaredSymbol(methodStatement); if (methodSymbol != null && methodSymbol.IsOverride && methodSymbol.OverriddenMethod != null && methodSymbol.OverriddenMethod.IsMustOverride()) { return(true); } return(methodSymbol.IsOverrides() && semanticModel.Compilation.IsTest()); }
private async Task <Document> AddSealedKeywordAsync(Document document, MethodStatementSyntax methodDeclaration, CancellationToken cancellationToken) { var generator = SyntaxGenerator.GetGenerator(document); DeclarationModifiers modifiers = DeclarationModifiers.None; if (methodDeclaration.Modifiers.Any(SyntaxKind.NewKeyword)) { modifiers |= DeclarationModifiers.New; } if (methodDeclaration.Modifiers.Any(SyntaxKind.OverridesKeyword)) { modifiers |= DeclarationModifiers.Override; } modifiers |= DeclarationModifiers.Sealed; var newMethodDeclaration = generator.WithModifiers(methodDeclaration, modifiers); var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration); // Return document with transformed tree. return(document.WithSyntaxRoot(newRoot)); }
public override void VisitMethodStatement(MethodStatementSyntax node) { //MethodNames.Add(node.ToString()); MethodNames.Add(node.Identifier.ToString()); base.VisitMethodStatement(node); }
public override SyntaxNode VisitMethodStatement(MethodStatementSyntax node) { if (IsInterfaceMember(node)) { return(node); } return(EnsureVisibility(node, node.Modifiers, (x, l) => x.WithModifiers(l), () => SyntaxKind.PublicKeyword)); }
public override void VisitMethodStatement(MethodStatementSyntax node) { if (_targetPatternRegEx.Match(node.Identifier.ToString()).Success) { RecordMatchAndContext(node, BlockType.None); } base.VisitMethodStatement(node); }
internal static bool IsEventHandler(MethodStatementSyntax declaration, SemanticModel semanticModel) { if (declaration.HandlesClause != null) { return true; } var symbol = semanticModel.GetDeclaredSymbol(declaration); return symbol != null && symbol.IsProbablyEventHandler(); }
internal static bool IsEventHandler(MethodStatementSyntax declaration, SemanticModel semanticModel) { if (declaration.HandlesClause != null) { return(true); } var symbol = semanticModel.GetDeclaredSymbol(declaration); return(symbol != null && symbol.IsEventHandler()); }
private static bool IsDllImport(MethodStatementSyntax methodStatement) => methodStatement.AttributeLists .SelectMany(list => list.Attributes) .Any(a => a.Name.GetText().ToString().Equals("dllimport", System.StringComparison.OrdinalIgnoreCase));
static ExpressionSyntax GetDefaultValueExpression(SemanticModel semanticModel, MethodStatementSyntax methodStatement, TypeSyntax type) { var typeSymbol = semanticModel.GetTypeInfo(type).Type; if (typeSymbol == null) { return(null); } if (typeSymbol.IsArrayType()) { return(SyntaxFactory.ObjectCreationExpression(type)); } if (typeSymbol.IsEnumType()) { var members = type.GetMembers().OfType <EnumMemberDeclarationSyntax>(); if (!members.Any()) { return(GetDefaultTypeExpression(type)); } return(SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.Token(SyntaxKind.DotToken), SyntaxFactory.IdentifierName(members.First().Identifier))); } if (typeSymbol.IsKind(SymbolKind.DynamicType)) { return(GetNothingExpression()); } if (typeSymbol.IsReferenceType) { if (typeSymbol.GetTypeParameters().Any() && (methodStatement.TypeParameterList != null) && methodStatement.TypeParameterList.Parameters.Any()) { return(GetDefaultTypeExpression(type)); } return(GetNothingExpression()); } switch (typeSymbol.SpecialType) { case SpecialType.System_Boolean: return(SyntaxFactory.FalseLiteralExpression(SyntaxFactory.Token(SyntaxKind.FalseKeyword))); case SpecialType.System_Char: return(SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal('\0'))); case SpecialType.System_SByte: case SpecialType.System_Byte: case SpecialType.System_Int16: case SpecialType.System_UInt16: case SpecialType.System_Int32: case SpecialType.System_UInt32: case SpecialType.System_Int64: case SpecialType.System_UInt64: case SpecialType.System_Single: case SpecialType.System_Double: case SpecialType.System_Decimal: return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); } if (typeSymbol.IsNullableType()) { return(GetNothingExpression()); } if (typeSymbol.IsValueType) { return(SyntaxFactory.ObjectCreationExpression(type)); } return(GetDefaultTypeExpression(type)); }
// Interface のメソッドの時とか // Windows API 系(DllImport) public override void VisitMethodStatement(MethodStatementSyntax statementNode) { // VisitMethodBlock() から来た場合は、二重登録になってしまうので飛ばす if (statementNode.Parent is MethodBlockSyntax) { base.VisitMethodStatement(statementNode); return; } var isWinAPI = statementNode.AttributeLists.Any(x => x.ToString().Contains("DllImport")); var isSubMethod = statementNode.ChildTokens().Any(x => x.Kind() == SyntaxKind.SubKeyword); var isPartial = statementNode.ChildTokens().Any(x => x.Kind() == SyntaxKind.PartialKeyword); var defineName = statementNode.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString(); // ジェネリック型を定義している場合 if (statementNode.ChildNodes().OfType <TypeParameterListSyntax>().Any()) { var listNode = statementNode.ChildNodes().OfType <TypeParameterListSyntax>().FirstOrDefault(); var genericTypes = listNode .ChildNodes() .OfType <TypeParameterSyntax>() .Select(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).ToString()); defineName = $"{defineName}<{string.Join(", ", genericTypes)}>"; } var methodArguments = new List <MethodArgument>(); if (statementNode.ChildNodes().OfType <ParameterListSyntax>().FirstOrDefault().ChildNodes().Any()) { var listNode = statementNode.ChildNodes().OfType <ParameterListSyntax>().FirstOrDefault(); methodArguments = GetMethodArguments(listNode); } var returnType = string.Empty; if (!isSubMethod) { var asNode = statementNode.ChildNodes().FirstOrDefault(x => x is SimpleAsClauseSyntax); returnType = asNode.ChildNodes().FirstOrDefault().ToString(); } var startLength = statementNode.Span.Start; var endLength = statementNode.Span.End; var parentNamespace = GetNamespace(DefineKinds.Method, startLength, endLength); UserDefinitions.Add(new UserDefinition { DefineKinds = isWinAPI ? DefineKinds.WindowsAPI : DefineKinds.Method, IsPartial = isPartial, Namespace = parentNamespace, DefineName = defineName, DefineFullName = $"{parentNamespace}.{defineName}", MethodArguments = methodArguments, ReturnType = ConvertCSharpType(returnType), SourceFile = SourceFile, StartLength = startLength, EndLength = endLength, }); base.VisitMethodStatement(statementNode); }
public override void VisitMethodStatement(MethodStatementSyntax node) => TryAddPublicApi(node);