Using Ja.Net to integrate Java and DotNet(c#): Class and Type

2009-09-26 07:39:00 | , , , , , ,

How to use IKVM to integrate Java to DotNet

Using Ja.Net to integrate Java and DotNet(c#): Basic

Using Ja.Net to integrate Java and DotNet(c#): Class and Type

Using Ja.Net to integrate Java and DotNet(c#): More detailed basic issues

Using Ja.Net to integrate Java and DotNet(c#): Encoding and Properties

Using Ja.Net to integrate Java and DotNet(c#): Class implementing and inheriting

Using Ja.Net to integrate Java and DotNet(c#): Assemblies in Java Source code

Using Ja.Net to integrate Java and DotNet(c#): IO in Java and DotNet

OK, let’s don some test for verify if Ja.Net matches our requirements.

The mainly goal of this test case is verify whether Java and DotNet could find classes/objects by names. Let’s suppose there was some function looks like this:

//Java Source code

public void testFunction(Class cls)

{

//Some source code

}

How to invoke this method in C#? C# does not know what the “Class” is? it only know what the “Type” is. We are going to know how to create a Java object-instance in C# and how to create a DotNet object-instance in Java, both by class and by name.

Let’s check what the term “Type” is in Java VM.

package jatest;
public class case001
{
    private static void showClassHierarchy(Class cls)
    {
        if (null == cls)
        {
            System.Console.WriteLine("....Class is not existed!");
        }
        while(null!=cls )
        {
            System.out.println("...."+cls.getName());
            cls=cls.getSuperclass();
        }
    }
    private static void showClassHierarchy(System.Type cls)
    {
        showClassHierarchy(null==cls?(Class)null:cls.getClass());
    }
    public static void main(String[] argvs)
    {
            Class cls=null;
            try
            {
                System.out.println("What's 'Type'?");
                showClassHierarchy(Class.forName("System.Type"));
                System.out.println("What's 'System.String'?");
                System.String dotNetString="A";
                showClassHierarchy(dotNetString.GetType().getClass());
                System.out.println("What's 'System.Int32'?");
                System.Int32 dotNetInt=10;
                showClassHierarchy(dotNetInt.GetType().getClass());
                System.out.println("What's 'System.Console'?");
                showClassHierarchy(Class.forName("System.Console"));

                System.out.println("What's 'System.Object'?");
                showClassHierarchy(Class.forName("System.Object"));

                System.out.println("What's 'java.lang.String'?");
                showClassHierarchy(java.lang.String.class);               

                System.out.println("Is it possible that create a DotNet 'Type' from name?");
                showClassHierarchy(System.Type.GetType("System.Console"));

                System.out.println("Is it possible that create a java 'Type' from name?");
                showClassHierarchy(System.Type.GetType("java.lang.Class"));
            }
            catch(Exception excep)
            {
                excep.printStackTrace();
            }
    }
}

The result is:

What's 'Type'?
....System.Type
....System.Reflection.MemberInfo
....java.lang.Object
What's 'System.String'?
....System.RuntimeType
....System.Type
....System.Reflection.MemberInfo
....java.lang.Object
What's 'System.Int32'?
....System.RuntimeType
....System.Type
....System.Reflection.MemberInfo
....java.lang.Object
What's 'System.Console'?
....System.Console
....java.lang.Object
What's 'System.Object'?
....java.lang.Object
What's 'java.lang.String'?
....java.lang.String
....java.lang.Object

Is it possible that create a DotNet 'Type' from name?
....System.RuntimeType
....System.Type
....System.Reflection.MemberInfo
....java.lang.Object
Is it possible that create a java 'Type' from name?
....Class is not existed!

The result of this test means:

  • Java can create a object instance by Class.forName, the names generated by DotNet language are in the same namespace which Java is using.
  • The DotNet type “System.Type” is not a sub-class of “java.lang.Class”, it can not be treat as a instance of “java.lang.Class”, so we can not use the DotNet style invoking “GetType” to retrieve the type of some objects created by DotNet.
  • In Java source code, the DotNet primitive types are sub-class of “System.Type”, it is quite different with the primitive types in Java.
  • The Root objects in Java and in DotNet are similar.
  • In Java source code, DotNet Type.GetType() can create classes which generated by DotNet.
  • In Java source code, DotNet Type.GetType() CAN NOT create classes which generated by Java.

Let’s check the same thing in DotNet. Some cases are different in DotNet which we can not describe them by program.

  • DotNet can not create any object created by Java by using Type.GetType().
  • The class java.lang.Class DOES NOT inherit from System.Type.
  • The primitive (String, int, double, and so on.) types in Java and DotNet are same!
  • java.lang.Class can create DotNet objects, but System.Type CAN NOT create java object.
  • Can not convert directly between System.Type and java.lang.Class.

Let’s Check some source code:

abstract class ICase
{
    public abstract void RunCases();
    public void Msg(String s)
    {
        System.Console.WriteLine(s);
    }
    public void ShowClassHierarchy(java.lang.Class t)
    {
        if (null == t)
        {
            System.Console.WriteLine("....Type is not existed!");
        }
        while (null != t)
        {
            System.Console.WriteLine("...." + t.getName());
            t = t.getSuperclass();
        }
    }
    public void ShowClassHierarchy(Type t)
    {
        if (null == t)
        {
            System.Console.WriteLine("....Type is not existed!");
        }
        while (null != t)
        {
            System.Console.WriteLine("...." + t.FullName);
            t = t.BaseType;
        }
    }
}

 

class case001:ICase
    {
        public override void RunCases()
        {
            Msg("What's 'java.lang.Integer'?");
            java.lang.Integer javaInt = new java.lang.Integer(10);
            ShowClassHierarchy(javaInt.GetType());

            Msg("Is it possible that create a DotNet 'Type' from name?");
            ShowClassHierarchy(Type.GetType("System.Console"));

            Msg("Is it possible that create a java 'Type' from name?");
            ShowClassHierarchy(Type.GetType("java.lang.Integer"));

            Msg("What's 'Class'?");
            ShowClassHierarchy(typeof(java.lang.Integer));

            Msg("What's 'System.Console'?");
            ShowClassHierarchy(java.lang.Class.forName("System.Console"));
        }
    }

 

The result of this program is:

What's 'java.lang.Integer'?
....java.lang.Integer
....java.lang.Number
....System.Object
Is it possible that create a DotNet 'Type' from name?
....System.Console
....System.Object
Is it possible that create a java 'Type' from name?
....Type is not existed!
What's 'Class'?
....java.lang.Integer
....java.lang.Number
....System.Object
What's 'System.Console'?
....System.Console
....java.lang.Object


Jeason Zhao (沈胜衣,斛律光) ------雪饮再现,一个人的江湖
我知道我是谁,我是沈胜衣,默默的活着,就像空气。

Comments (3) -

Franceriad :

Right what I was searching for, understand it for posting . “There are innumerable victories worse than a debacle.” by George Eliot.

Francevoir Marrakech :

I gotta special this locale it appears same cooperative same desirable

Francevisite Marrakech :

I like this fabric point precise many so many impressive info . “Fate wills your relations, you take your patrons.” by Jacques Delille.

Add comment




biuquote
  • Comment
  • Preview
Loading