필터 지우기
필터 지우기

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 CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by