フィルターのクリア

How do I access a Java inner class from Matlab?

3 ビュー (過去 30 日間)
Peter Li
Peter Li 2011 年 9 月 13 日
コメント済み: zhaogang han 2018 年 11 月 6 日
As described in this MathWorks support node, it is not straightforward to access inner Java classes from Matlab...

採用された回答

Peter Li
Peter Li 2011 年 9 月 13 日
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 件のコメント
zhaogang han
zhaogang han 2018 年 11 月 6 日
why Outerclass.class().getClassLoader() throws error in matlab? I can't instantiate the Outerclass, also there is no other class in the same package.

サインインしてコメントする。

その他の回答 (1 件)

Yair Altman
Yair Altman 2012 年 3 月 15 日
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 件のコメント
Peter Li
Peter Li 2012 年 3 月 15 日
But what if you cannot instantiate the outer class? That was the main difficulty that required me to find the proper classloader.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJava Package Integration についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by