Skip to content

Commit c680295

Browse files
authored
21.1.3 seald class modifires (#9)
* SealdClassModifires * FixTest * AddAppendIf * AppendRefactoring * Revert "AppendRefactoring" This reverts commit 5c32436. * Refactoring
1 parent b34d947 commit c680295

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

DevExpress.Mvvm.CodeGenerators.Tests/TestSource/GenerationTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ public object? ParentViewModel {
313313
IServiceContainer? serviceContainer;
314314
protected IServiceContainer ServiceContainer { get => serviceContainer ??= new ServiceContainer(this); }
315315
IServiceContainer ISupportServices.ServiceContainer { get => ServiceContainer; }
316-
T? GetService<T>() where T : class => ServiceContainer.GetService<T>();
316+
protected T? GetService<T>() where T : class => ServiceContainer.GetService<T>();
317+
protected T? GetRequiredService<T>() where T : class => ServiceContainer.GetRequiredService<T>();
317318
318319
protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
319320
protected void RaisePropertyChanging(PropertyChangingEventArgs e) => PropertyChanging?.Invoke(this, e);
@@ -406,6 +407,23 @@ public DelegateCommand<int> Command3Command {
406407

407408
}
408409
#endif
410+
[Test]
411+
public void PrivateInSealedClass() {
412+
const string source =
413+
@"using DevExpress.Mvvm.CodeGenerators;
414+
415+
namespace Test {
416+
[GenerateViewModel(ImplementINotifyPropertyChanging = true, ImplementISupportParentViewModel = true, ImplementISupportServices = true, ImplementIDataErrorInfo = true)]
417+
sealed partial class SealdClass {
418+
[GenerateProperty]
419+
string str;
420+
[GenerateCommand]
421+
public void Command1(int arg) { }
422+
}
423+
}";
424+
var generated = GenerateCode(source);
425+
StringAssert.DoesNotContain("protected", generated);
426+
}
409427

410428
[Test, Category("TODO")]
411429
public void CommonLanguageVersionTest() {

DevExpress.Mvvm.CodeGenerators/DevExpress.Mvvm.CodeGenerators.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<PropertyGroup>
7171
<Title>DevExpress.Mvvm.CodeGenerators</Title>
7272
<Product>DevExpress.Mvvm.CodeGenerators</Product>
73-
<Version>21.1.2</Version>
73+
<Version>21.1.3</Version>
7474
<NeutralLanguage>en-US</NeutralLanguage>
7575
<Company>Developer Express Inc.</Company>
7676
</PropertyGroup>

DevExpress.Mvvm.CodeGenerators/Generators/ClassGenerator.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static void GenerateSourceCode(SourceBuilder source, ContextInfo contextI
4343
if(implISS) {
4444
mvvmComponentsList.Add("ISupportServices");
4545
if(contextInfo.IsMvvmAvailable && !ClassHelper.IsInterfaceImplementedInCurrentClass(classSymbol, contextInfo.ISSSymbol!))
46-
interfaces.Add(new ISupportServicesGenerator());
46+
interfaces.Add(new ISupportServicesGenerator(classSymbol.IsSealed));
4747
}
4848

4949
List<ITypeSymbol> genericTypes = new();
@@ -107,10 +107,14 @@ static SourceBuilder GenerateHeader(SourceBuilder source, INamedTypeSymbol class
107107
} else
108108
source.AppendLine(" {");
109109
source = source.Tab;
110+
const string protectedModifier = "protected ";
111+
bool isSealed = classSymbol.IsSealed;
110112
if(!string.IsNullOrEmpty(raiseChangedMethod))
111-
source.AppendMultipleLines(raiseChangedMethod!);
113+
source.AppendIf(!isSealed, protectedModifier)
114+
.AppendMultipleLines(raiseChangedMethod!);
112115
if(!string.IsNullOrEmpty(raiseChangingMethod))
113-
source.AppendMultipleLines(raiseChangingMethod!);
116+
source.AppendIf(!isSealed, protectedModifier)
117+
.AppendMultipleLines(raiseChangingMethod!);
114118
if(!string.IsNullOrEmpty(raiseChangedMethod) || !string.IsNullOrEmpty(raiseChangingMethod))
115119
source.AppendLine();
116120
return source;

DevExpress.Mvvm.CodeGenerators/Generators/InterfaceGenerator.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ public object? ParentViewModel {
4444
}
4545
}
4646
class ISupportServicesGenerator : IInterfaceGenerator {
47-
const string Implementation = @"IServiceContainer? serviceContainer;
48-
protected IServiceContainer ServiceContainer { get => serviceContainer ??= new ServiceContainer(this); }
49-
IServiceContainer ISupportServices.ServiceContainer { get => ServiceContainer; }
50-
T? GetService<T>() where T : class => ServiceContainer.GetService<T>();";
47+
const string protectedModifier = "protected ";
48+
readonly bool isSealed;
49+
public ISupportServicesGenerator(bool isSealed) => this.isSealed = isSealed;
5150
public string GetName() => "ISupportServices";
52-
public void AppendImplementation(SourceBuilder source) => source.AppendMultipleLines(Implementation);
53-
51+
public void AppendImplementation(SourceBuilder source) {
52+
source.AppendLine("IServiceContainer? serviceContainer;")
53+
.AppendIf(!isSealed, protectedModifier)
54+
.AppendMultipleLines(@"IServiceContainer ServiceContainer { get => serviceContainer ??= new ServiceContainer(this); }
55+
IServiceContainer ISupportServices.ServiceContainer { get => ServiceContainer; }");
56+
source.AppendIf(!isSealed, protectedModifier)
57+
.AppendLine("T? GetService<T>() where T : class => ServiceContainer.GetService<T>();")
58+
.AppendIf(!isSealed, protectedModifier)
59+
.AppendLine("T? GetRequiredService<T>() where T : class => ServiceContainer.GetRequiredService<T>();");
60+
}
5461
}
5562
}

DevExpress.Mvvm.CodeGenerators/Helpers/SourceBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void BeforeAppend() {
6565
}
6666

6767
public static class SourceBuilderExtensions {
68+
public static SourceBuilder AppendIf(this SourceBuilder builder, bool condition, string str) => condition ? builder.Append(str) : builder;
69+
6870
public static SourceBuilder AppendLine(this SourceBuilder builder, string str) => builder.Append(str).AppendLine();
6971

7072
public static void AppendMultipleLines(this SourceBuilder builder, string lines, bool trimLeadingWhiteSpace = false) {

DevExpress.Mvvm.CodeGenerators/Info/INPCInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public static INPCInfo GetINPCedInfo(ContextInfo info, INamedTypeSymbol classSym
1818
symbol => AttributeHelper.HasAttribute(symbol, info.ViewModelAttributeSymbol),
1919
"RaisePropertyChanged",
2020
"System.ComponentModel.PropertyChangedEventArgs",
21-
"protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);");
21+
"void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);");
2222
public static INPCInfo GetINPCingInfo(ContextInfo info, INamedTypeSymbol classSymbol) =>
2323
new INPCInfo(classSymbol,
2424
info.INPCingSymbol,
2525
symbol => AttributeHelper.HasAttribute(symbol, info.ViewModelAttributeSymbol) &&
2626
AttributeHelper.GetPropertyActualValue(symbol, info.ViewModelAttributeSymbol, AttributesGenerator.ImplementINPCing, false),
2727
"RaisePropertyChanging",
2828
"System.ComponentModel.PropertyChangingEventArgs",
29-
"protected void RaisePropertyChanging(PropertyChangingEventArgs e) => PropertyChanging?.Invoke(this, e);");
29+
"void RaisePropertyChanging(PropertyChangingEventArgs e) => PropertyChanging?.Invoke(this, e);");
3030

3131
public bool HasNoImplementation() =>
3232
HasAttribute && !hasImplementation;

0 commit comments

Comments
 (0)