public static void MoveToMethodWithFieldIntroduction([NotNull] IClassDeclaration classDeclaration, [NotNull] IMethodDeclaration methodDeclaration, [NotNull] ICSharpExpression expression, string fieldName = null) { classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed(); var result = GetDeclaredElementFromParentDeclaration(expression); var factory = CSharpElementFactory.GetInstance(classDeclaration); var type = expression.Type(new ResolveContext(classDeclaration.GetPsiModule())); if (type.IsUnknown) { type = TypeFactory.CreateTypeByCLRName("System.Object", classDeclaration.GetPsiModule()); } var isVoid = type.IsVoid(); if (!isVoid) { var baseName = fieldName ?? CreateBaseName(expression, result); var name = NamingUtil.GetUniqueName(expression, baseName, NamedElementKinds.PrivateInstanceFields, baseName == null ? collection => { collection.Add(expression.Type(), new EntryOptions()); } : (Action <INamesCollection>)null, de => !de.Equals(result)); var field = factory.CreateFieldDeclaration(type, name); field.SetAccessRights(AccessRights.PRIVATE); classDeclaration.AddClassMemberDeclaration(field); var initialization = factory.CreateStatement("$0 = $1;", name, expression.CopyWithResolve()); var body = methodDeclaration.EnsureStatementMemberBody(); body.AddStatementAfter(initialization, null); RenameOldUsages(expression, result, name, factory); } else { var initialization = factory.CreateStatement("$0;", expression.CopyWithResolve()); var body = methodDeclaration.EnsureStatementMemberBody(); body.AddStatementAfter(initialization, null); ExpressionStatementNavigator.GetByExpression(expression).NotNull("statement != null").RemoveOrReplaceByEmptyStatement(); } }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var factory = CSharpElementFactory.GetInstance(_theclass.GetPsiModule()); if (factory == null) { return(null); } AddProxyClass(_theclass, factory); return(null); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { AttributeUtil.AddAttributeToSingleDeclaration(myClassDeclaration, KnownTypes.RequireComponent, new[] { new AttributeValue(myType) }, myClassDeclaration.GetPsiModule(), myFactory, true); return(null); }
/// <summary> /// Executes the specified class declaration. /// </summary> /// <param name="classDeclaration"> /// The class declaration. /// </param> private static void Execute(IClassDeclaration classDeclaration) { var factory = CSharpElementFactory.GetInstance(classDeclaration.GetPsiModule()); if (factory == null) { return; } AddProxyClass(classDeclaration, factory); }
/// <summary> /// Executes the specified class declaration. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="classDeclaration"> /// The class declaration. /// </param> private static void Execute(ISolution solution, IClassDeclaration classDeclaration) { var factory = CSharpElementFactory.GetInstance(classDeclaration.GetPsiModule()); if (factory == null) { return; } AddInterface(solution, classDeclaration); AddAttribute(solution, classDeclaration, factory); AddConstructor(classDeclaration, factory); AddGetObjectDataMethod(classDeclaration, factory); }