Пример #1
0
    public static string NestArray2FunctionName(Language language, List <object> nestArray, CodeMode codeMode = CodeMode.None)
    {
        string code = string.Empty;

        code += BufferCodeExporter.GetCodeModeName(language, codeMode);
        object element;

        for (int i = 0; i < nestArray.Count; i++)
        {
            element = nestArray[i];
            if (element is string)
            {
                string prefixType     = element as string;
                int    array          = GetArraySuffix(ref prefixType);
                string typeConvention = BufferCodeExporter.GetByteBufferCode(language, prefixType, codeMode);               //UppercaseWords();

                code += typeConvention;
                if (i + 1 < nestArray.Count && nestArray[i + 1] is List <object> )
                {
                    List <object> generics = nestArray[i + 1] as List <object>;
                    code += NestArray2FunctionName(language, generics, CodeMode.None);
                    i++;
                }
                if (nestArray.Count > 1 || array > 0)
                {
                    code += array;
                }
            }
        }
        return(code);
    }
Пример #2
0
        public static List <string> GetNamespaces(Language language, string type, List <ClassDefine> queryClasss)
        {
            List <string> namespaceList = new List <string>();
            List <object> nestArray     = CodeExporterHelper.TypeName2NestArray(type);
            object        element       = nestArray[0];

            if (element is string)
            {
                string prefixType = element as string;
                string typeForGetArrayDemision = type;
                int    array = CodeExporterHelper.GetArraySuffix(ref prefixType);
                if (array == 0)
                {
                    array = CodeExporterHelper.GetArraySuffix(ref typeForGetArrayDemision);
                }
                if (array > 0) //Array
                {
                    namespaceList.AddRange(GetNamespaces(language, CodeExporterHelper.GetArrayElementTypeName(type), queryClasss));
                }
                else if (prefixType == CodeGenConstants.ComplexType[1])//List
                {
                    if (language == Language.CSharp)
                    {
                        namespaceList.Add("System.Collections.Generic");
                    }
                    else if (language == Language.Java)
                    {
                        namespaceList.Add("java.util.List");
                        namespaceList.Add("java.util.ArrayList");
                    }
                    namespaceList.AddRange(GetNamespaces(language, CodeExporterHelper.GetListGenericTypeName(type), queryClasss));
                }
                else if (prefixType == CodeGenConstants.ComplexType[2])//Dictionary
                {
                    if (language == Language.CSharp)
                    {
                        namespaceList.Add("System.Collections.Generic");
                    }
                    else if (language == Language.Java)
                    {
                        namespaceList.Add("java.util.HashMap");
                    }
                    string[] genericTypes = CodeExporterHelper.GetDictionaryGenericTypeName(type);
                    namespaceList.AddRange(GetNamespaces(language, genericTypes[0], queryClasss));
                    namespaceList.AddRange(GetNamespaces(language, genericTypes[1], queryClasss));
                }
                else if (Array.IndexOf(CodeGenConstants.BaseType, prefixType) == -1) //custom class
                {
                    namespaceList.Add(BufferCodeExporter.GetNamespaceStatement(language, "", NamespaceMode.None));
                    namespaceList.Add("ByteBuilder");
                }
            }
            return(namespaceList);
        }
Пример #3
0
        //增加命名空间:xxx.xxx.xxx 这种形式,然后xxx都是小写
        public static bool ExportNamespace(StringBuilder sb, ClassDefine classNode, Language language)
        {
            string           namesapceStatement = BufferCodeExporter.GetNamespaceStatement(language, classNode.decoration, NamespaceMode.Declare);
            HashSet <string> usingNamesapceSet  = new HashSet <string>();

            foreach (var fieldNode in classNode.fieldList)
            {
                var usingNamespaces = GetNamespaces(language, fieldNode.decoration, globalClassDefines);
                foreach (var usingNamespace in usingNamespaces)
                {
                    if (!usingNamesapceSet.Contains(usingNamespace))
                    {
                        usingNamesapceSet.Add(usingNamespace);
                    }
                }
            }
            if (language == Language.Java)
            {
                sb.AppendIndentLine(namesapceStatement);
                sb.AppendLine("");

                foreach (var usingNamespace in usingNamesapceSet)
                {
                    if (!string.IsNullOrEmpty(usingNamespace))
                    {
                        sb.AppendIndentLine(BufferCodeExporter.GetNamespaceStatement(language, usingNamespace, NamespaceMode.Using));
                    }
                }
            }
            else if (language == Language.CSharp)
            {
                foreach (var usingNamespace in usingNamesapceSet)
                {
                    if (!string.IsNullOrEmpty(usingNamespace))
                    {
                        sb.AppendIndentLine(BufferCodeExporter.GetNamespaceStatement(language, usingNamespace, NamespaceMode.Using));
                    }
                }
                sb.AppendLine("");
                sb.AppendIndentLine(namesapceStatement);
                AddLeftBrace(sb, language);
                return(true);
            }
            return(false);
        }
Пример #4
0
    //复杂类型函数导出
    public static string GenerateFunctionCode(Language language, string type, CodeMode codeMode)
    {
        List <object> nestArray    = TypeName2NestArray(type);
        string        code         = string.Empty;
        object        element      = nestArray[0];
        string        functionName = NestArray2FunctionName(language, nestArray, codeMode);

        if (element is string)
        {
            string prefixType = element as string;
            string typeForGetArrayDemision = type;
            int    array = GetArraySuffix(ref prefixType);
            if (array == 0)
            {
                array = GetArraySuffix(ref typeForGetArrayDemision);
            }
            if (array > 0) //Array
            {
                string nestStatement = GetFunctionStatement(language, codeMode, GetArrayElementTypeName(type), "value[i]");
                return(BufferCodeExporter.GetArrayFunctionCode(language, type, functionName, codeMode, nestStatement));
            }
            else if (prefixType == CodeGenConstants.ComplexType[1])//List
            {
                string nestStatement = GetFunctionStatement(language, codeMode, GetListGenericTypeName(type), "value[i]");
                return(BufferCodeExporter.GetListFunctionCode(language, type, functionName, codeMode, nestStatement));
            }
            else if (prefixType == CodeGenConstants.ComplexType[2])//Dictionary
            {
                string[] genericTypes   = GetDictionaryGenericTypeName(type);
                string   nestStatement1 = GetFunctionStatement(language, codeMode, genericTypes[0], "kv.Key");
                string   nestStatement2 = GetFunctionStatement(language, codeMode, genericTypes[1], "kv.Value");

                return(BufferCodeExporter.GetDictionaryFunctionCode(language, type, functionName, codeMode, nestStatement1, nestStatement2));
            }
            else if (Array.IndexOf(CodeGenConstants.BaseType, prefixType) == -1) //custom class
            {
                return(BufferCodeExporter.GetCustomClassFunctionCode(language, type, functionName, codeMode));
            }
        }
        return(code);
    }