2
// CreateClassDeclarationAction.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.Semantics;
30
using ICSharpCode.NRefactory.TypeSystem;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[ContextAction("Create class", Description = "Creates a class declaration out of an object creation.")]
35
public class CreateClassDeclarationAction : ICodeActionProvider
37
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
39
var simpleType = context.GetNode<SimpleType>();
40
if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
41
return GetActions(context, simpleType);
43
var createExpression = context.GetNode<ObjectCreateExpression>();
44
if (createExpression != null)
45
return GetActions(context, createExpression);
47
var identifier = context.GetNode<IdentifierExpression>();
48
if (identifier != null && (identifier.Parent is MemberReferenceExpression))
49
return GetActions(context, identifier);
51
return Enumerable.Empty<CodeAction>();
54
static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
56
var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
57
if (resolveResult == null)
60
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
61
if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class)) {
64
ClassType classType = GuessClassTypeByName(service, node);
65
ModifyClassTypeBasedOnTypeGuessing(context, node, ref classType);
69
case ClassType.Struct:
70
message = context.TranslateString("Create struct");
72
case ClassType.Interface:
73
message = context.TranslateString("Create interface");
76
message = context.TranslateString("Create class");
79
yield return new CodeAction(message, script => {
80
script.CreateNewType(CreateType(context, service, node, classType));
83
if (node.Parent is TypeDeclaration || classType != ClassType.Class)
85
yield return new CodeAction(context.TranslateString("Create nested class"), script => {
86
script.InsertWithCursor(
87
context.TranslateString("Create nested class"),
88
Script.InsertPosition.Before,
89
CreateType(context, service, node, classType)
94
static void ModifyClassTypeBasedOnTypeGuessing(RefactoringContext context, AstNode node, ref ClassType classType)
96
var guessedType = CreateFieldAction.GuessType(context, node);
97
if (guessedType.Kind == TypeKind.TypeParameter) {
98
var tp = (ITypeParameter)guessedType;
99
if (tp.HasValueTypeConstraint)
100
classType = ClassType.Struct;
101
if (tp.HasReferenceTypeConstraint)
102
classType = ClassType.Class;
106
static ClassType GuessClassTypeByName(NamingConventionService service, string identifier)
109
return ClassType.Class;
110
if (service.IsValidName (identifier, AffectedEntity.Interface, Modifiers.Public))
111
return ClassType.Interface;
112
if (!service.IsValidName (identifier, AffectedEntity.Class, Modifiers.Public) &&
113
service.IsValidName (identifier, AffectedEntity.Struct, Modifiers.Public))
114
return ClassType.Struct;
115
return ClassType.Class;
118
static ClassType GuessClassTypeByName(NamingConventionService service, AstNode node)
120
if (node is SimpleType)
121
return GuessClassTypeByName (service, ((SimpleType)node).Identifier);
122
if (node is IdentifierExpression)
123
return GuessClassTypeByName (service, ((IdentifierExpression)node).Identifier);
124
return ClassType.Class;
127
static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node, ClassType classType)
129
TypeDeclaration result;
130
if (node is SimpleType) {
131
result = CreateClassFromType(context, classType, (SimpleType)node);
132
} else if (node is ObjectCreateExpression) {
133
result = CreateClassFromObjectCreation(context, (ObjectCreateExpression)node);
135
result = CreateClassFromIdentifier(context, classType, (IdentifierExpression)node);
138
return AddBaseTypesAccordingToNamingRules(context, service, result);
141
static TypeDeclaration CreateClassFromIdentifier(RefactoringContext context, ClassType classType, IdentifierExpression identifierExpression)
143
var result = new TypeDeclaration { Name = identifierExpression.Identifier, ClassType = classType };
144
var entity = identifierExpression.GetParent<EntityDeclaration>();
146
result.Modifiers |= entity.Modifiers & Modifiers.Public;
150
static TypeDeclaration CreateClassFromType(RefactoringContext context, ClassType classType, SimpleType simpleType)
152
TypeDeclaration result;
153
string className = simpleType.Identifier;
155
if (simpleType.Parent is Attribute && classType == ClassType.Class) {
156
if (!className.EndsWith("Attribute", System.StringComparison.Ordinal))
157
className += "Attribute";
160
result = new TypeDeclaration { Name = className, ClassType = classType };
161
var entity = simpleType.GetParent<EntityDeclaration>();
163
result.Modifiers |= entity.Modifiers & Modifiers.Public;
165
var guessedType = CreateFieldAction.GuessType (context, simpleType);
166
if (guessedType.Kind == TypeKind.TypeParameter)
167
ImplementConstraints (context, result, (ITypeParameter)guessedType);
171
static void ImplementConstraints(RefactoringContext context, TypeDeclaration result, ITypeParameter tp)
173
if (tp.HasValueTypeConstraint)
174
result.ClassType = ClassType.Struct;
175
if (tp.HasReferenceTypeConstraint)
176
result.ClassType = ClassType.Class;
177
if (tp.HasDefaultConstructorConstraint)
178
result.AddChild (new ConstructorDeclaration { Modifiers = Modifiers.Public, Body = new BlockStatement () }, Roles.TypeMemberRole);
179
foreach (var baseType in tp.DirectBaseTypes) {
180
if (baseType.Namespace == "System") {
181
if (baseType.Name == "Object" || baseType.Name == "ValueType")
184
result.BaseTypes.Add (context.CreateShortType (baseType));
188
static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
190
TypeDeclaration result;
191
string className = createExpression.Type.GetText();
192
if (!createExpression.Arguments.Any()) {
193
result = new TypeDeclaration { Name = className };
195
var decl = new ConstructorDeclaration {
197
Modifiers = Modifiers.Public,
198
Body = new BlockStatement {
199
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
202
result = new TypeDeclaration {
208
decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
210
var guessedType = CreateFieldAction.GuessType(context, createExpression);
211
if (guessedType.Kind == TypeKind.Interface || guessedType.Kind == TypeKind.Class && guessedType.GetDefinition ().IsAbstract) {
212
result.BaseTypes.Add(context.CreateShortType(guessedType));
213
AddImplementation(context, result, guessedType);
219
static Modifiers GetModifiers(IEntity property)
221
if (property.DeclaringType.Kind == TypeKind.Interface)
222
return Modifiers.Public;
223
switch (property.Accessibility) {
224
case Accessibility.Public:
225
return Modifiers.Public | Modifiers.Override;
226
case Accessibility.Protected:
227
return Modifiers.Protected | Modifiers.Override;
228
case Accessibility.Internal:
229
return Modifiers.Internal | Modifiers.Override;
230
case Accessibility.ProtectedOrInternal:
232
return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
233
case Accessibility.ProtectedAndInternal:
235
return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
237
return Modifiers.Override;
240
static void AddImplementation(RefactoringContext context, TypeDeclaration result, IType guessedType)
242
foreach (var property in guessedType.GetProperties ()) {
243
if (!property.IsAbstract)
245
if (property.IsIndexer) {
246
var indexerDecl = new IndexerDeclaration {
247
ReturnType = context.CreateShortType(property.ReturnType),
248
Modifiers = GetModifiers(property),
251
indexerDecl.Parameters.AddRange(ConvertParameters(context, property.Parameters));
253
indexerDecl.Getter = new Accessor();
255
indexerDecl.Setter = new Accessor();
256
result.AddChild(indexerDecl, Roles.TypeMemberRole);
259
var propDecl = new PropertyDeclaration {
260
ReturnType = context.CreateShortType(property.ReturnType),
261
Modifiers = GetModifiers (property),
265
propDecl.Getter = new Accessor();
267
propDecl.Setter = new Accessor();
268
result.AddChild(propDecl, Roles.TypeMemberRole);
271
foreach (var method in guessedType.GetMethods ()) {
272
if (!method.IsAbstract)
274
var decl = new MethodDeclaration {
275
ReturnType = context.CreateShortType(method.ReturnType),
276
Modifiers = GetModifiers (method),
278
Body = new BlockStatement {
279
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
282
decl.Parameters.AddRange(ConvertParameters(context, method.Parameters));
283
result.AddChild(decl, Roles.TypeMemberRole);
286
foreach (var evt in guessedType.GetEvents ()) {
289
var decl = new EventDeclaration {
290
ReturnType = context.CreateShortType(evt.ReturnType),
291
Modifiers = GetModifiers (evt),
293
new VariableInitializer {
298
decl.Variables.Add(new VariableInitializer(evt.Name));
299
result.AddChild(decl, Roles.TypeMemberRole);
303
static IEnumerable<ParameterDeclaration> ConvertParameters(RefactoringContext context, IList<IParameter> parameters)
305
foreach (var param in parameters) {
306
ParameterModifier mod = ParameterModifier.None;
308
mod = ParameterModifier.Out;
309
} else if (param.IsRef) {
310
mod = ParameterModifier.Ref;
311
} else if (param.IsParams) {
312
mod = ParameterModifier.Params;
314
yield return new ParameterDeclaration(context.CreateShortType(param.Type), param.Name, mod);
318
static TypeDeclaration AddBaseTypesAccordingToNamingRules(RefactoringContext context, NamingConventionService service, TypeDeclaration result)
320
if (service.HasValidRule(result.Name, AffectedEntity.CustomAttributes, Modifiers.Public)) {
321
result.BaseTypes.Add(context.CreateShortType("System", "Attribute"));
322
} else if (service.HasValidRule(result.Name, AffectedEntity.CustomEventArgs, Modifiers.Public)) {
323
result.BaseTypes.Add(context.CreateShortType("System", "EventArgs"));
324
} else if (service.HasValidRule(result.Name, AffectedEntity.CustomExceptions, Modifiers.Public)) {
325
result.BaseTypes.Add(context.CreateShortType("System", "Exception"));
b'\\ No newline at end of file'