public static void Initialize(CSharpGeneratorContext context) { IDeclaredType byteType = TypeFactory.CreateTypeByCLRName(PredefinedType.BYTE_FQN, context.PsiModule, context.Anchor.GetResolveContext()); _byteArrayType = TypeFactory.CreateArrayType(byteType, 1); _conversionRule = new CSharpTypeConversionRule(context.PsiModule); }
public override bool Accepts(IDeclaredElement declaredElement, ISubstitution substitution) { var method = declaredElement as IMethod; if (method == null) { return(false); } if (!Equals(myMethodSignature.ReturnType, method.ReturnType)) { return(false); } if (myMethodSignature.Parameters.Length != method.Parameters.Count) { return(false); } for (var i = 0; i < method.Parameters.Count; i++) { if (!Equals(myMethodSignature.Parameters[i].Type, method.Parameters[i].Type)) { ITypeConversionRule rule = method.Module.GetTypeConversionRule(); if (!rule.IsImplicitlyConvertibleTo(method.Parameters[i].Type, myMethodSignature.Parameters[i].Type)) { return(false); } } } return(true); }
public void Generate(CSharpGeneratorContext context, IList <GeneratorDeclaredElement <ITypeOwner> > elements, CSharpElementFactory factory) { IMethodDeclaration methodDeclaration = null; ITypeElement declaredElement = context.ClassDeclaration.DeclaredElement; if (declaredElement != null) { IDeclaredType byteType = TypeFactory.CreateTypeByCLRName(PredefinedType.BYTE_FQN, context.PsiModule, context.Anchor.GetResolveContext()); _byteArrayType = TypeFactory.CreateArrayType(byteType, 1); _typeConversionRule = new CSharpTypeConversionRule(context.PsiModule); IOverridableMember member = declaredElement.Methods.FirstOrDefault(ValidateMethod); if (member != null) { methodDeclaration = (IMethodDeclaration)member.GetDeclarations().FirstOrDefault(); Generate(context, methodDeclaration, elements, factory); return; } } string method = String.Format("public Int32 {0}(Byte[] bytes, Int32 startIndex)", MethodName); methodDeclaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(method); Generate(context, methodDeclaration, elements, factory); context.PutMemberDeclaration(methodDeclaration, null, newDeclaration => new GeneratorDeclarationElement(newDeclaration)); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var callTreeNode = error?.Reference.GetTreeNode(); if (callTreeNode == null) { throw new ArgumentNullException(); } if (callMethod == null || !(callTreeNode is IReferenceExpression callExpression) || !(callTreeNode.Parent is IInvocationExpression ctorArgumentOwner)) { return(null); } var callParams = callMethod.Parameters; if (callParams.Count == 0) { return(null); } var psiModule = error.Reference.GetAccessContext().GetPsiModule(); cSharpTypeConversionRule = psiModule.GetTypeConversionRule(); var factory = CSharpElementFactory.GetInstance(psiModule); var methodDeclaration = callTreeNode.FindParent <IMethodDeclaration>(); var existedArguments = ctorArgumentOwner.AllArguments(false) .Select(x => new ArgumentInfo { Type = x.GetExpressionType()?.ToIType(), Expression = (x as ICSharpArgument)?.Value, IsCSharpArgument = x is ICSharpArgument }) .ToArray(); var mockInfos = GenerateNewMockInfos(callParams, existedArguments, factory); AddMocksToClassDeclaration(methodDeclaration, ctorArgumentOwner, mockInfos); var argExpressions = GetCtorArgumentExpressions(existedArguments, callParams); var argumentsPattern = string.Join(", ", Enumerable.Range(2, argExpressions.Length).Select(x => $"${x}")); var newExpression = factory.CreateExpression($"$0.$1({argumentsPattern});", new[] { callExpression.FirstChild, callExpression.LastChild }.Concat(argExpressions).ToArray()); ctorArgumentOwner.ReplaceBy(newExpression); AddUsings(mockInfos, factory); return(null); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var ctorTreeNode = error?.Reference.GetTreeNode(); if (ctor == null || !(ctorTreeNode is IObjectCreationExpression ctorExpression)) { return(null); } var ctorParams = ctor.Parameters; if (ctorParams.Count == 0) { return(null); } var psiModule = error.Reference.GetAccessContext().GetPsiModule(); cSharpTypeConversionRule = psiModule.GetTypeConversionRule(); var factory = CSharpElementFactory.GetInstance(psiModule); var methodDeclaration = ctorTreeNode.FindParent <IMethodDeclaration>(); var existedArguments = ctorExpression.AllArguments(false) .Select(x => new ArgumentInfo { Type = x.GetExpressionType()?.ToIType(), Expression = (x as ICSharpArgument)?.Value, IsCSharpArgument = x is ICSharpArgument }) .ToArray(); var superClassTypes = superTypes.Select(x => x.GetClassType()).Where(x => x != null).ToArray(); var mockInfos = GenerateNewMockInfos(ctorParams, superClassTypes, existedArguments, factory); AddMocksToClassDeclaration(methodDeclaration, ctorExpression, mockInfos, classDeclaration, factory); var argExpressions = GetCtorArgumentExpressions(existedArguments, ctorParams, superClassTypes); var argumentsPattern = string.Join(", ", Enumerable.Range(1, ctorParams.Count).Select(x => $"${x}")); var newExpression = factory.CreateExpression($"new $0({argumentsPattern});", argExpressions.ToArray()); ctorExpression.ReplaceBy(newExpression); return(null); }
private static bool CheckSingleType(PredefinedType predefinedType, IPsiModule psiModule, IType type, ITypeConversionRule conversionRule, IType typeInDeclaration) { if (!type.IsGenericOrNonIEnumerable()) { if (!type.IsSubtypeOf(predefinedType.Array)) return true; IDeclaredType scalarType = type.GetScalarType(); return scalarType != null && scalarType.IsImplicitlyConvertibleTo(typeInDeclaration, conversionRule); } IDeclaredType ienumerableOf = CollectionTypeUtil.CreateIEnumerableOf(psiModule, typeInDeclaration); return ienumerableOf == null || type.IsImplicitlyConvertibleTo(ienumerableOf, conversionRule); }