Azzera filtri
Azzera filtri

How do I access a Java inner class from Matlab?

3 visualizzazioni (ultimi 30 giorni)
As described in this MathWorks support node, it is not straightforward to access inner Java classes from Matlab...

Risposta accettata

Peter Li
Peter Li il 13 Set 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 Commento
zhaogang han
zhaogang han il 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.

Accedi per commentare.

Più risposte (1)

Yair Altman
Yair Altman il 15 Mar 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 Commento
Peter Li
Peter Li il 15 Mar 2012
But what if you cannot instantiate the outer class? That was the main difficulty that required me to find the proper classloader.

Accedi per commentare.

Categorie

Scopri di più su Java Package Integration in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by