private IClassLikeDeclaration CreateClassHandlerDeclaration([NotNull] string entityName, [NotNull] string handlerInterfaceName)
        {
            var baseCtorArgs = new List <string>(2);


            var propertyWithKeyAttribute = classDeclaration.PropertyDeclarations.FirstOrDefault(x => x.Attributes.HasAttribute(Constants.Key));

            if (propertyWithKeyAttribute?.Type.IsGuid() ?? false)
            {
                baseCtorArgs.Add(propertyWithKeyAttribute.NameIdentifier.Name);
            }

            var timestampPropertyName = FindTimestampPropertyName();

            if (!string.IsNullOrWhiteSpace(timestampPropertyName))
            {
                baseCtorArgs.Add(timestampPropertyName);
            }

            var classHandlerName        = $"{entityName}Handler";
            var baseCtorArgsStr         = string.Join(", ", new[] { "dataContextProvider" }.Concat(baseCtorArgs.Select(x => $"x => x.{x}")));
            var classHandlerDeclaration = (IClassLikeDeclaration)factory.CreateTypeMemberDeclaration($"public class $0 : EntityHandlerBase<{className}>, $1 {{" +
                                                                                                     $"    public $0(IDataContextProvider dataContextProvider) : base({baseCtorArgsStr}){{}}" +
                                                                                                     $"}}", classHandlerName, handlerInterfaceName);

            return(classHandlerDeclaration);
        }
        private IMethodDeclaration CreateDeclaration([NotNull] MonoBehaviourEvent monoBehaviourEvent, [NotNull] CSharpElementFactory elementFactory, [NotNull] IClassLikeDeclaration context)
        {
            var builder = new StringBuilder(128);

            builder.Append("void ");
            builder.Append(monoBehaviourEvent.Name);
            builder.Append("(");

            for (int i = 0; i < monoBehaviourEvent.Parameters.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(",");
                }
                var parameter = monoBehaviourEvent.Parameters[i];
                builder.Append(parameter.ClrTypeName.FullName);
                if (parameter.IsArray)
                {
                    builder.Append("[]");
                }
                builder.Append(' ');
                builder.Append(parameter.Name);
            }

            builder.Append(");");

            var declaration = (IMethodDeclaration)elementFactory.CreateTypeMemberDeclaration(builder.ToString());

            declaration.SetResolveContextForSandBox(context, SandBoxContextType.Child);
            declaration.FormatNode();
            return(declaration);
        }
        public void Generate(CSharpGeneratorContext context,
                             IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                             CSharpElementFactory factory)
        {
            IMethodDeclaration methodDeclaration = null;

            ITypeElement declaredElement = context.ClassDeclaration.DeclaredElement;

            if (declaredElement != null)
            {
                IDeclaredType byteType = TypeFactory.CreateTypeByCLRName(PredefinedType.BYTE_FQN, context.PsiModule,
                                                                         context.Anchor.GetResolveContext());
                _byteArrayType      = TypeFactory.CreateArrayType(byteType, 1);
                _typeConversionRule = new CSharpTypeConversionRule(context.PsiModule);
                IOverridableMember member = declaredElement.Methods.FirstOrDefault(ValidateMethod);
                if (member != null)
                {
                    methodDeclaration = (IMethodDeclaration)member.GetDeclarations().FirstOrDefault();
                    Generate(context, methodDeclaration, elements, factory);
                    return;
                }
            }
            string method = String.Format("public Int32 {0}(Byte[] bytes, Int32 startIndex)", MethodName);

            methodDeclaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(method);
            Generate(context, methodDeclaration, elements, factory);
            context.PutMemberDeclaration(methodDeclaration, null,
                                         newDeclaration => new GeneratorDeclarationElement(newDeclaration));
        }
        public void Generate(CSharpGeneratorContext context,
                             IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                             CSharpElementFactory factory)
        {
            IMethodDeclaration methodDeclaration = null;

            ITypeElement declaredElement = context.ClassDeclaration.DeclaredElement;

            if (declaredElement != null)
            {
                IOverridableMember member = declaredElement.Methods.FirstOrDefault(ValidateMethod);
                if (member != null)
                {
                    methodDeclaration = (IMethodDeclaration)member.GetDeclarations().FirstOrDefault();
                    Generate(context, methodDeclaration, elements, factory);
                    return;
                }
            }
            string method = String.Format("public Int32 {0}()", MethodName);

            methodDeclaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(method);
            Generate(context, methodDeclaration, elements, factory);
            context.PutMemberDeclaration(methodDeclaration, null,
                                         newDeclaration => new GeneratorDeclarationElement(newDeclaration));
        }
Exemplo n.º 5
0
        public IMethodDeclaration CreateDeclaration([NotNull] CSharpElementFactory factory, [NotNull] IClassLikeDeclaration classDeclaration)
        {
            var builder = new StringBuilder(128);

            builder.Append("private ");
            if (IsStatic)
            {
                builder.Append("static ");
            }
            builder.Append(ReturnType.FullName);
            if (ReturnTypeIsArray)
            {
                builder.Append("[]");
            }
            builder.Append(" ");
            builder.Append(Name);
            builder.Append("(");

            for (var i = 0; i < Parameters.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(",");
                }

                var parameter = Parameters[i];
                // TODO: `out` or `ref`?
                // From reflection point of view, it's a "ByRef" Type, and that's all we know...
                // The only place it's currently being used is an out parameter
                if (parameter.IsByRef)
                {
                    builder.Append("out ");
                }
                builder.Append(parameter.ClrTypeName.FullName);
                if (parameter.IsArray)
                {
                    builder.Append("[]");
                }
                builder.Append(' ');
                builder.Append(parameter.Name);
            }

            builder.Append(");");

            var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(builder.ToString());

            declaration.SetResolveContextForSandBox(classDeclaration, SandBoxContextType.Child);
            declaration.FormatNode();
            return(declaration);
        }
        public IMethodDeclaration CreateDeclaration([NotNull] CSharpElementFactory factory, [NotNull] IClassLikeDeclaration classDeclaration)
        {
            var builder = new StringBuilder(128);

            builder.Append("private ");
            if (IsStatic)
            {
                builder.Append("static ");
            }
            builder.Append(ReturnType.FullName);
            if (ReturnTypeIsArray)
            {
                builder.Append("[]");
            }
            builder.Append(" ");
            builder.Append(Name);
            builder.Append("(");

            for (var i = 0; i < Parameters.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(",");
                }

                var parameter = Parameters[i];
                builder.Append(parameter.ClrTypeName.FullName);
                if (parameter.IsArray)
                {
                    builder.Append("[]");
                }
                builder.Append(' ');
                builder.Append(parameter.Name);
            }

            builder.Append(");");

            var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(builder.ToString());

            declaration.SetResolveContextForSandBox(classDeclaration, SandBoxContextType.Child);
            declaration.FormatNode();
            return(declaration);
        }
Exemplo n.º 7
0
        private IClassDeclaration GenerateContractClassDeclaration(string contractClassName)
        {
            Contract.Ensures(Contract.Result <IClassDeclaration>() != null);

            var classDeclaration = (IClassDeclaration)_factory.CreateTypeMemberDeclaration(
                string.Format("class {0} {{}}", contractClassName));

            if (_addContractForAvailability.IsInterface)
            {
                AddContractClassForInterface(classDeclaration);
            }
            else
            {
                AddContractClassForAbstractClass(classDeclaration);
            }

            //ITypeUsage typeUsage = null;
            //var contractClass = CSharpTypeFactory.CreateType(typeUsage);
            return(classDeclaration);
        }
Exemplo n.º 8
0
        private static void CreateDispose(CSharpGeneratorContext context, CSharpElementFactory factory,
                                          ICollection <GeneratorDeclaredElement <ITypeOwner> > typeOwners)
        {
            var existingEquals = FindDispose(context);
            IMethodDeclaration declaration;

            if (existingEquals != null)
            {
                if (context.GetGlobalOptionValue("ChangeDispose") == "Skip")
                {
                    return;
                }
                if (context.GetGlobalOptionValue("ChangeDispose") == "Replace")
                {
                    declaration = (IMethodDeclaration)existingEquals.GetDeclarations().FirstOrDefault();
                    GenerateDisposeBody(context, declaration, typeOwners, factory);
                    return;
                }
            }
            declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(
                "public void Dispose();");
            GenerateDisposeBody(context, declaration, typeOwners, factory);
            context.PutMemberDeclaration(declaration, null, newDeclaration => new GeneratorDeclarationElement(newDeclaration));
        }
Exemplo n.º 9
0
 public void Signature(IClassLikeDeclaration classDeclaration, CSharpElementFactory dataProviderElementFactory)
 {
     classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProviderElementFactory.CreateTypeMemberDeclaration(Signature()));
 }
Exemplo n.º 10
0
        public IMethodDeclaration CreateDeclaration([NotNull] CSharpElementFactory factory,
                                                    [NotNull] IClassLikeDeclaration classDeclaration,
                                                    AccessRights accessRights,
                                                    bool makeVirtual = false)
        {
            var builder = new StringBuilder(128);

            if (accessRights != AccessRights.NONE)
            {
                builder.Append(CSharpDeclaredElementPresenter.Instance.Format(accessRights));
                builder.Append(" ");
            }

            if (IsStatic)
            {
                builder.Append("static ");
            }

            // Consider this declaration a template, and the final generated code implements (or overrides) this API
            if (makeVirtual)
            {
                builder.Append("virtual ");
            }
            builder.Append("global::");
            builder.Append(ReturnType.FullName);
            if (ReturnTypeIsArray)
            {
                builder.Append("[]");
            }
            builder.Append(" ");
            builder.Append(Name);
            builder.Append("(");

            for (var i = 0; i < Parameters.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(",");
                }

                var parameter = Parameters[i];
                // TODO: `out` or `ref`?
                // From reflection point of view, it's a "ByRef" Type, and that's all we know...
                // The only place it's currently being used is an out parameter
                if (parameter.IsByRef)
                {
                    builder.Append("out ");
                }
                builder.Append("global::");
                builder.Append(parameter.ClrTypeName.FullName);
                if (parameter.IsArray)
                {
                    builder.Append("[]");
                }
                builder.Append(' ');
                builder.Append(parameter.Name);
            }

            builder.Append(");");

            var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(builder.ToString());

            declaration.SetResolveContextForSandBox(classDeclaration, SandBoxContextType.Child);
            return(declaration);
        }
        /// <summary>
        /// Adds the interface.
        /// </summary>
        /// <param name="classDeclaration">
        /// The class declaration.
        /// </param>
        /// <param name="factory">
        /// The factory.
        /// </param>
        private static void AddProxyClass(IClassDeclaration classDeclaration, CSharpElementFactory factory)
        {
            var className = classDeclaration.DeclaredName;
              var variableName = GetVariableName(classDeclaration);

              var namingPolicy = CodeStyleSettingsManager.Instance.CodeStyleSettings.GetNamingSettings2().PredefinedNamingRules[NamedElementKinds.PrivateInstanceFields];
              var prefix = namingPolicy.NamingRule.Prefix;

              var proxyProperty = prefix + variableName;

              var isStatic = false; // classDeclaration.IsStatic;

              var staticClass = isStatic ? " static" : string.Empty;

              var code = new StringBuilder(string.Format("public{1} class {0}Proxy {{", classDeclaration.DeclaredName, staticClass));

              if (!isStatic)
              {
            code.Append(string.Format("\r\nreadonly {0} {1};", classDeclaration.DeclaredName, proxyProperty));
            code.Append(string.Format("public {0}Proxy({0} {1}) {{\r\n{2} = {1};\r\n}}\r\n", classDeclaration.DeclaredName, variableName, proxyProperty));
              }

              var cls = classDeclaration.DeclaredElement as IClass;
              if (cls == null)
              {
            return;
              }

              AddMembers(code, cls, proxyProperty, className);

              code.Append("}");

              var memberDeclaration = factory.CreateTypeMemberDeclaration(code.ToString()) as IClassDeclaration;

              var namespaceDeclaration = classDeclaration.GetContainingNamespaceDeclaration();

              namespaceDeclaration.AddTypeDeclarationAfter(memberDeclaration, classDeclaration);
        }
        public IMethodDeclaration CreateDeclaration([NotNull] CSharpElementFactory factory,
                                                    [NotNull] KnownTypesCache knownTypesCache,
                                                    [NotNull] IClassLikeDeclaration classDeclaration,
                                                    AccessRights accessRights,
                                                    bool makeVirtual   = false,
                                                    bool makeCoroutine = false)
        {
            var    builder = new StringBuilder(128);
            var    args    = new object[1 + Parameters.Length];
            object arg;
            var    argIndex = 0;
            var    module   = classDeclaration.GetPsiModule();

            if (accessRights != AccessRights.NONE)
            {
                builder.Append(CSharpDeclaredElementPresenter.Instance.Format(accessRights));
                builder.Append(" ");
            }

            if (IsStatic)
            {
                builder.Append("static ");
            }

            // Consider this declaration a template, and the final generated code implements (or overrides) this API
            if (makeVirtual)
            {
                builder.Append("virtual ");
            }
            if (makeCoroutine && CanBeCoroutine)
            {
                builder.Append(PredefinedType.IENUMERATOR_FQN.FullName);
            }
            else
            {
                arg = GetTypeObject(ReturnType, knownTypesCache, module);
                if (arg is string)
                {
                    builder.Append(arg);
                    if (ReturnType.IsArray)
                    {
                        builder.Append("[]");
                    }
                }
                else
                {
                    builder.Append("$0");
                    args[argIndex++] = arg;
                }
            }

            builder.Append(" ");
            builder.Append(Name);
            builder.Append("(");

            for (var i = 0; i < Parameters.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(",");
                }

                var parameter = Parameters[i];
                // TODO: `out` or `ref`?
                // From reflection point of view, it's a "ByRef" Type, and that's all we know...
                // The only place it's currently being used is an out parameter
                if (parameter.IsByRef)
                {
                    builder.Append("out ");
                }

                arg = GetTypeObject(parameter.TypeSpec, knownTypesCache, module);
                if (arg is string)
                {
                    builder.Append(arg);
                    if (parameter.TypeSpec.IsArray)
                    {
                        builder.Append("[]");
                    }
                }
                else
                {
                    builder.Append($"${argIndex}");
                    args[argIndex++] = arg;
                }
                builder.Append(' ');
                builder.Append(parameter.Name);
            }

            builder.Append(");");

            var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(builder.ToString(), args);

            declaration.SetResolveContextForSandBox(classDeclaration, SandBoxContextType.Child);
            return(declaration);
        }
        private static void AddProxyClass(IClassDeclaration classDeclaration, CSharpElementFactory factory)
        {
            var builderType = string.Format("{0}Builder", classDeclaration.DeclaredName);

            var code = new StringBuilder(string.Format("public class {0} {{", builderType));

            code.AppendLine();
            var cls = classDeclaration.DeclaredElement as IClass;

            if (cls == null)
            {
                return;
            }

            var ctor = cls.Constructors.OrderByDescending(x => x.Parameters.Count).FirstOrDefault();

            if (ctor == null)
            {
                return;
            }

            var fields  = new StringBuilder();
            var methods = new StringBuilder();

            foreach (var parameter in ctor.Parameters)
            {
                var typePresentableName  = parameter.Type.GetPresentableName(cls.PresentationLanguage);
                var shortName            = parameter.ShortName;
                var capitalizedShortName = shortName.Capitalize();

                fields.AppendLine("private {0} _{1};", typePresentableName, shortName);
                methods.AppendLine("public {2} With{1}({0} value){{ ", typePresentableName, capitalizedShortName, builderType);
                methods.AppendLine(" _{0} = value;", shortName);
                methods.AppendLine("return this;");
                methods.AppendLine("}");
                methods.AppendLine();

                if (parameter.Type.IsGenericIEnumerable())
                {
                    var genericParameter = typePresentableName.Split(new[] { '<', '>' })[1];
                    var listType         = string.Format("List<{0}>", genericParameter);
                    var singularName     = NounUtil.GetSingular(capitalizedShortName);

                    methods.AppendLine("public {0} Add{1}({2} value){{", builderType, singularName, genericParameter);
                    methods.AppendLine(" if(_{0} == null){{", shortName);
                    methods.AppendLine("  _{0} = new {1}();", shortName, listType);
                    methods.AppendLine(" }");
                    methods.AppendLine("  var collection = _{0} as ICollection<{1}>;", shortName, genericParameter);
                    methods.AppendLine("  if (collection == null || collection.IsReadOnly){");
                    methods.AppendLine("     throw new InvalidOperationException(\"Add{0}() method cannot be used with this collection type\");", singularName);
                    methods.AppendLine("  }");
                    methods.AppendLine(" collection.Add(value);");
                    methods.AppendLine(" return this;");
                    methods.AppendLine("}");
                    methods.AppendLine();
                }
            }

            code.Append(fields);
            code.Append(methods);


            code.AppendLine("public {0} Build(){{", classDeclaration.DeclaredName);
            code.AppendFormat("return new  {0}(", classDeclaration.DeclaredName);
            code.Append(ctor.Parameters.Select(x => string.Format("_{0}", x.ShortName)).ToArray().Join(", "));
            code.AppendLine(");");
            code.AppendLine("}");


            code.Append("}");

            var memberDeclaration = factory.CreateTypeMemberDeclaration(code.ToString()) as IClassDeclaration;

            var namespaceDeclaration = classDeclaration.GetContainingNamespaceDeclaration();

            namespaceDeclaration.AddTypeDeclarationAfter(memberDeclaration, classDeclaration);
        }
        /// <summary>
        /// Adds the member.
        /// </summary>
        /// <param name="classDeclaration">
        /// The class declaration.
        /// </param>
        /// <param name="factory">
        /// The factory.
        /// </param>
        /// <param name="code">
        /// The member code.
        /// </param>
        private static void AddMember(IClassDeclaration classDeclaration, CSharpElementFactory factory, string code)
        {
            var memberDeclaration = factory.CreateTypeMemberDeclaration(code) as IClassMemberDeclaration;

              classDeclaration.AddClassMemberDeclaration(memberDeclaration);
        }
 private IClassDeclaration GenerateNonGenericContractClassDeclaration(string contractClassName)
 {
     return((IClassDeclaration)_factory.CreateTypeMemberDeclaration(
                "abstract class $0 : $1 {}",
                contractClassName, _addContractForAvailability.DeclaredType));
 }