views:

84

answers:

3

I am using jni4net to access Java code from within a C# application, and vice-versa. jni4net uses reflection to generate JNI code proxies, so obviously one of the limitations is that your Java and C# code have to compile in order to build proxies.

Unfortunately this can result in a catch-22 problem. Consider:

C# class X uses Java class Y
Java class Y uses C# class X

Neither can be compiled, so the well established workaround is to take one of the classes (either X or Y), strip it down to its bare signature and get it to compile and then generate the proxy from the compiled skeleton. You can then replace the stripped class with the original and go on your merry way.

This seems like an ugly approach to me and I believe there should be a better way. An obvious solution would be to tell the compiler (either C# or Java, it really does not matter which) to ignore references to the missing class.

Is ignoring references to a certain missing class feasible for either the C# or Java compilers? Is there a better way to do this (and no, I am not open to considering sockets or anything of that nature; I require true interop between .NET and Java)?

Example code was requested:

Example code with jni4net bridge code removed for clarity. IA and IB are simple interfaces also not included.

Java:

public class A implements IA  
{  
    public void m1()  
    {  
        System.out.println("m1 called");  
    }  

    public static void main (String args[])  
    {  
        IB b = new B();  
        b.m2(new A());  
    }  
}  

C#:

public class B : IB  
{  
    public void m2(IA a)  
    {  
        a.m1();  
        A a2 = new A();  
        a2.m1();  
    }  
}  
A: 

In C# 4 you might be able to use the dynamic keyword when accessing the Java object. Haven't tried your scenario exactly. Some example code would help.

edit for updated question
You have a circular dependency that a single compiler will likely complain about, let alone two. Refactoring to something more like the following will help. Of course it probably won't be that simple in your real code.

// java
public class A implements IA  
{  
    public void m1()  
    {  
        System.out.println("m1 called");  
    }  
}

public class MainClass
{
    public static void main (String args[])  
    {  
        IB b = new B();  
        b.m2(new A());  
    }  
}  

//C#

public class B : IB  
{  
    public void m2(IA a)  
    {  
        a.m1();  
        A a2 = new A();  
        a2.m1();  
    }  
}
BioBuckyBall
See example code I just added.
Chuck Wolber
Yes, I am not sure that would work for my use case, but it is an interesting approach nonetheless.
Chuck Wolber
+1  A: 

I ended up solving the problem on my own. It did not involve my original hunch, so I changed the title of this problem to better reflect the actual issue.

While the jni4net package has a lot of useful examples, there did not seem to be any good ones showing object passing in both directions without some strange build gymnastics (see original problem statement). I figured out how to do it and present the solution here along with the commands required to build the result.

The easiest way to get this running is to set up jni4net as the instructions say and drop all of this stuff into a new sub-directory of the sample directory that comes with the jni4net package.

First the C# code:

src/test/left.cs

namespace test
{
    using System;
    using net.sf.jni4net;
    using test;

    public class left : iright
    {
        public static void Main(String[] args)
        {
            left l = new left();
            l.sendMeToRight();
        }

        public left()
        {
            Console.WriteLine("left side constructed...");
        }

        public void sendMeToRight()
        {
            BridgeSetup bridgeSetup = new BridgeSetup();
            bridgeSetup.AddAllJarsClassPath(".");
            Bridge.CreateJVM(bridgeSetup);
            Bridge.RegisterAssembly(typeof(right).Assembly);

            Console.WriteLine("Sending myself to right.");
            right il = new right(this);
            il.announceMyself();
        }

        public void announceMyself()
        {
            Console.WriteLine("Hello from the left side...");
        }
    }
}

And now the Java code:

src/test/iright.java

package test;

public interface iright
{
    void announceMyself();
}

src/test/right.java

package test;

public class right implements iright
{
    public right(iright f)
    {
        System.out.println("right side constructed... ");
    f.announceMyself();
    }

    public void announceMyself()
    {
        System.out.println("Hello from the right side...");  
    }    
}

And finally the build script (I built it to run in cygwin, and on very short notice, so alter as needed.):

@echo off
rm -rf build
mkdir build
mkdir build\test
copy ..\..\lib\*.* build

echo Compile base classes.
javac src/test/*.java
mv src/test/*.class build/test

echo Creating jar file.
jar cvf build\lr.jar -C build test/iright.class -C build test/right.class
rm build/test/*.class

echo Generating proxies.
..\..\bin\proxygen.exe build\lr.jar -wd build

echo Compiling derived proxy classes.
javac -cp build\lr.jar;build\jni4net.j-0.8.0.0.jar build\jvm\test\iright_.java build\jvm\test\right_.java

echo Packing compiled derived proxy classes.
jar cvf build\lr.j4n.jar -C build\jvm test\__iright.class -C build\jvm test\iright_.class -C build\jvm test\right_.class

echo Generating derived proxy DLL.
cd build
csc.exe /nologo /warn:0 /t:library /out:lr.j4n.dll /recurse:clr\*.cs /reference:"c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll" /reference:jni4net.n-0.8.0.0.dll

echo Generating product executable.
csc.exe /nologo /warn:0 /out:demo.exe /target:exe /reference:jni4net.n-0.8.0.0.dll /reference:lr.j4n.dll ..\src\test\left.cs
Chuck Wolber
A: 

Because it seems you own both classes, you could add interfaces IX and IY and implement them in respective classes. The interfaces could be compiled into separate DLL/JAR to enable smooth compilation of X,Y classes.

This example is bit complicated, but leads to proposed result.

Pavel Savara
Pavel - That is what lead me on this odyssey in the first place. That example, IMHO, is a poor one because it requires a swapping of the B.cs code from one version to another. I needed to see something that did not involve altering source code at build time.
Chuck Wolber