Java To CSharp源代码转换

前言

开发环境

客户端:Unity3D开发(C#)

服务器:Java (基于Java7)

日   期:2016年09月

需求说明

部分服务器的部分逻辑功能在客户端实现一遍,可以简单的理解为服务器的部分逻辑代码搬到客户端来实现一遍。

想到的办法是代码转换。有两个原因:

  • 时间问题,把时间用在更有意义的事情上。
  • 解放双手和大脑,无须把相同的代码写两遍。

源代码转换法

源代码转换就是指把Java的代码转换成C#的源代码。

这样做的好处是方便后续的开发和调试,有源码更放心。

以下是几个工具,用于Java源码转换到C#源码

Java Language Conversion Assistant

文档:https://msdn.microsoft.com/zh-cn/library/1kwtxa08(v=vs.80).aspx

下载:https://www.microsoft.com/en-us/download/details.aspx?id=14349

 

visual studio 2008及之前的老版本集成此工具,但从visual studio2010起已不提供,故放弃之。

j2cstranslator

下载:https://sourceforge.net/projects/j2cstranslator/

介绍:http://www.cnblogs.com/Lawson/archive/2012/02/21/2361827.html

 

开源,免费,但13年至今未更新

octopus .NET Translator

官网:http://www.remotesoft.com/octopus/

 

收费,未进行详细了解

Java to C# Converter

官网:http://www.tangiblesoftwaresolutions.com/Product_Details/Java_to_CSharp_Converter.html

介绍:http://www.cnblogs.com/yiyan127/p/CSharp_CrackJava2CSharpConverter.html

 

收费,免费版有1000行代码限制。看了官网介绍后,决定使用它。

XES – Java To C#

官网:http://www.euclideanspace.com/software/language/xes/userGuide/convert/javaToCSharp/index.htm

下载:https://sourceforge.net/projects/xes/files/OldFiles/xes_java_runtime_alpha06.zip/download

 

免费,似乎用起来并不那么理想,目前的最新版本是2004年,未有更新,故放弃之。

Java to C# Converter

经过对比之后,我选择了Java to C# Coverter,此工具的更多详情,可以在官网的介绍中查看

转换过程中的信息信息,会出现在对话框中,同时也会标注在转换后的代码中。

Java-To-CSharper

虚拟机运行法(IKVM)

本小节主要是说 IKVM在Unity中的使用。

IKVM下载:https://github.com/Unity-Technologies/kaizen/tree/master/bundles/IKVM.NET

IKVM和Unity

话题讨论

http://forum.unity3d.com/threads/building-project-with-ikvm-dlls-inside.101097/

 

JavaToDll导出

下图中,上图是Java的源代码,下方是转换成Dll后反编译查看的代码。

IKVM-转换Java-To-Dll

 

我的测试

引擎版本:Unity 4.0 / Unity 5.3.5 (目标平台测试过 Windows和Android 平台)

IKVM:ikvm-7.2.4630.5

OS:Windows 7 x64

  1. 从git或官网下载ikvm,比如我下载的ikvm-7.2.4630.5.zip,并解压
  2. 拷贝ikvm-7.2.4630.5\bin\下的所有dll 到Unity的Assets\Plugins
  3. 拷贝Java转换出的dll,放到Assets\Plugins 下,比如我的hello.dll
  4. 在Unity的脚本中调用java中的class , method 等等

下方是我测试过程中出现的Error,出于性能和后期调试考虑,我放弃了此种方式,采用将Java代码转换成C#源码的方式。

已知Error

当在脚本的全局变量,返回值,协程中引用了java中的class,method时,就会报以下Error。

private ExampleLibrary exampleLibrary2;
    IEnumerator CoLog()
    {
        int idx = 0;
        ExampleLibrary exampleLibrary = new ExampleLibrary();
        while (idx < 100)
        {
            DoLog(exampleLibrary.HelloWorld());
            yield return null;
            idx++;
        }
    }

但如果是内部变量则不会有这些Error。

void TestLog()
    {
        ExampleLibrary exampleLibrary = new ExampleLibrary();
        for (int idx = 0; idx < 20; idx++)
        {
            DoLog(exampleLibrary.HelloWorld());
        }
    }

运行时Error

GameObject (named 'Main Camera') references runtime script in scene file. Fixing!
The script behaviour 'IKVM_Java_HelloWorld' could not be instantiated!

 

Project中选中脚本时的Error

TypeLoadException: Could not load type 'IKVM_Java_HelloWorld' from assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
System.MonoType.GetFields (BindingFlags bindingAttr) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/MonoType.cs:164)
UnityEditor.MonoScriptImporterInspector.ShowFieldInfo (System.Type type, UnityEditor.MonoImporter importer, System.Collections.Generic.List`1 names, System.Collections.Generic.List`1 objects, System.Boolean& didModify) (at C:/buildslave/unity/build/Editor/Mono/Inspector/MonoScriptInspector.cs:75)
UnityEditor.MonoScriptImporterInspector.OnInspectorGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/MonoScriptInspector.cs:117)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1231)
UnityEditor.DockArea:OnGUI()

 

测试代码review

https://github.com/zhaoqingqing/blog_samplecode/tree/master/technical-research/java-to-csharp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个很强大的工具, 能将c#代码片段、文件甚至工程直接转换java代码,并能彻底解决外部引用的DLL问题,最强的是支持c#工程的直接转换,生成的Java代码质量也很不错。软件已破解,去除了未注册版最多只能转换1000行的限制,亲测可用!压缩包内含帮助文档,不过由于软件的使用很简单,帮助文档基本可以忽略。(小提示:如无法运行,请确认是否安装了.NET framework) 下面是一些英文介绍: C# to Java Converter features include: 1. Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h missing declarations. (代码片段和文件转换) 3. Full support for the latest .NET code syntax: Generics and other newer C# features are converted where possible to the Java equivalent.(全面支持最新版的.NET语法) 4. Conversion options: You can specify numerous conversion options via our Options dialog. Code formatting options, custom type & member replacements, custom string replacements, and miscellaneous options. (可以指定转换规则) 5. File comparison feature: After converting a project or folder, you can compare original and converted files side-by-side. Comparing converted code to original code.(原代码与转换后代码的比较) 6. Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. Converts all versions of C# code (2002, 2003, 2005, 2008, and 2010) (可以转换所有版本的C#代码) 2. Evaluates all referenced assemblies and .NET projects in order to resolve external references more completely.(能彻底解决外部引用的dll类库) 3. Converts C# ref parameters using Java generics 4. Superb conversions of all types of arrays 5. Handles the numerous coding alternatives and ambiguities of C# code 6. Flawless conversion of all aspects of inheritance and interfaces 7. Allows custom replacement of strings in the final converted code 8. Accurate even with poorly formatted C# code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值