2018-10-17 22:54:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace CoderEngine
|
|
|
|
|
{
|
|
|
|
|
public class JavaCodeWrite
|
|
|
|
|
{
|
|
|
|
|
private string CodeStr = "";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 一个代码缩进
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string tabstr = " ";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 逐行添加代码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="codeline"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public void CoderAddLine(string CodeLine)
|
|
|
|
|
{
|
|
|
|
|
CodeStr += CodeLine + "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ModelToJavaCode(string _AssemblyName, string _TypeName)
|
|
|
|
|
{
|
2018-10-18 07:00:43 +08:00
|
|
|
|
TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
|
2018-10-17 22:54:58 +08:00
|
|
|
|
ModelToJavaCodeWriter(tm);
|
|
|
|
|
return CodeStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ModelToJavaCodeWriter(TypeMode TopMode)
|
|
|
|
|
{
|
|
|
|
|
//如果有代码嵌套的下级类 在本类外 写入新的类
|
|
|
|
|
if (TopMode.TypeList != null && TopMode.TypeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine(tabstr);
|
|
|
|
|
JavaCodeDownRecursion(TopMode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void JavaCodeDownRecursion(TypeMode TopMode)
|
|
|
|
|
{
|
|
|
|
|
string ClassName = "";
|
|
|
|
|
//如果自己就是集合类型 则取类型下名字
|
|
|
|
|
if (TopMode.MemberType.Name == "List`1")
|
|
|
|
|
{
|
|
|
|
|
//FullName的值为 "System.Collections.Generic.List`1[[ --集合内嵌类型名称--, CoderEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"
|
|
|
|
|
//其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型”
|
|
|
|
|
var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2);
|
|
|
|
|
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
|
|
|
|
|
ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".")+1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ClassName = TopMode.MemberType.Name;
|
|
|
|
|
|
|
|
|
|
CoderAddLine("public class " + ClassName + " {");
|
|
|
|
|
foreach (var tl in TopMode.TypeList)
|
|
|
|
|
{
|
2018-10-18 10:59:53 +08:00
|
|
|
|
CoderAddLine("");
|
|
|
|
|
if (tl.MemberType.Name == "List`1")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine(tabstr + "@SerializedName(value = \"" + tl.Name + "\")");
|
|
|
|
|
}
|
|
|
|
|
CoderAddLine(tabstr + "private " + CSharpToJavaTypeName(tl.MemberType) + " " + tl.Name + ";");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CoderAddLine("");
|
|
|
|
|
CoderAddLine(tabstr + "//"+ ClassName + "中所有属性对应get set 方法");
|
|
|
|
|
|
|
|
|
|
foreach (var tl in TopMode.TypeList)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine("");
|
|
|
|
|
CoderAddLine(tabstr + "public " + CSharpToJavaTypeName(tl.MemberType) + " get" + tl.Name + "(){return " + tl.Name + ";}");
|
|
|
|
|
CoderAddLine("");
|
|
|
|
|
CoderAddLine(tabstr + "public void set" + tl.Name + "(" + CSharpToJavaTypeName(tl.MemberType) + " " + tl.Name + "){return this." + tl.Name + " = " + tl.Name + ";}");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
2018-10-18 10:59:53 +08:00
|
|
|
|
CoderAddLine("");
|
|
|
|
|
CoderAddLine(tabstr+"//为该实体类生成的toString打印方法");
|
|
|
|
|
CoderAddLine(tabstr+ "@Override");
|
|
|
|
|
CoderAddLine(tabstr + "public String toString() {");
|
|
|
|
|
CoderAddLine(tabstr + tabstr + "return \"CommentInfoModel{ \" +");
|
|
|
|
|
foreach (var tl in TopMode.TypeList)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine(tabstr + tabstr + tabstr + "\"" + tl.Name + "='\" + " + tl.Name + " + '\\'' + \",\" ");
|
|
|
|
|
}
|
|
|
|
|
CoderAddLine(tabstr + tabstr + tabstr + "'}';");
|
|
|
|
|
|
|
|
|
|
CoderAddLine(tabstr + "}");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
|
|
|
|
|
CoderAddLine("}");
|
2018-10-18 10:59:53 +08:00
|
|
|
|
|
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
foreach (var tl in TopMode.TypeList)
|
|
|
|
|
{
|
|
|
|
|
if(tl.MemberType.Name == "List`1")
|
|
|
|
|
{
|
|
|
|
|
if (tl.TypeList != null && tl.TypeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine("");
|
|
|
|
|
JavaCodeDownRecursion(tl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-18 10:59:53 +08:00
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 转换成Java代码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string CSharpToJavaTypeName(Type myType)
|
|
|
|
|
{
|
|
|
|
|
var oneinfo = CSharpToJavaTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault();
|
|
|
|
|
if (oneinfo.Value != null)
|
|
|
|
|
{
|
|
|
|
|
return oneinfo.Value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(myType.Name == "List`1")
|
|
|
|
|
{
|
|
|
|
|
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
|
|
|
|
|
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
|
|
|
|
|
return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.')+1) + ">";
|
|
|
|
|
}
|
2018-10-18 07:00:43 +08:00
|
|
|
|
else if (myType.Name == "Nullable`1")
|
|
|
|
|
{
|
|
|
|
|
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
|
|
|
|
|
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
|
|
|
|
|
return SonParamName.Substring(SonParamName.LastIndexOf('.')+1);
|
|
|
|
|
}
|
2018-10-17 22:54:58 +08:00
|
|
|
|
else
|
|
|
|
|
return "string";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// C#和Java代码对照表
|
|
|
|
|
/// </summary>
|
|
|
|
|
public System.Collections.Generic.Dictionary<string, string> CSharpToJavaTypeItem =
|
|
|
|
|
new Dictionary<string, string>() {
|
|
|
|
|
{ "int","int"},
|
|
|
|
|
{ "Int16","int"},
|
|
|
|
|
{ "Int32","int"},
|
|
|
|
|
{ "Int64","int"},
|
|
|
|
|
{ "int?","int"},
|
|
|
|
|
{ "long","long"},
|
|
|
|
|
{ "long?","long"},
|
|
|
|
|
{ "float","float"},
|
|
|
|
|
{ "float?","float"},
|
|
|
|
|
{ "double","double"},
|
|
|
|
|
{ "double?","double"},
|
|
|
|
|
{ "boolean","boolean"},
|
|
|
|
|
{ "string","string"},
|
|
|
|
|
{ "DateTime","DateTime"},
|
|
|
|
|
{ "DateTime?","DateTime"},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|