Archive for the 'C# & Java conversion' Category

Jul 15 2009

Get involved

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

CSharpJavaMerger Framework is free and open source. It is developed by Dynamsoft. CSharpJavaMerger Framework isn’t perfect yet. Developers who are interested at the software are most welcome to get involved in the following ways:

  • Edit web pages
    The ones register will be granted with commit right to the wiki-type articles. To join KevinGao.net, you can register an account here:
    http://www.kevingao.net/wp-login

    After registering, you will receive an email including the password for your account. You can then log in KevinGao.net and make changes to the article.

    You can find the wiki-type articles in Recent contributions to Wiki section at the bottom of Dashboard page.
     

  • Write Code
    If you wish to contribute source code of CSharpJavaMerfer via SCM Anywhere Hosted, please contact Kevin Gao at kgao@dynamsoft.com with notice of your intension. He will give you an account of SCM Anywhere Hosted with change/commit right.
     
  • Submit feature requests or bug reports
    If you have any feature requests or find any bugs, you can submit or update tickets in the Issue Tracking Explorer of SC M Anywhere Hosted:
    http://www.scmsoftwareconfigurationmanagement.com/help/SCM%20Hosted/Issue-Tracking/Issue.htm

Links:
Previous article >>>>: License
Next article >>>>:
WCF & Java Interop series home page: CSharpJavaMerger Framework

One response so far

Jul 15 2009

License

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

CSharpJavaMerger is distributed under General Public License (GPL).

 

Links:
Previous article >>>>: How to write Java code?
Next article >>>>: Get involved
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

How to write Java code?

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!
  • Use “class” instead of “interface” in enumerations and add “public final static” keywords before enumerations.For example:

    public class EnumEOL
    {
      public final static int enumEOLNative = 0;
      public final static int enumEOLWindowsCRLF = 1;
      public final static int enumEOLUnixCR = 2;
      public final static int enumEOLMacLF = 3;
    }
  • Don’t use “e.printStackTrace” in try… catch and System.out.println(e.getStackTrace(…)) in classes.
  • Don’t use annotations like @Override, @suppresswarning.
  • Use “CompareTo” and “CompareToIgnoreCace” to compare strings. Don’t use “equels ()” or “==”.
  • Replace all “String” with “CString”, which is a class defined in the CSharpJavaMerger Framework, except for the following two places:
    • Don’t change the “String” in “String Object”.
    • Don’t change the “String” in string constant. For example, public final static String VSSVER= “vssver.scc”

    Two things to notice:

    • Use “string == null” instead of “CSting == null || CString.getValue() == null”.
    • The logic of Substring (int, int) is changed. The meaning of the 1st parameter is the same in C# and Java, while the 2nd one not. The 2nd parameter is the subscript of the substring in Java, and it means the length of the substring in C#. The substring of “CString” here is same with substring in C#.
  • Use Debug.Assert (b) instead of assert (b). Debug is a class defined in CSharpJavaMerger Framework.
  • Omit “public” keyword in the member functions in interfaces.
  • Make sure the subclasses’ functions, including returned value, function names, parameter list, are different from that of the superclass prototype, except implementation classes of Abstract and interface.
  • The name convention for variables of checkbox control variables: m_chk, for example, m_chkUseSSL.
  • Exception processing
    • Only catch the exceptions in java.lang.Exception, which is a base class of Java.
      After caching an exception, if any functions in java.lang.Exception need to be called, please add the objects in the constructor of CException, new an object of CException and call the according function of CException.For example:
      try
        {
          //...
        }
        catch(Exception e1)
        {
          CException exp = new CException(e1);
          CString str = exp.GetMessage()
        }
    • Use e.GetMessage() instead of e.getLocaleMessage().
  • File, I/O classes processing
    • Use CFile instead of File. CFile is a class in CSharpJava Framework. The class is used to judge file path, whether file exists or not, file’s properties, and whether files is deleted.
    • Use FileStream to read/write binary.
    • Use StreamWriter/StreamReader to read/write text.
    • Use CFileInputStream to read all files.
    • Use CFileOutputStream to write to all files.
  • Use “DateTime” instead of “Calendar”. DateTime is a function in CSharepJavaMerger Framework.
  • Use Guid instead of UUID.
  • Omit Constructors called by Constructors. Replace “this ()” in Constructors with corresponding functions.
  • Add “break” before Default in switch case.
  • Use Boolean instead of Boolean. Boolean is a system class.
  • Replace Anonymous inner classes with inner or common classes.
    • Use List, Map etc instead of ArrayList, HashMap for statement. For example:
      Use: CList list = CList();
      Instead of :ArrayList list = ArrayList()
  • There may be some tricks when adding strings. For example:
    • CString str1 = new CString(”hello”);
    • CString str2 = new CString(”world” + str1);
      //This line can be compiled successfully.
      //However, what you want may be CString str2 = new CString(”World” + str1.getValue());
  • Use CThread instead of Thread.
  • Use DESUtil instead of DES.
  • Capitalize the name of all packages.
  • Replace the function used for sorting “Collections.sort(listChangeSet,comparator);” with “list.Sort(Comparer comparer);”. Compare in a class in CSharepJavaMerger Framework.
    For example, use the following code:

    class SortListComparator extends Comparer<Object>
    {
      public int Compare(Object obj1, Object obj2)
      ...
    }

    Rather than:

    class SortListComparator implements Comparator<Object>
    {
      public int compare(Object obj1, Object obj2)
      ...
    }
  • Use CMap instead of Map. For example:
    Use:
    “CMap map = new CMap();”
    Instead of:
    “Map map = new HashMap();”
  • Don’t use Integer and Long system functions.
  • Change private inner constructors to be public.
  • If a member variable of a private inner class needs to be used by its external classes, please change the member variable to public.
  • If you want to convert between CString and Long, integer, please use Str.ToLong() instead of new Long(CString.GetValue).longValue().
  • Add [] before array definition like this:
    String [] var = null
  • Don’t use keywords as variables. For example,
    Do not use “out” which is in CFileStream out;.
  • Don’t get the string out of CString and then call the functions of String. For example:
    • Don’t use the following way: CString str; str.GetValue().compareTo
    • Other classes either. Don’t get the system classes and then call system functions.
  • Don’t use the following functions:
    • Integer.intValue()
    • new Integer()
    • new Long
    • Long.longValue()
  • If conditional expression (? : ) uses string, it’s not CString.

 

Links:
Previous article >>>>: How to write C# code?
Next article >>>>: License
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

How to write C# code?

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!
  • Use “base” instead of “super”.
  • Use “namespace” instead of “package”.
  • Use “using” instead of “import”. You can comment the import section and use the Resolve function of Visual Studio 2008 to add “using” automatically.
  • Omit “throw Exception” in the end of the functions.
  • Using “delegate callback” when accessing UI resources through workers threads.Below is the example of closing dialog boxes in workers thread:
    • Java
      public void closeDialog()
      {
        m_isCanceled = false;
        Display.getDefault().syncExec(new Runnable()
        {
          public void run()
          {
            Shell shell = getShell();
            if (shell != null)
            {
              if (!shell.isDisposed())
              {
                shell.dispose();
              }
            }
          }
        });
      }
    • C#
      delegate void GUICallback(string pos);
      //Function declaration.
      //Please note the prototype of the function is certain:
      //the return value is void and the input parameter is string.
      private void closeDialogInner(string strMessage)
      {
        if (this.InvokeRequired)
        //This is a system function.
        //If this function needs to access some control resources,
        //please change "this" to the name of the control,
        //for example: this.m_btnOK.InvokeRequired.
        {
          GUICallback callback = new GUICallback(closeDialogInner);
          this.Invoke(callback, new object[] { strMessage });
        }
        else
        {
          this.Close();
        }
      }
  • Replace Display.getDefault().getActiveShell() with Form.ActiveForm(). This function may be not used much.
  • Use “is” instead of “instanceof”.
  • Replace Integer.MAX_VALUE with int.MaxValue and replace Long.MAX_VALUE with long.MaxValue.
  • An important thing: please keep the variables of interfaces’ controls consistent.

    Don’t change logic. Don’t comment codes if you don’t need to. Please try to convert a function at a dash.
    These are the recommended steps:

    • Transfer util class.
    • Transfer IView class.
    • Draw dialog boxes. Please make sure the variables of the control names in C# and Java are the same.
    • Implement the functions in IView.
    • Convert the controller classes.
      • Convert thread classes
      • Convert the Dialog control classes
    • Add events for controls
    • Copy the implementing code of Copy event.
      One thing to notice: The constructor of Form calls InitializeComponent() directly, but in Java, createContents is called only when openDialob, so the time to create controls are different in C# and Java. The time to call FormLoad(…) method in C# is similar to that of createContents in Java.
  • Replace “final” in local constant with “const”.
  • Omit “final” in parameters.
  • Use “public const” instead of “public final static”.
  • Replace “length” with “Length” in the member of array’s length.
  • Using Long:
    • Using Long= System.Int64;
    • Integer.MaxValue->Integer.MAX_VALUE
  • Using Integer:
    • Using Integer = System.Int32
    • Long.MaxValue->Long.MAX_VALUE

 

Links:
Previous article >>>>: An example
Next article >>>>: How to write Java code?
WCF & Java Interop series home page: CSharpJavaMerger Framework

One response so far

Jul 15 2009

An example

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

Below is a piece of code of a method. In the sample, CString, a class defined in the CSharpJavaMerger Framework, is used instead of String. This way, the method AddTwoValues can be used in both C# and Java.

public void AddTwoValues()
{
    try
    {
        CString strValue1 = m_mainForm.GetFirstTextBoxValue();
        CString strValue2 = m_mainForm.GetSecondTextBoxValue();

        int iValue1 = strValue1.ToInteger();
        int iValue2 = strValue2.ToInteger();

        int iResult = iValue1 + iValue2;
        CString strShowResult = CString.ValueOf(iResult);
        m_mainForm.ShowResult(strShowResult);

        CString strSaveResult = new CString(strValue1.GetValue() + " + " + strValue2.GetValue() + " = " + strShowResult.GetValue() + "\n");
        m_mainForm.SaveResult(strSaveResult);
    }
    catch (Exception ee)
    {
        m_mainForm.SaveResult(new CString(ee.Message + "\n"));
        m_mainForm.ShowResult(new CString("Error"));
    }
}

 

Links:
Previous article >>>>: Architecture
Next article >>>>: How to write C# code?
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

Architecture

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

CSharpJavaMerger Framework Architecture
(CSharpJavaMerger Framework Architecture)

 

Links:
Previous article >>>>: I’m a Java developer. How to use CSharpJavaMerger?
Next article >>>>: An example
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Next »

Version Control Software/System | Source Control Software/System | Software Configuration Management | SCM Hosting Solution | Bug Tracking System
SourceSafe (VSS) Replacement/Alternative | SourceSafe (VSS) Hosting | SourceSafe (VSS) Remote/Web/Internet Access | Scanner COM
Customer Service Software | Live Chat | Live Help | Forum Software | Knowledge Base Software | Newsletter Email Marketing Software