tags:

views:

730

answers:

4

I'm wondering how in MATLAB you can get a reference to a Java enum or static public field. In MATLAB, if you are trying to use Java objects/methods, there are equivalents to Java object creation / method call / etc.:

Java: new com.example.test.Foo();

MATLAB: javaObject('com.example.test.Foo');

Java: com.example.test.Foo.staticMethod();

MATLAB: javaMethod('staticMethod', 'com.example.test.Foo');

Java: SomeEnum e = com.example.test.SomeEnum.MY_FAVORITE_ENUM;

MATLAB: ?????

Java: int n = com.example.test.Foo.MAX_FOO;

MATLAB: ?????

+3  A: 

EDIT: From here it sounds like the regular way would just work. Or are Enums different than other classes with statics for some reason?

Can you call a Java method with parameters?

SomeEnum e = com.example.test.SomeEnum.valueOf(SomeEnum.class, "MY_FAVORITE_ENUM")
Instantsoup
oh, that would work. I guess that's a workaround for enums.
Jason S
Wait: no, that wouldn't work. I still have to get a reference to SomeEnum.class which is a public static field. ARGH!
Jason S
w/r/t your updated link to "Accessing Private and Public Fields" -- I guess that does work for fields "directly" accessed in MATLAB, e.g. com.example.test.Myfield.FIELD_VAL, I was hoping I could get it based on a dynamic string e.g. fieldval = javaField('com.example.test.Myfield.FIELD_VAL') which doesn't seem like it's possible.
Jason S
+5  A: 

You can reference Java enum constants from Matlab using the package.class.FIELD syntax, as with any other static Java field. Let's say you have an enum.

package com.example;
public enum MyEnum {
    FOO, BAR, BAZ
}

You can get at the enum constants in Matlab using a direct reference. (The Java classes must be in Matlab's javaclasspath, of course.)

% Static reference
foo = com.example.MyEnum.FOO

% Import it if you want to omit the package name
import com.example.MyEnum;
foo = MyEnum.FOO
bar = MyEnum.BAR

If you want a "dynamic" reference determined at runtime, you can just build a string containing the equivalent static reference and pass it to eval(). This works on almost any Matlab code.

% Dynamic reference
foo = eval('com.example.MyEnum.FOO')

And if you want to get really fancy, you can use Java reflection to get at all the enumerated constants at run time. Make a thin wrapper to put with your other custom classes to get around quirks with Matlab's classloader. (There's no Matlab javaClass() equivalent; IMHO this is a Matlab oversight.)

//In Java
package com.example;
public class Reflector {
    public static Class forName(String className) throws Exception {
        return Class.forName(className);
    }
}

Then you can enumerate the constants in Matlab.

% Constant enumeration using reflection
klass = com.example.Reflector.forName('com.example.MyEnum');
enums = klass.getEnumConstants();
Andrew Janke
very well-stated, thanks!
Jason S
A: 

Hey guys,

The complete solution of andrew seems to work only if the enum is outside of a class, if it's inside a class, it does not seem to work

please look at my complete post here

http://groups.google.com/group/comp.soft-sys.matlab/browse%5Fthread/thread/4cf82b27fc943b54#

Sylvain
have you tried accessing Downloader$DownloadStatus ? (there's some slight ambiguity in naming between "." and "$" for inner static classes) In any case it's an interesting enough question that it's probably worth posting a separate question here. good luck!
Jason S
+3  A: 

Some cases require conversion of '.' to '$' in Matlab. For example:

Java: SomeEnum e = com.example.test.SomeEnum.MY_FAVORITE_ENUM;
MATLAB: javaObject('com.example.test.SomeEnum$MY_FAVORITE_ENUM')

Java: int n = com.example.test.Foo.MAX_FOO;
MATLAB: javaMethod('com.example.test.Foo$MAX_FOO')

This may actually due to the way the Java compiler stores internal class objects. It behaves similarly for internal classes (e.g. javax.swing.plaf.basic.BasicTextUI$UpdateHandler). Matlab is not smart enough to automatically convert these internal '$' into '.' (as the JVM is), so we can't use the regular simple dot-notation in these cases in Matlab, and must resort to using the '$' within javaMethod, javaObject, awtinvoke and their relatives.

For my systray utility on the File Exchange, I used the reflection approach, as described in this post: http://UndocumentedMatlab.com/blog/setting-system-tray-popup-messages/

Yair Altman
thanks, welcome to stackoverflow!
Jason S