Re: [Ikvm-developers] How to pass a Java callable to C#?
Brought to you by:
jfrijters
|
From: Jeroen F. <je...@su...> - 2014-02-18 07:10:21
|
The Delegate class I'm talking about is the .NET Delegate base class (cli.System.Delegate from IKVM). It has a bunch of CreateDelegate factory methods. You can't easily use the ones taking a MethodInfo, but you can go from a Java class to a cli.System.Type using ikvm.runtime.Util.getInstanceTypeFromClass(Class).
Regards,
Jeroen
> -----Original Message-----
> From: Doug Blank [mailto:dou...@gm...]
> Sent: Monday, February 17, 2014 19:36
> To: Jeroen Frijters
> Cc: ikv...@li...
> Subject: Re: [Ikvm-developers] How to pass a Java callable to C#?
>
> > BTW, I just realized you can also use one of the static methods on
> > Delegate to create a delegate to the specified method and simply pass
> that.
>
>
> Where is "Delegate" and what are its static methods? Sorry for being
> dense... I'm having trouble finding where to begin tackling this.
>
> I have found that, via Java reflection, I can:
>
> > widget.getClass().getMethods()
>
> which shows much of what I am looking for, including that mangled name:
>
> public final void
> cli.Widgets$Widget.on_click(cli.System.Func$$00602_$$$_Ljava__lang__Obje
> ct_$$_Ljava__lang__Object_$$$$_)
>
>
> If I try to make one of those, I get:
>
> > f = new
> > cli.System.Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_
> > $$$$_()
>
> ExecutionError: No constructor in
> Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$$$_ matches
> this invocation
> Arguments: ()
> Candidate signatures:
>
> Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$$$_(Func$$0
> 0602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$$$_.Method)
>
> Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$$$_(Object,
> IntPtr)
>
> So, I now understand what you meant about generating the Class that
> takes that Method. But if there is an easier method, I would like to do
> it in that manner.
>
> Thanks!
>
> -Doug
>
> On Mon, Feb 17, 2014 at 11:25 AM, Jeroen Frijters <je...@su...
> <mailto:je...@su...> > wrote:
>
>
> From the Java side, the on_click method signature is:
>
> on_click(Lcli/System/Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__
> lang__Object_$$$$_)V
>
> The ugliness of the name is due to generics, but not really
> relevant to this discussion.
>
> If you reflect on this class, you can determine it is a .NET
> delegate (it extends cli.System.MulticastDelegate and has a nested class
> called Method with a single Invoke method and it (the delegate class)
> has a constructor taking a reference to the inner Method interface).
>
> BTW, I just realized you can also use one of the static methods on
> Delegate to create a delegate to the specified method and simply pass
> that.
>
>
> Regards,
> Jeroen
>
> > -----Original Message-----
> > From: Doug Blank [mailto:dou...@gm...
> <mailto:dou...@gm...> ]
>
> > Sent: Monday, February 17, 2014 16:25
> > To: Jeroen Frijters
> > Cc: ikv...@li... <mailto:ikvm-
> dev...@li...>
> > Subject: Re: [Ikvm-developers] How to pass a Java callable to C#?
> >
>
> > On Sun, Feb 16, 2014 at 8:41 AM, Jeroen Frijters
> <je...@su... <mailto:je...@su...>
>
> > <mailto:je...@su... <mailto:je...@su...> > > wrote:
> >
> >
> > Hi Doug,
> >
>
> > Your C# on_click method is callable from (IKVM) Java.
> >
> > You'll need to generate a class that implements the
> >
> cli.System.Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$
> > $$_$Method interface.
> >
> >
> >
> >
> > Jeroen,
> >
> > Thanks for these details; can you expand a bit on this?
> > Where did the name
> >
> "cli.System.Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$
> > $$$_$Method" come from?
> > What is that?
> > How do you create an interface that implements it?
> >
> > I've read and googled looking for details on signatures and IKVM,
> but I
> > can't find anything that appears to be relevant.
> >
> > -Doug
> >
> >
> > This class can then be instantiated and passed to the
> constructor
> > of the
> >
> cli.System.Func$$00602_$$$_Ljava__lang__Object_$$_Ljava__lang__Object_$$
> > $$_ class (the delegate) and this object can be passed to the
> on_click
> > method.
> >
> >
> > Regards,
> > Jeroen
> >
> > > -----Original Message-----
> > > From: Doug Blank [mailto:dou...@gm...
> <mailto:dou...@gm...>
> > <mailto:dou...@gm... <mailto:dou...@gm...> > ]
> >
> > > Sent: Sunday, February 16, 2014 13:46
> > > To: Jeroen Frijters
>
> > > Cc: ikv...@li... <mailto:ikvm-
> dev...@li...> <mailto:ikvm- <mailto:ikvm->
> > dev...@li...
> <mailto:dev...@li...> >
>
> > > Subject: Re: [Ikvm-developers] How to pass a Java
> callable to C#?
> > >
> > > Jeroen,
> > >
> > > Thanks for the feedback, but I think I didn't explain
> very well
> > my
> > > problem. The Widget library is written in C# and is being
> used by
> > the
> > > Java interpreter (running on the CLR). I wish to be able
> to
> > register the
> > > callback in Java.
> > >
> > > This works in IronPython, because IronPython knows how to
> map
> > Python
> > > functions onto Func<object,object>:
> > >
> > > """
> > > from Widgets import Widget
> > > def f(obj):
> > > print("Click!")
> > > w = Widget()
> > > w.on_click(f)
> > > """
> > >
> > > I have the same working in a Scheme running on the CLR,
> because I
> > can
> > > define and wrap around callback1 in C# to map it to
> > > Func<object,object>:
> > >
> > > """
> > > (using "Widgets")
> > > (define f (lambda (obj) (printf "~a\n" obj)) (define w
> (Widget))
> > > (w.on_click (callback1 f)) """
> > >
> > > So, I'm trying to figure out how to do the same thing in
> Java:
> > map
> > > something in Java onto Func<object,object>, so something
> like
> > this will
> > > work in DynamicJava:
> > >
> > > """
> > > import static cli.Widgets.*;
> > >
> > > public int f(Object widget) {
> > > }
> > >
> > > Widget w = new Widget();
> > > w.on_click(f);
> > > """
> > >
> > > Does ikvm have something built-in to help pass a Java
> function to
> > C#? Or
> > > do you have a general strategy for doing this?
> > >
> > > Thanks for any further hints,
> > >
> > > -Doug
> > >
> > >
> > > On Sun, Feb 16, 2014 at 4:21 AM, Jeroen Frijters
> > <je...@su... <mailto:je...@su...>
> <mailto:je...@su... <mailto:je...@su...> >
> >
>
> > > <mailto:je...@su... <mailto:je...@su...>
> <mailto:je...@su... <mailto:je...@su...> > > > wrote:
> > >
> > >
> > > Hi Doug,
> > >
> > > I'm probably misunderstanding the question, but in
> Java
> > you'd use
> > > interfaces for this. See
> > >
> >
> http://docs.oracle.com/javase/7/docs/api/java/awt/Button.html#addActionL
> > > istener(java.awt.event.ActionListener) for example.
> > >
> > > Regards,
> > > Jeroen
> > >
> > >
> > > > -----Original Message-----
> > > > From: Doug Blank [mailto:dou...@gm...
> <mailto:dou...@gm...>
> > <mailto:dou...@gm... <mailto:dou...@gm...> >
> > > <mailto:dou...@gm...
> <mailto:dou...@gm...> <mailto:dou...@gm...
> <mailto:dou...@gm...> > > ]
> > > > Sent: Sunday, February 16, 2014 0:57
> >
> > > > To: ikv...@li...
> <mailto:ikv...@li...> <mailto:ikvm-
> <mailto:ikvm->
>
> > dev...@li...
> <mailto:dev...@li...> > <mailto:ikvm- <mailto:ikvm-
> > <mailto:ikvm- <mailto:ikvm-> >
> > > dev...@li...
> <mailto:dev...@li...>
> > <mailto:dev...@li...
> <mailto:dev...@li...> > >
>
> > > > Subject: [Ikvm-developers] How to pass a Java
> callable to
> > C#?
> > > >
> > > > I am doing some refining on the DynamicJava
> interpreter
> > written
> > > in Java
> > > > that is running under Mono [1]. (C# libraries are
> made
> > available
> > > to the
> > > > interpreted Java through ikvmstub, and the Java
> > interpreter is
> > > running
> > > > by being compiled with ikvmc). Everything is
> working very
> > well,
> > > but one
> > > > thing I would like to do is be able to pass a
> Java
> > callback to a
> > > C#
> > > > function.
> > > >
> > > >
> > > > The C# code looks like this (although I could
> change it
> > in any
> > > manner):
> > > >
> > > >
> > > > public class Widget {
> > > > public void on_click(System.Func<object,object>
> function)
> > {
> > > > }
> > > > }
> > > >
> > > > In general, how would a Java program register a
> callback
> > to the
> > > C#? I
> > > > can write an interface on either the Java side,
> or the C#
> > side.
> > > >
> > > > Thanks for any suggestions,
> > > >
> > > > -Doug
> > > >
> > > > [1] -
> > > >
> > >
> >
> https://bitbucket.org/ipre/calico/src/master/languages/Java/dynamicjava/
> > >
> > >
> >
> >
> >
>
>
>
|