Behavior of handle function on java objects (javahandle)

When the function handle is used on a java object, it returns a new "javahandle" object. For example:
out = handle(javax.swing.JFrame);
disp(class(out))
% The class of out is javahandle.javax.swing.JFrame
Interestingly, the properties and methods of this class appear identical, with the only difference being the addition of a new method called "java", which returns the original java object.
Even more interesting is that we can pass these javahandles directly to java. For example:
Frame = javax.swing.JFrame;
Panel = handle(javax.swing.JPanel);
Frame.add(Panel);
% The method add for the object Frame recognized the signature of Panel
Presumably, the object Panel knows to call its java method before being sent to Java.
What I want is to be able to programatically extend java classes at run time. I know I could make individual wrapper classes for each java class. This approach importantly relies on having a wrapper for the class calling the method, Frame in the example above, to convert the argument to the wrapped input class. Instead, it would be convenient to only need to make a wrapper for the input class. If the input is used as an index, we can easily overload subsindex in the class defintion to accomplish this, but this does not work for method calls of the form obj.theMethod(in).
The handle wrapper proves that this is possible, but the actual mechanisms are obfuscated, and again we cannot extend it directly as far as I am aware. Is there any way to alter inputs from within the input class like is done with javahandles using the matlab class definition?
I know that javahandle classes are actually UDD classes. I would perfectly accept this approach but there is very little documentation.

Answers (1)

Hi @James,

To address your query regarding, “Is there any way to alter inputs from within the input class like is done with javahandles using the matlab class definition? “

Handle classes in Matlab allow you to modify object properties directly, similar to how javahandles work. Please see example of

Derive Class from handle

So, after observing this example, you can alter inputs within the input class similar to how it is done with javahandles, providing flexibility and ease of modification within your class definitions.

4 Comments

This unfortunately does not answer my question.
I am concerned with implictly converting the argument of a method call by only changing the defintion of the arguments class. Of course I can manually call methods to perform this, but that is specifically what I want to avoid. This can be done with handle or value classes.
It looks like this is supported with the matlab class defintion as long as the class making the method call uses the argument validation defintion. The class being used as an argument will get a call to perform the conversion if the method to perform that conversion is available.
Another example of a similar behavior is what I described in my question about overloading the subsindex function. If the class instance is being used as an array index, it will call that method and the argument will be replaced.
My problem is that this implicit conversion does not seem to be available when passing to java directly.
Interestingly, I did learn that if you subclass "JavaVisible", there is a method "java" that creates a java object to serve as an adapter for the class. I assume this is the actual layer that Matlab uses when calling any method on a java object.

Hi @James,

I agree 100% about what you mentioned. Implicit conversion of arguments in method calls by only modifying the definition of the argument's class can indeed be achieved in MATLAB using handle or value classes. This capability is supported within MATLAB's class definitions when the class making the method call utilizes the argument validation definition. The key aspect here is that if a method for performing the conversion is available within the class being used as an argument, it will be called automatically during the method invocation, enabling seamless implicit conversion. To illustrate this behavior further, consider the analogy with overloading the subsindex function. When a class instance is used as an array index, MATLAB automatically calls the subsindex method to replace the argument. This showcases how MATLAB's object-oriented framework handles implicit conversions efficiently within method calls. By utilizing the provided tables, classes, and methods from the Java Engine API summary, you can effectively manage data conversions between Java and MATLAB, ensuring a smooth exchange of information without encountering implicit conversion issues. By observing the example shown in attached mathworks link, you can effectively manage data conversions between Java and MATLAB using the Java Engine API summary provided. This approach will ensure a seamless exchange of information without encountering implicit conversion issues.

https://www.mathworks.com/matlabcentral/answers/373014-how-can-i-get-com-mathworks-engine-package

Additionally, understanding the specialized MATLAB types supported by the com.mathworks.matlab.types package can further enhance your ability to handle complex data structures during communication.

Regarding your observation about subclassing "JavaVisible" and using the "java" method to create a Java object adapter, this indeed reflects one of the mechanisms through which MATLAB interfaces with Java objects. The "java" method essentially creates a bridge between MATLAB and Java, allowing for seamless interaction between the two environments. An example demonstrating this integration could involve creating a Java object adapter for a MATLAB class and showcasing how methods can be invoked on the Java object. Hope this helps. Please let me know if you have any further questions.

This unfortunately does not help me with the original problem of use with Java objects.
While mathworks may not openly support this, there clearly is some layer abstracted from the user that allows for this conversion at least for the built in javahandle objects.
Hi @James,
While the specifics of implicit conversion for Java objects in MATLAB may not be openly disclosed by MathWorks, the existence of abstraction layers and predefined behaviors within the framework based on my suggestion makes sense about MATLAB inherently supporting implicit conversions for certain object types like javahandle objects. I would suggest to have you exploring further within MATLAB's documentation or experimenting with built-in objects, so you get a chance to uncover additional insights into how these implicit conversions are facilitated.

Sign in to comment.

Categories

Asked:

on 5 Aug 2024

Commented:

on 8 Aug 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!