public static (List <AttributeListSyntax>, List <MemberDeclarationSyntax>) GenerateSyntax( Assembly targetAssembly, List <GrainInterfaceDescription> grainInterfaces, List <GrainClassDescription> grainClasses, SerializationTypeDescriptions typeDescriptions) { var attributes = new List <AttributeListSyntax>(); var members = new List <MemberDeclarationSyntax>(); var className = CodeGeneratorCommon.ClassPrefix + Guid.NewGuid().ToString("N").Substring(0, 10) + ClassSuffix; // Generate a class for populating the metadata. var classSyntax = SF.ClassDeclaration(className) .AddBaseListTypes( SF.SimpleBaseType(typeof(IFeaturePopulator <GrainInterfaceFeature>).GetTypeSyntax()), SF.SimpleBaseType(typeof(IFeaturePopulator <GrainClassFeature>).GetTypeSyntax()), SF.SimpleBaseType(typeof(IFeaturePopulator <SerializerFeature>).GetTypeSyntax())) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword), SF.Token(SyntaxKind.SealedKeyword)) .AddMembers(GeneratePopulateMethod(grainInterfaces), GeneratePopulateMethod(grainClasses), GeneratePopulateMethod(typeDescriptions)) .AddAttributeLists(SF.AttributeList(SF.SingletonSeparatedList(CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax()))); var namespaceSyntax = SF.NamespaceDeclaration(NamespaceName.ToIdentifierName()).AddMembers(classSyntax); members.Add(namespaceSyntax); // Generate an assembly-level attribute with an instance of that class. var attribute = SF.AttributeList( SF.AttributeTargetSpecifier(SF.Token(SyntaxKind.AssemblyKeyword)), SF.SingletonSeparatedList( SF.Attribute(typeof(FeaturePopulatorAttribute).GetNameSyntax()) .AddArgumentListArguments(SF.AttributeArgument(SF.TypeOfExpression(SF.ParseTypeName(NamespaceName + "." + className)))))); attributes.Add(attribute); return(attributes, members); }
/// <summary> /// Generates the class for the provided grain types. /// </summary> /// <param name="grainType"> /// The grain interface type. /// </param> /// <returns> /// The generated class. /// </returns> internal static TypeDeclarationSyntax GenerateClass(Type grainType) { var baseTypes = new List <BaseTypeSyntax> { SF.SimpleBaseType(typeof(IGrainMethodInvoker).GetTypeSyntax()) }; var grainTypeInfo = grainType.GetTypeInfo(); var genericTypes = grainTypeInfo.IsGenericTypeDefinition ? grainTypeInfo.GetGenericArguments() .Select(_ => SF.TypeParameter(_.ToString())) .ToArray() : new TypeParameterSyntax[0]; // Create the special method invoker marker attribute. var interfaceId = GrainInterfaceUtils.GetGrainInterfaceId(grainType); var interfaceIdArgument = SF.LiteralExpression(SyntaxKind.NumericLiteralExpression, SF.Literal(interfaceId)); var grainTypeArgument = SF.TypeOfExpression(grainType.GetTypeSyntax(includeGenericParameters: false)); var attributes = new List <AttributeSyntax> { CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax(), SF.Attribute(typeof(MethodInvokerAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument(grainType.GetParseableName().GetLiteralExpression()), SF.AttributeArgument(interfaceIdArgument), SF.AttributeArgument(grainTypeArgument)), #if !NETSTANDARD SF.Attribute(typeof(ExcludeFromCodeCoverageAttribute).GetNameSyntax()) #endif }; var members = new List <MemberDeclarationSyntax> { GenerateInvokeMethod(grainType), GenerateInterfaceIdProperty(grainType) }; // If this is an IGrainExtension, make the generated class implement IGrainExtensionMethodInvoker. if (typeof(IGrainExtension).IsAssignableFrom(grainType)) { baseTypes.Add(SF.SimpleBaseType(typeof(IGrainExtensionMethodInvoker).GetTypeSyntax())); members.Add(GenerateExtensionInvokeMethod(grainType)); } var classDeclaration = SF.ClassDeclaration( CodeGeneratorCommon.ClassPrefix + TypeUtils.GetSuitableClassName(grainType) + ClassSuffix) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword)) .AddBaseListTypes(baseTypes.ToArray()) .AddConstraintClauses(grainType.GetTypeConstraintSyntax()) .AddMembers(members.ToArray()) .AddAttributeLists(SF.AttributeList().AddAttributes(attributes.ToArray())); if (genericTypes.Length > 0) { classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes); } return(classDeclaration); }
internal static AttributeSyntax GetGeneratedCodeAttributeSyntax() { return (SF.Attribute(typeof(GeneratedCodeAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument(CodeGeneratorName.GetLiteralExpression()), SF.AttributeArgument(CodeGeneratorVersion.GetLiteralExpression()))); }
public static AttributeListSyntax GenAttribute(string name, string args) { return(SF.AttributeList( SF.SingletonSeparatedList( SF.Attribute( SF.IdentifierName(name)).WithArgumentList( SF.AttributeArgumentList( SF.SingletonSeparatedList( SF.AttributeArgument( SF.ParseExpression(args)))))))); }
/// <summary> /// Generates the class for the provided grain types. /// </summary> /// <param name="grainType"> /// The grain interface type. /// </param> /// <param name="onEncounteredType"> /// The callback which is invoked when a type is encountered. /// </param> /// <returns> /// The generated class. /// </returns> internal static TypeDeclarationSyntax GenerateClass(Type grainType, Action <Type> onEncounteredType) { var grainTypeInfo = grainType.GetTypeInfo(); var genericTypes = grainTypeInfo.IsGenericTypeDefinition ? grainTypeInfo.GetGenericArguments() .Select(_ => SF.TypeParameter(_.ToString())) .ToArray() : new TypeParameterSyntax[0]; // Create the special marker attribute. var markerAttribute = SF.Attribute(typeof(GrainReferenceAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument( SF.TypeOfExpression(grainType.GetTypeSyntax(includeGenericParameters: false)))); var attributes = SF.AttributeList() .AddAttributes( CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax(), SF.Attribute(typeof(SerializableAttribute).GetNameSyntax()), #if !NETSTANDARD_TODO //ExcludeFromCodeCoverageAttribute became an internal class in netstandard SF.Attribute(typeof(ExcludeFromCodeCoverageAttribute).GetNameSyntax()), #endif markerAttribute); var className = CodeGeneratorCommon.ClassPrefix + TypeUtils.GetSuitableClassName(grainType) + ClassSuffix; var classDeclaration = SF.ClassDeclaration(className) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword)) .AddBaseListTypes( SF.SimpleBaseType(typeof(GrainReference).GetTypeSyntax()), SF.SimpleBaseType(grainType.GetTypeSyntax())) .AddConstraintClauses(grainType.GetTypeConstraintSyntax()) .AddMembers(GenerateConstructors(className)) .AddMembers( GenerateInterfaceIdProperty(grainType), GenerateInterfaceNameProperty(grainType), GenerateIsCompatibleMethod(grainType), GenerateGetMethodNameMethod(grainType)) .AddMembers(GenerateInvokeMethods(grainType, onEncounteredType)) .AddAttributeLists(attributes); if (genericTypes.Length > 0) { classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes); } return(classDeclaration); }
public static CompilationUnitSyntax AddGeneratedCodeAttribute(GeneratedSyntax generatedSyntax) { var codeGenTargetAttributes = SF.AttributeList() .AddAttributes( generatedSyntax.SourceAssemblies.Select( asm => SF.Attribute(typeof(OrleansCodeGenerationTargetAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument(asm.GetName().FullName.GetLiteralExpression()))).ToArray()) .WithTarget(SF.AttributeTargetSpecifier(SF.Token(SyntaxKind.AssemblyKeyword))); return(generatedSyntax.Syntax.AddAttributeLists(codeGenTargetAttributes)); }
/// <summary> /// Generates the class for the provided grain types. /// </summary> /// <param name="type">The grain interface type.</param> /// <param name="onEncounteredType"> /// The callback invoked when a type is encountered. /// </param> /// <returns> /// The generated class. /// </returns> internal static TypeDeclarationSyntax GenerateClass(Type type, Action <Type> onEncounteredType) { var typeInfo = type.GetTypeInfo(); var genericTypes = typeInfo.IsGenericTypeDefinition ? typeInfo.GetGenericArguments().Select(_ => SF.TypeParameter(_.ToString())).ToArray() : new TypeParameterSyntax[0]; var attributes = new List <AttributeSyntax> { CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax(), #if !NETSTANDARD SF.Attribute(typeof(ExcludeFromCodeCoverageAttribute).GetNameSyntax()), #endif SF.Attribute(typeof(SerializerAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument(SF.TypeOfExpression(type.GetTypeSyntax(includeGenericParameters: false)))) }; var className = CodeGeneratorCommon.ClassPrefix + type.GetParseableName(GeneratedTypeNameOptions); var fields = GetFields(type); // Mark each field type for generation foreach (var field in fields) { var fieldType = field.FieldInfo.FieldType; onEncounteredType(fieldType); } var members = new List <MemberDeclarationSyntax>(GenerateStaticFields(fields)) { GenerateDeepCopierMethod(type, fields), GenerateSerializerMethod(type, fields), GenerateDeserializerMethod(type, fields), }; var classDeclaration = SF.ClassDeclaration(className) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword)) .AddAttributeLists(SF.AttributeList().AddAttributes(attributes.ToArray())) .AddMembers(members.ToArray()) .AddConstraintClauses(type.GetTypeConstraintSyntax()); if (genericTypes.Length > 0) { classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes); } return(classDeclaration); }
public static SyntaxList <AttributeListSyntax> GenerateStructLayoutAttributes() { return(SF.SingletonList( SF.AttributeList( SF.SingletonSeparatedList( SF.Attribute(SF.IdentifierName("StructLayout")) .WithArgumentList( SF.AttributeArgumentList( SF.SingletonSeparatedList( SF.AttributeArgument( SF.MemberAccessExpression( SK.SimpleMemberAccessExpression, SF.IdentifierName("LayoutKind"), SF.IdentifierName("Explicit")))))))))); }
public static AttributeSyntax GenAttribute(string name, string args) { var attribure = SF.Attribute(SF.IdentifierName(name)); if (args != null) { attribure = attribure.WithArgumentList( SF.AttributeArgumentList( SF.SingletonSeparatedList( SF.AttributeArgument( SF.ParseExpression(args))))); } return(attribure); }
private SyntaxList <AttributeListSyntax> CreateSerializationAttribute(SymbolData symbol) { string requiredValueName; if (symbol.IsRequired) { requiredValueName = (symbol.isNullable) ? "AllowNull" : "Always"; } else { requiredValueName = "Default"; } Func <string, string, string, AttributeArgumentSyntax> createAttr = (lhs, expr, name) => { // lhs = expr.name return(SF.AttributeArgument( SF.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SF.IdentifierName(expr), SF.Token(SyntaxKind.DotToken), SF.IdentifierName(name))) .WithNameEquals( SF.NameEquals(SF.IdentifierName(lhs)))); }; var attributes = SF.SeparatedList(new[] { // Required = Required.[requiredValueName] createAttr("Required", "Required", requiredValueName), }); if (requiredValueName == "Default" && !symbol.isNullable) { // NullValueHandling = NullValueHandling.Ignore attributes = attributes.Add( createAttr("NullValueHandling", "NullValueHandling", "Ignore")); } return(SF.SingletonList( SF.AttributeList( SF.SingletonSeparatedList( SF.Attribute(SF.IdentifierName("JsonProperty")) .WithArgumentList( SF.AttributeArgumentList(attributes)))))); }
/// <summary> /// Generates the class for the provided grain types. /// </summary> /// <param name="type">The grain interface type.</param> /// <param name="onEncounteredType"> /// The callback invoked when a type is encountered. /// </param> /// <returns> /// The generated class. /// </returns> internal static IEnumerable <TypeDeclarationSyntax> GenerateClass(Type type, Action <Type> onEncounteredType) { var typeInfo = type.GetTypeInfo(); var genericTypes = typeInfo.IsGenericTypeDefinition ? typeInfo.GetGenericArguments().Select(_ => SF.TypeParameter(_.ToString())).ToArray() : new TypeParameterSyntax[0]; var attributes = new List <AttributeSyntax> { CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax(), SF.Attribute(typeof(ExcludeFromCodeCoverageAttribute).GetNameSyntax()), SF.Attribute(typeof(SerializerAttribute).GetNameSyntax()) .AddArgumentListArguments( SF.AttributeArgument(SF.TypeOfExpression(type.GetTypeSyntax(includeGenericParameters: false)))) }; var className = CodeGeneratorCommon.ClassPrefix + type.GetParseableName(GeneratedTypeNameOptions); var fields = GetFields(type); // Mark each field type for generation foreach (var field in fields) { var fieldType = field.FieldInfo.FieldType; onEncounteredType(fieldType); } var members = new List <MemberDeclarationSyntax>(GenerateStaticFields(fields)) { GenerateDeepCopierMethod(type, fields), GenerateSerializerMethod(type, fields), GenerateDeserializerMethod(type, fields), }; if (typeInfo.IsConstructedGenericType || !typeInfo.IsGenericTypeDefinition) { members.Add(GenerateRegisterMethod(type)); members.Add(GenerateConstructor(className)); attributes.Add(SF.Attribute(typeof(RegisterSerializerAttribute).GetNameSyntax())); } var classDeclaration = SF.ClassDeclaration(className) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword)) .AddAttributeLists(SF.AttributeList().AddAttributes(attributes.ToArray())) .AddMembers(members.ToArray()) .AddConstraintClauses(type.GetTypeConstraintSyntax()); if (genericTypes.Length > 0) { classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes); } var classes = new List <TypeDeclarationSyntax> { classDeclaration }; if (typeInfo.IsGenericTypeDefinition) { // Create a generic representation of the serializer type. var serializerType = SF.GenericName(classDeclaration.Identifier) .WithTypeArgumentList( SF.TypeArgumentList() .AddArguments( type.GetGenericArguments() .Select(_ => SF.OmittedTypeArgument()) .Cast <TypeSyntax>() .ToArray())); var registererClassName = className + "_" + string.Join("_", type.GetTypeInfo().GenericTypeParameters.Select(_ => _.Name)) + "_" + RegistererClassSuffix; classes.Add( SF.ClassDeclaration(registererClassName) .AddModifiers(SF.Token(SyntaxKind.InternalKeyword)) .AddAttributeLists( SF.AttributeList() .AddAttributes( CodeGeneratorCommon.GetGeneratedCodeAttributeSyntax(), SF.Attribute(typeof(ExcludeFromCodeCoverageAttribute).GetNameSyntax()), SF.Attribute(typeof(RegisterSerializerAttribute).GetNameSyntax()))) .AddMembers( GenerateMasterRegisterMethod(type, serializerType), GenerateConstructor(registererClassName))); } return(classes); }
public static byte[] CreateControllerCode(IService service) { var @file = SF.CompilationUnit(); var @namespace = SF.NamespaceDeclaration(SF.ParseName("MBase.ServiceHost.Controllers")).NormalizeWhitespace(); @file = @file .AddUsings(SF.UsingDirective(SF.IdentifierName("System"))); @file = @file .AddUsings(SF.UsingDirective(SF.IdentifierName("Microsoft.AspNetCore.Mvc"))); @file = @file .AddUsings(SF.UsingDirective(SF.IdentifierName("System.Threading.Tasks"))); @file = @file.AddUsings(SF.UsingDirective(SF.IdentifierName(service.GetType().Namespace))); @file = @file.AddUsings(SF.UsingDirective(SF.IdentifierName(service.GetType().Namespace + ".Models"))); foreach (var item in service.Commands.Where(m => typeof(ICommand).IsAssignableFrom(m.GetType()))) { @file = @file.AddUsings(SF.UsingDirective(SF.IdentifierName(item.GetType().Namespace))); } var classDeclaration = SF.ClassDeclaration(service.GetType().Name.ToString() + "Controller") .AddModifiers(SF.Token(SyntaxKind.PublicKeyword)) .AddBaseListTypes(SF.SimpleBaseType(SF.ParseTypeName("ControllerBase"))) .WithAttributeLists( SF.List( new AttributeListSyntax[] { SF.AttributeList( SF.SingletonSeparatedList( SF.Attribute(SF.IdentifierName("Route")) .WithArgumentList( SF.AttributeArgumentList( SF.SingletonSeparatedList( SF.AttributeArgument( SF.LiteralExpression(SyntaxKind.StringLiteralExpression, SF.Literal($"api/[controller]")))))))), SF.AttributeList( SF.SingletonSeparatedList( SF.Attribute(SF.IdentifierName("ApiController")))) })); List <MemberDeclarationSyntax> controllerMembers = new List <MemberDeclarationSyntax>(); controllerMembers.Add( SF.FieldDeclaration( SF.VariableDeclaration( SF.ParseTypeName(service.GetType().Name)) .AddVariables(SF.VariableDeclarator("_service"))) .AddModifiers(SF.Token(SyntaxKind.PrivateKeyword))); controllerMembers.Add( SF.ConstructorDeclaration(service.GetType().Name.ToString() + "Controller") .WithParameterList( SF.ParameterList( SF.SingletonSeparatedList( SF.Parameter( SF.Identifier("service")) .WithType( SF.IdentifierName("IService"))) )) .AddModifiers(SF.Token(SyntaxKind.PublicKeyword)) .WithBody(SF.Block(SF.ParseStatement($"this._service = ({service.GetType().Name})service;")))); foreach (var item in service.Commands.Where(m => typeof(ICommand).IsAssignableFrom(m.GetType()))) { var syntax = @$ " var response = await CommandHelper.ExecuteMethod<{item.Name}>(new {item.Name}(),new Request<{item.Name}>(request, new MessageEnvelope()));