I want to do the equivalent of serializing a "function pointer" in Java and I am trying to find the most efficient method to do so. What I want to do is to take a "static method pointer" and then assign it to a field of a class instance that gets serialized (let say this class is called SerClass). I want the "static method pointer" itself to get serialized, but I don't want the class where that static method is defined to get instantiated or an instance of it to get serialized through the serialization mechanism of SerClass. Basically what I want is to be able to call the pointed static method after an instance of SerClass gets unserialized. Of course the "pointer" value has to be dynamic.

So let say that the signature of the method I want to pass is double f(double x). What I had thought to do is to declare an interface like this:

as a way to transmit the "pointer". So this way the "pointer" to MyClass.f would get serialized without having to instantiate MyClass or serialize an instance of it. I could define FPtrC as a static sub-class of MyClass and instantiate it when passing the "pointer" to SerClass. I could thus define a subclass FPtrC for every class where I want the pointer to potentially point. Is it the most efficient way of performing what I am trying to do (from a serialization point of view, but also to have an efficient way to call MyClass.f without having to use reflection)?

Thanks!

08-10-2011, 11:14 PM

Norm

Quote:

What I want to do is to pass a "pointer" to a static method

By pointer I assume you mean a reference to an object.
There should be no problem with that.

<CLASSNAME>.staticMethod(new Object()); // pass a pointer to an object to a static method in CLASSNAME

Not sure what you are trying to do or why.

08-10-2011, 11:19 PM

kreyszig

Quote:

Originally Posted by Norm

By pointer I assume you mean a reference to an object.
There should be no problem with that.

<CLASSNAME>.staticMethod(new Object()); // pass a pointer to an object to a static method in CLASSNAME

Not sure what you are trying to do or why.

Sorry, I don't want to pass a pointer to a static method. I want to pass a "static method pointer" to another method. (I changed the quoted sentence in my original post)

08-10-2011, 11:23 PM

Norm

Is this it: You want to have a reference(pointer) to a class that has a static method so you can pass that reference to another method without creating a new instance of that class. Sounds like a job for reflection.

08-11-2011, 12:44 AM

kreyszig

Quote:

Originally Posted by Norm

Is this it: You want to have a reference(pointer) to a class that has a static method so you can pass that reference to another method without creating a new instance of that class. Sounds like a job for reflection.

Yes I think this is what you describe, and in addition I want the pointer to be serializable (without instantiating/serializing the class where the static method is defined). Sorry I was having a hard time to explain it. I edited me original post again to explain it more in details. I want to avoid reflection as much as possible since it is probably not the most efficient option. The pointed static methods will get called millions of times...

08-11-2011, 08:52 AM

JosAH

Java doesn't have method pointers; C# has; aren't you mixing up those two languages?

kind regards,

Jos

08-11-2011, 09:29 AM

Tolls

In your OP example MyClass won't get serilialsed anyway. Objects get serilised, not Classes.
Indeed, it's the data in the objects that gets serilialised.
The receiving system of that serialised data needs to have access to the same version of that Class in order to reconstitute the object.
So your stuff in the OP is doing nothing at all, since there is no (useful) data in that FPtrC class.

You've also got your extends and implements back to front.

08-11-2011, 02:01 PM

kreyszig

Quote:

Originally Posted by Tolls

In your OP example MyClass won't get serilialsed anyway. Objects get serilised, not Classes.
Indeed, it's the data in the objects that gets serilialised.
The receiving system of that serialised data needs to have access to the same version of that Class in order to reconstitute the object.
So your stuff in the OP is doing nothing at all, since there is no (useful) data in that FPtrC class.

You've also got your extends and implements back to front.

I know that instances get serialised (I edited my original post to make it more exact, although harder to read) and that the receiving system must have access to the class... But this is not true to say that my example is not doing anything, because the SerClass instance holds a pointer to a FPtrC instance. When the pointer of the SerClass instance gets serialised, the information regarding the fact that it points to an instance of MyClass.FPtrC, instead of let say, an instance of MyOtherClass.FPtrC, will get serialized as well, right?

So you would set myFunction to some concrete implementation, each of which would do something different (in the above, run MyClass.f() in one, and MyClass.g() in the other)?

Yes, that would work.

Not sure what you achieve via this, but it would work.

08-11-2011, 03:56 PM

Norm

Still not sure what you are trying to do. Your questions are more about how to implement something a specific way rather than looking for a solution to a problem. Perhaps there are other solutions.
In java all methods belong to a class.
Question about C## Can you code a function without a class?
In java pointers are references to instances of an object created by the new statement or other methods.
The serialization process saves the contents of variables in a class. The way you are describing what you want to do, there is no data to be saved. Then you should not need to serialize the class.
The class file will contain the byte code for the method you want to call.
It seems what you are looking for is provided by the use of an interface. That will allow you to have many objects of the same type each of which can have a single method that does its thing.

08-11-2011, 04:06 PM

kreyszig

Quote:

Originally Posted by Tolls

So, just to clarify:
...
So you would set myFunction to some concrete implementation, each of which would do something different (in the above, run MyClass.f() in one, and MyClass.g() in the other)?

Yes, that would work.

Not sure what you achieve via this, but it would work.

Yep, you got it pretty right. SerClass instances are used to pass messages in a parallel computing environment. They transmit a message between a class that represents a model and another class that needs to retrieve information from that model. Since the message can be transmitted across the network, it needs to be serialisable and what I am trying to do is some kind of serialisable callback mechanism. So what I have is:

Not sure, but I've got db locking issues so can't really spend the time on it.
Now that it's spelled out (and what it's for) someone else might have a better solution for this sort of thing in a Java environment, but if it works for you and makes sense in your head then maybe that's not important.

08-11-2011, 04:58 PM

kreyszig

Quote:

Originally Posted by Norm

Still not sure what you are trying to do. Your questions are more about how to implement something a specific way rather than looking for a solution to a problem. Perhaps there are other solutions.

I am constrained by the fact that I have to use a single message passing class (such as SerClass).

Quote:

In java all methods belong to a class.
Question about C## Can you code a function without a class?

Sorry I don't know C##...

Quote:

In java pointers are references to instances of an object created by the new statement or other methods.

I know that

Quote:

The serialization process saves the contents of variables in a class. The way you are describing what you want to do, there is no data to be saved. Then you should not need to serialize the class.

I don't think the above statement is right. The serialization mechanism also saves identifiers for the classes whose instances are getting saved. So even if the classes *.FPtrC do not contain any fields, tags that identify them get saved.

Quote:

The class file will contain the byte code for the method you want to call.
It seems what you are looking for is provided by the use of an interface. That will allow you to have many objects of the same type each of which can have a single method that does its thing.

08-11-2011, 05:05 PM

Norm

Quote:

The serialization mechanism also saves identifiers for the classes whose instances are getting saved

Of course, the serialized data must identify what instance of what class was saved.

Quote:

I have to use a single message passing class (such as SerClass).

What does that mean wrto the reference to a method to be called?
The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?

08-11-2011, 05:06 PM

Norm

Quote:

The serialization mechanism also saves identifiers for the classes whose instances are getting saved

Of course, the serialized data must identify what instance of what class was saved.

Quote:

I have to use a single message passing class (such as SerClass).

What does that mean wrto the reference to a method to be called?
The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?

08-11-2011, 05:14 PM

kreyszig

Quote:

Originally Posted by Norm

Of course, the serialized data must identify what instance of what class was saved.

What does that mean wrto the reference to a method to be called?
The current instance of the SerClass has a variable that is a reference to a class with the method you want to use.
The question is: When the SerClass is serialized will the class referred to by the variable be serialized also?

When SerClass gets serialised, the instance pointed by SerClass.myPointer will also get serialised (it is serialisable). The class for that instance does not have any field, but nonetheless the serialisation mechanism will take care of identifying the specific FPtrC class which is referenced by SerClass.myPointer

08-11-2011, 05:17 PM

Norm

Then is that the answer you want? Will that do it?

08-11-2011, 05:25 PM

Tolls

Essentially it's a mechanism for telling the receiving system to "run this method on this class".

You could, of course, subclass SerClass to include this method, but I'm not sure that wouldn't actually be more clunky.

08-11-2011, 05:32 PM

Norm

Time to write a simple test program with several classes and see what happens.

08-11-2011, 05:34 PM

kreyszig

Quote:

Originally Posted by Tolls

Essentially it's a mechanism for telling the receiving system to "run this method on this class".

You could, of course, subclass SerClass to include this method, but I'm not sure that wouldn't actually be more clunky.

Yes I could potentially subclass SerClass instead... I can't think of any better method of doing what I want...