How do I access a Java inner class from Matlab?

9 Ansichten (letzte 30 Tage)
Peter Li
Peter Li am 13 Sep. 2011
Kommentiert: zhaogang han am 6 Nov. 2018
As described in this MathWorks support node, it is not straightforward to access inner Java classes from Matlab...

Akzeptierte Antwort

Peter Li
Peter Li am 13 Sep. 2011
However, contrary to the answer provided by support, this is possible without writing custom wrappers!
If you are trying to get an inner class that's an Enum class, because you need the Enum values, chances are you can get away with something like (credit Bob Gilmore):
NORMAL = javaMethod('valueOf', 'javax.swing.JTable$PrintMode', 'NORMAL')
For trickier situations, the best I've come up with so far is:
InnerClass = java.lang.Class.forName('package.OuterClass$InnerClass', true, classloader);
In order to get a classloader object that knows how to find your InnerClass, the best I've come up with so far is to start with a trivially constructable object from the same package, get that instance's class, then get the classloader from that:
trivial_instance = package.TrivialClass();
TrivialClass = trivial_instance.getClass();
package_class_loader = TrivialClass.getClassLoader();
Hopefully there's a better way out there...
Once you have the InnerClass object you can call newInstance() on it to make an instance. If InnerClass is an Enum, you can also use getFields() to get a list of the possible values.
  1 Kommentar
zhaogang han
zhaogang han am 6 Nov. 2018
why Outerclass.class().getClassLoader() throws error in matlab? I can't instantiate the Outerclass, also there is no other class in the same package.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Yair Altman
Yair Altman am 15 Mär. 2012
You don't need to create new instances, and in fact in some cases the class may not allow you to create a new instance. Here are alternative tricks from section 1.7 of my Matlab-Java book:
First create a tray-icon object (the outer class object):
myIcon = fullfile(matlabroot,'toolbox/matlab/icons/matlabicon.gif');
iconImage = java.awt.Toolkit.getDefaultToolkit.createImage(myIcon);
trayIcon = java.awt.TrayIcon(iconImage, 'initial tooltip');
Now access the TrayIcon.MessageType inner class:
>> trayIconClasses = trayIcon.getClass.getClasses;
>> trayIconClasses(1)
ans =
class java.awt.TrayIcon$MessageType <= hurray!!!
>> MessageTypes = trayIconClasses(1).getEnumConstants
MessageTypes =
java.awt.TrayIcon$MessageType[]:
[java.awt.TrayIcon$MessageType] <= 1: ERROR
[java.awt.TrayIcon$MessageType] <= 2: WARNING
[java.awt.TrayIcon$MessageType] <= 3: INFO
[java.awt.TrayIcon$MessageType] <= 4: NONE
We can also access Java enums using their built-in values() and valueOf() methods:
>> msgType=javaMethod('valueOf','java.awt.TrayIcon$MessageType','INFO')
msgType =
INFO <= a java.awt.TrayIcon$MessageType object
>> enums = cell(javaMethod('values','java.awt.TrayIcon$MessageType'));
>> msgType = enums{3}; % alternative way to find the INFO enum value
>> cellfun(@(c)c.toString.char, enums, 'uniform',false)'
ans =
'ERROR' 'WARNING' 'INFO' 'NONE'
More information about using system-tray icons in Matlab can be found in:
  1 Kommentar
Peter Li
Peter Li am 15 Mär. 2012
But what if you cannot instantiate the outer class? That was the main difficulty that required me to find the proper classloader.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by