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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM