This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nbilal
authored and
nbilal
committed
Sep 5, 2016
1 parent
f39e72b
commit b93154c
Showing
14 changed files
with
229 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Etg.SimpleStubs.CodeGen | ||
{ | ||
internal interface IPropertyStubber | ||
{ | ||
ClassDeclarationSyntax StubProperty(ClassDeclarationSyntax classDclr, IPropertySymbol propertySymbol, | ||
INamedTypeSymbol stubbedInterface); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,75 @@ | ||
using System; | ||
using Etg.SimpleStubs.CodeGen.Utils; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Etg.SimpleStubs.CodeGen.Utils; | ||
|
||
namespace Etg.SimpleStubs.CodeGen | ||
{ | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using System.Linq; | ||
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using SF = SyntaxFactory; | ||
|
||
internal class PropertyStubber : IMethodStubber | ||
internal class PropertyStubber : IPropertyStubber | ||
{ | ||
public ClassDeclarationSyntax StubMethod(ClassDeclarationSyntax classDclr, IMethodSymbol methodSymbol, | ||
public ClassDeclarationSyntax StubProperty(ClassDeclarationSyntax classDclr, IPropertySymbol propertySymbol, | ||
INamedTypeSymbol stubbedInterface) | ||
{ | ||
if (!methodSymbol.IsPropertyAccessor()) | ||
{ | ||
return classDclr; | ||
} | ||
|
||
string delegateTypeName = NamingUtils.GetDelegateTypeName(methodSymbol, stubbedInterface); | ||
string indexerType = propertySymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); | ||
BasePropertyDeclarationSyntax propDclr = null; | ||
|
||
string propName = methodSymbol.AssociatedSymbol.Name; | ||
string propType = | ||
((IPropertySymbol) methodSymbol.AssociatedSymbol).Type.ToDisplayString( | ||
SymbolDisplayFormat.FullyQualifiedFormat); | ||
var propDclr = GetPropDclr(classDclr, propName); | ||
if (propDclr == null) | ||
if (propertySymbol.GetMethod != null) | ||
{ | ||
propDclr = SF.PropertyDeclaration(SF.ParseTypeName(propType), SF.Identifier(propName)) | ||
.WithExplicitInterfaceSpecifier(SF.ExplicitInterfaceSpecifier( | ||
SF.IdentifierName(methodSymbol.GetContainingInterfaceGenericQualifiedName()))); | ||
} | ||
IMethodSymbol getMethodSymbol = propertySymbol.GetMethod; | ||
string parameters = StubbingUtils.FormatParameters(getMethodSymbol); | ||
|
||
if (methodSymbol.IsPropertyGetter()) | ||
{ | ||
string delegateTypeName = NamingUtils.GetDelegateTypeName(getMethodSymbol, stubbedInterface); | ||
var accessorDclr = SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, SF.Block( | ||
SF.List(new[] | ||
{ | ||
SF.ParseStatement("return " + StubbingUtils.GenerateInvokeDelegateStmt(delegateTypeName, methodSymbol.Name, "")) | ||
SF.ParseStatement("return " + StubbingUtils.GenerateInvokeDelegateStmt(delegateTypeName, getMethodSymbol.Name, parameters)) | ||
}))); | ||
|
||
propDclr = CreatePropertyDclr(getMethodSymbol, indexerType); | ||
propDclr = propDclr.AddAccessorListAccessors(accessorDclr); | ||
|
||
} | ||
else if (methodSymbol.IsPropertySetter()) | ||
if (propertySymbol.SetMethod != null) | ||
{ | ||
IMethodSymbol setMethodSymbol = propertySymbol.SetMethod; | ||
string parameters = $"{StubbingUtils.FormatParameters(setMethodSymbol)}"; | ||
string delegateTypeName = NamingUtils.GetDelegateTypeName(setMethodSymbol, stubbedInterface); | ||
var accessorDclr = SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration, SF.Block( | ||
SF.List(new[] | ||
{ | ||
SF.ParseStatement(StubbingUtils.GenerateInvokeDelegateStmt(delegateTypeName, methodSymbol.Name, "value")) | ||
SF.ParseStatement(StubbingUtils.GenerateInvokeDelegateStmt(delegateTypeName, setMethodSymbol.Name, parameters)) | ||
}))); | ||
propDclr = propDclr.AddAccessorListAccessors(accessorDclr); | ||
} | ||
|
||
if (propDclr != null) | ||
{ | ||
PropertyDeclarationSyntax existingPropDclr = GetPropDclr(classDclr, propName); | ||
if (existingPropDclr != null) | ||
if (propDclr == null) | ||
{ | ||
classDclr = classDclr.ReplaceNode(existingPropDclr, propDclr); | ||
} | ||
else | ||
{ | ||
classDclr = classDclr.AddMembers(propDclr); | ||
propDclr = CreatePropertyDclr(setMethodSymbol, indexerType); | ||
} | ||
propDclr = propDclr.AddAccessorListAccessors(accessorDclr); | ||
} | ||
|
||
classDclr = classDclr.AddMembers(propDclr); | ||
return classDclr; | ||
} | ||
|
||
private static PropertyDeclarationSyntax GetPropDclr(ClassDeclarationSyntax classDclr, string propName) | ||
private BasePropertyDeclarationSyntax CreatePropertyDclr(IMethodSymbol methodSymbol, string propType) | ||
{ | ||
return | ||
classDclr.DescendantNodes() | ||
.OfType<PropertyDeclarationSyntax>() | ||
.FirstOrDefault(p => p.Identifier.Text == propName); | ||
if (methodSymbol.IsIndexerAccessor()) | ||
{ | ||
IndexerDeclarationSyntax indexerDclr = SF.IndexerDeclaration( | ||
SF.ParseTypeName(propType)) | ||
.WithExplicitInterfaceSpecifier(SF.ExplicitInterfaceSpecifier( | ||
SF.IdentifierName(methodSymbol.GetContainingInterfaceGenericQualifiedName()))); | ||
indexerDclr = indexerDclr.AddParameterListParameters( | ||
RoslynUtils.GetMethodParameterSyntaxList(methodSymbol).ToArray()); | ||
return indexerDclr; | ||
} | ||
|
||
string propName = methodSymbol.AssociatedSymbol.Name; | ||
PropertyDeclarationSyntax propDclr = SF.PropertyDeclaration(SF.ParseTypeName(propType), SF.Identifier(propName)) | ||
.WithExplicitInterfaceSpecifier(SF.ExplicitInterfaceSpecifier( | ||
SF.IdentifierName(methodSymbol.GetContainingInterfaceGenericQualifiedName()))); | ||
return propDclr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.