Using Java Enumerations or Public Static Fields in MATLAB

I am wondering how in MATLAB you can get a link to Java enum or a static public field. In MATLAB, if you are trying to use Java objects / methods, there are equivalents to creating / calling a Java object / 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:

+4
source share
4 answers

You can reference the enum Java constants from Matlab using the syntax package.class.FIELD, as in any other static Java field. Say you have an enumeration.

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

You can get enum constants in Matlab using a direct link. (Of course, Java classes must be in Matlab javaclasspath.)

 % 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 need a β€œdynamic” link defined at runtime, you can simply build a string containing an equivalent static link and pass it to eval (). This works with 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 enumerated constants at runtime. Make a thin shell to fit with your other custom classes to get around the quirks with the Matlab class loader. (There is no equivalent to Matlab javaClass (), IMHO is Matlab supervision.)

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

Then you can list the constants in Matlab.

 % Constant enumeration using reflection klass = com.example.Reflector.forName('com.example.MyEnum'); enums = klass.getEnumConstants(); 
+6
source

Inner classes require a conversion of '.' to '$' in Matlab.

This may be due to the way the Java compiler stores the internal objects of the class. It behaves similarly for inner classes (e.g. javax.swing.plaf.basic.BasicTextUI$UpdateHandler ). Matlab is not as smart as the JVM to automatically convert these internal "$" to ".". Therefore, we cannot use the usual simple dot notation in these cases in Matlab, and since "$" is an invalid character in the Matlab syntax, we must resort to using "$" in javaObject , javaMethod , awtinvoke and their relatives. For instance:

 Java: InnerClass c = new com.example.test.SomeEnum.InnerClass; MATLAB: c = javaObject('com.example.test.SomeEnum$InnerClass') 

Enumerations require a similar conversion. to '$' in Matlab. But the MATLAB javaObject function calls the constructor of the class, and since the enumerations do not have a constructor, we get the following error:

Java class does not have a constructor with the corresponding signature

Fortunately, enum has a built-in valueOf() method that we can use with javaMethod :

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

Similarly:

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

Static fields can be obtained directly in Matlab using simple dot notation:

 redColor = java.awt.Color.red; 

A complete list of static fields can be obtained using the built-in Matlab struct function:

 >> staticFields = struct(java.awt.Color.red) staticFields = white: [1x1 java.awt.Color] WHITE: [1x1 java.awt.Color] lightGray: [1x1 java.awt.Color] LIGHT_GRAY: [1x1 java.awt.Color] gray: [1x1 java.awt.Color] GRAY: [1x1 java.awt.Color] darkGray: [1x1 java.awt.Color] DARK_GRAY: [1x1 java.awt.Color] black: [1x1 java.awt.Color] BLACK: [1x1 java.awt.Color] red: [1x1 java.awt.Color] RED: [1x1 java.awt.Color] pink: [1x1 java.awt.Color] PINK: [1x1 java.awt.Color] orange: [1x1 java.awt.Color] ORANGE: [1x1 java.awt.Color] yellow: [1x1 java.awt.Color] YELLOW: [1x1 java.awt.Color] green: [1x1 java.awt.Color] GREEN: [1x1 java.awt.Color] magenta: [1x1 java.awt.Color] MAGENTA: [1x1 java.awt.Color] cyan: [1x1 java.awt.Color] CYAN: [1x1 java.awt.Color] blue: [1x1 java.awt.Color] BLUE: [1x1 java.awt.Color] OPAQUE: 1 BITMASK: 2 TRANSLUCENT: 3 

The MATLAB javaObject function may not work if the default constructor is private (hidden) and javaMethod probably will not work either. If a class with static methods is nested, you might be out of luck. For my systray utility on File Exchange, I used the reflection approach as described in this post: http://UndocumentedMatlab.com/blog/setting-system-tray-popup-messages/

Credit: edited by Mark Mikofsky

+10
source

EDIT: From here, it seems like the usual way would just work. Or for some reason Enums are different from other classes in statics?

Is it possible to call a Java method with parameters?

 SomeEnum e = com.example.test.SomeEnum.valueOf(SomeEnum.class, "MY_FAVORITE_ENUM") 
+3
source

I came across the same enumeration question as the first part of the original question. Gathering some information from posts like these, I realized that using the MATLAB javaMethod function to call valueOf() works:

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

MATLAB: e = javaMethod('valueOf', 'com.example.test$SomeEnum', 'MY_FAVORITE_ENUM');

Regarding the second question regarding static variables, I was able to access public static variables using the usual notation in MATLAB 2009b, so I cannot help. Assuming MAX_FOO declared as an open static int in the Foo class, I can do:

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

MATLAB: n = com.example.test.Foo.MAX_FOO;

Perhaps one of the other answers about dollar signs, as well as a call to fooVar.getClass().getFields() shed some light.

+2
source

All Articles