Behavior of handle function on java objects (javahandle)

18 views (last 30 days)
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)

Umar
Umar on 5 Aug 2024

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
James
James on 8 Aug 2024
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.
Umar
Umar on 8 Aug 2024
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

Find more on Downloads in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!