private string GenerateExceptionCreationExpression(ObjectCreationExpressionSyntax exceptionCreationExpressionSyntax, SemanticModel semanticModel) { var exceptionType = (INamedTypeSymbol)ModelExtensions.GetTypeInfo(semanticModel, exceptionCreationExpressionSyntax).Type; string exceptionTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(exceptionType); string generatedExceptionName = ""; if (exceptionTypeFullName == "System.ArgumentNullException") { generatedExceptionName = "IllegalArgumentException"; } else if (exceptionTypeFullName == "System.ArgumentException") { generatedExceptionName = "IllegalArgumentException"; } else if (exceptionTypeFullName == "System.NullReferenceException") { generatedExceptionName = "NullPointerException"; } else if (exceptionTypeFullName == "System.InvalidOperationException") { generatedExceptionName = "IllegalStateException"; } else { throw new NotSupportedException(); } string argumentList = ArgumentListGenerator.Generate(exceptionCreationExpressionSyntax.ArgumentList, semanticModel); return("new " + generatedExceptionName + argumentList); }
public static bool IsCustom(INamedTypeSymbol accessingType) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(accessingType); if (accessingTypeFullName == "System.String") { return(true); } return(false); }
public static string Generate(INamedTypeSymbol containingType, string ownerExpression, string propertyName) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(containingType); return(Generate(accessingTypeFullName, ownerExpression, propertyName)); }
public static string Generate(INamedTypeSymbol containingType, string ownerExpression, string methodName, List <string> parameterExpressions) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(containingType); return(Generate(accessingTypeFullName, ownerExpression, methodName, parameterExpressions)); }
public static bool IsCustom(INamedTypeSymbol leftOperandType, string operatorString) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(leftOperandType); if (accessingTypeFullName == "System.String") { if (operatorString == "==") { return(true); } } return(false); }
public static string Generate(INamedTypeSymbol accessingType, string accessingExpression, string generatedArgument, ElementAccessExpressionSyntax elementAccessExpressionSyntax, SemanticModel semanticModel) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(accessingType); if (accessingTypeFullName == "System.String") { return(accessingExpression + ".charAt(" + generatedArgument + ")"); } throw new ArgumentException(); }
private bool IsExceptionType(INamedTypeSymbol typeSymbol) { string baseExceptionType = "System.Exception"; while (typeSymbol != null) { string typeFullName = SyntaxTreeHelper.GetFullyQualifiedName(typeSymbol); if (typeFullName == baseExceptionType) { return(true); } typeSymbol = typeSymbol.BaseType; } return(false); }
public static string Generate(INamedTypeSymbol leftOperandType, string leftExpression, string operatorString, string rightExpression, SemanticModel semanticModel) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(leftOperandType); if (accessingTypeFullName == "System.String") { if (operatorString == "==") { return("by.besmart.cross.SystemStringUtils.equals(" + leftExpression + ", " + rightExpression + ")"); } } throw new ArgumentException(); }
public static bool IsCustom(INamedTypeSymbol containingType, string methodName) { string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(containingType); return(IsCustom(accessingTypeFullName, methodName)); }
private string GenerateInvocationExpression(InvocationExpressionSyntax invocationExpressionSyntax, SemanticModel semanticModel) { var parameterExpressions = new List <string>(); foreach (var argument in invocationExpressionSyntax.ArgumentList.Arguments) { string parameterExpression = GenerateExpression(argument.Expression, semanticModel); parameterExpressions.Add(parameterExpression); } var expression = invocationExpressionSyntax.Expression; var symbolInfo = ModelExtensions.GetSymbolInfo(semanticModel, expression); if (expression is IdentifierNameSyntax) { var identifierNameExpression = expression as IdentifierNameSyntax; var namedTypeSymbol = (INamedTypeSymbol)ModelExtensions.GetTypeInfo(semanticModel, expression).Type; if (namedTypeSymbol != null && TypeReferenceGenerator.IsDelegateType(namedTypeSymbol)) { return(identifierNameExpression.Identifier.ValueText + "." + GenerateMethodCallExpression("invoke", parameterExpressions)); } string containingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(symbolInfo.Symbol.ContainingType); string methodName = invocationExpressionSyntax.Expression.GetText() .ToString().Trim(); return(GenerateMethodCallExpression(methodName, parameterExpressions)); } if (expression is MemberAccessExpressionSyntax) { var memberAccessExpression = expression as MemberAccessExpressionSyntax; string memberName = memberAccessExpression.Name.Identifier.ValueText; string ownerExpression = GenerateExpression(memberAccessExpression.Expression, semanticModel); if (symbolInfo.Symbol is IMethodSymbol && (symbolInfo.Symbol as IMethodSymbol).IsExtensionMethod) { var containingType = (symbolInfo.Symbol as IMethodSymbol).ContainingType; string containingTypeReference = TypeReferenceGenerator.GenerateTypeReference(containingType, semanticModel).Text; parameterExpressions.Insert(0, ownerExpression); return(containingTypeReference + "." + GenerateMethodCallExpression(memberName, parameterExpressions)); } if (symbolInfo.Symbol.Kind == SymbolKind.Method) { string containingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(symbolInfo.Symbol.ContainingType); string methodName = memberName; if (CustomMethodInvocationHelper.IsCustom(containingTypeFullName, methodName)) { return(CustomMethodInvocationHelper.Generate(containingTypeFullName, ownerExpression, methodName, parameterExpressions)); } else { return(ownerExpression + "." + GenerateMethodCallExpression(methodName, parameterExpressions)); } } if (symbolInfo.Symbol.Kind == SymbolKind.Property && symbolInfo.Symbol is IPropertySymbol) { var propertyType = (symbolInfo.Symbol as IPropertySymbol).Type as INamedTypeSymbol; if (TypeReferenceGenerator.IsDelegateType(propertyType)) { string propertyName = memberAccessExpression.Name.Identifier.ValueText; var type = (INamedTypeSymbol)ModelExtensions.GetTypeInfo(semanticModel, memberAccessExpression).Type; var typeReference = TypeReferenceGenerator.GenerateTypeReference(type, semanticModel); string methodName = PropertyGenerator.GenerateGetterMethodName(typeReference, propertyName); return(ownerExpression + "." + GenerateMethodCallExpression(methodName, new List <string>()) + "." + GenerateMethodCallExpression("invoke", parameterExpressions)); } throw new NotImplementedException(); } throw new NotImplementedException(); } throw new NotImplementedException(); }
public TypeReference GenerateTypeReference(ITypeSymbol typeSymbol, SemanticModel semanticModel, bool isInGenericContext = false) { if (typeSymbol is ITypeParameterSymbol) { return(new TypeReference { Text = GenericTypeReferenceBuilder.BuildReference(typeSymbol as ITypeParameterSymbol), IsGeneric = true, IsReferenceType = typeSymbol.IsReferenceType }); } var namedTypeSymbol = (INamedTypeSymbol)typeSymbol; string fullyQualifiedName = SyntaxTreeHelper.GetFullyQualifiedName(namedTypeSymbol); var fullyQualifiedPropertyTypeNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(namedTypeSymbol); var typeParameters = new List <TypeReference>(); if (namedTypeSymbol.IsGenericType) { foreach (var typeArgument in namedTypeSymbol.TypeArguments) { var typeReference = GenerateTypeReference(typeArgument, semanticModel, isInGenericContext: true); typeParameters.Add(typeReference); } } if (PredefinedTypes.IsPredefined(fullyQualifiedName)) { return(new TypeReference { Text = isInGenericContext ? PredefinedTypes.GetInGenericContext(fullyQualifiedName) : PredefinedTypes.Get(fullyQualifiedName), IsPredefined = true, IsReferenceType = typeSymbol.IsReferenceType }); } if (IsCustomImplementedType(fullyQualifiedName)) { if (namedTypeSymbol.IsGenericType) { } string typeReferenceText = "by.misharp." + (namedTypeSymbol.IsGenericType ? TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts, typeParameters) : TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts)); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } if (IsDelegateType(namedTypeSymbol)) { return(BuildDelegateReference(namedTypeSymbol, semanticModel)); } if (namedTypeSymbol.IsGenericType) { string typeReferenceText = TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts, typeParameters); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } else { string typeReferenceText = TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } }
public bool IsDelegateType(INamedTypeSymbol namedTypeSymbol) { string fullName = SyntaxTreeHelper.GetFullyQualifiedName(namedTypeSymbol); return(fullName == "System.Action" || fullName == "System.Func"); }