Sign in to follow this  

[java] java callback design question

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello java wizards ;) I have a design question... As far as I know, hava does not have something like Function type, or equivalent of C++ pointer to (member) functions, which is kind of... ehmm pity. Well, what I need to do, is to have a java client receive some numbers through socket, let's say something like {2, 15, 4, ...}, where 2 indicate a class type (or interface), 15 means id of some conctrete object of that type, and 4 means some member function of that type or interface, ... are prameters to that member function. So that I can invoke a member function for some instance of class remotely, and the fastest way possible. Using Linj (Lisp In Java) I can have Linj translate this : (funcall (lambda (x y) (set-location x y)) 10 20) into something like this : new Procedure2() { public void funcall(Object x, Object y) { setLocation(((Number)x).doubleValue(), ((Number)y).doubleValue());}}. funcall(Bignum.valueOf(10), Bignum.valueOf(20)); which looks quite arcane, even without retyping to some specific class. Is there a better way to do it? How can I avoid hardwiring of retyping to specific class or interface in source? Thanks for some ideas !! skoco

Share this post


Link to post
Share on other sites
Quote:
Original post by skoco
As far as I know, hava does not have something like Function type, or equivalent of C++ pointer to (member) functions

But Java has java.lang.reflect.Method:
Quote:
From JavaDoc
A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).
A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

Maybe it is what you are searching for. However, you have to map the function number to a method name...

Share this post


Link to post
Share on other sites
Method 1. Remote method invocation.
Method 2. Switch statement.



BTW let's imagine, a program knows a location with exectutable code ... program would store it somewhere ... then the executable code would be relocated.

Share this post


Link to post
Share on other sites

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this