在 MATLAB 中使用 Java 枚举或公共静态字段

发布于 2024-07-30 11:06:43 字数 584 浏览 3 评论 0原文

我想知道如何在 MATLAB 中获取对 Java enum 或静态公共字段的引用。 在 MATLAB 中,如果您尝试使用 Java 对象/方法,则有与 Java 对象创建/方法调用等等效的方法:

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:???

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: ?????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦一生花开无言 2024-08-06 11:06:43

内部类需要转换“.” 到Matlab中的'$'。

这实际上可能是由于 Java 编译器存储内部类对象的方式造成的。 对于内部类(例如 javax.swing.plaf.basic.BasicTextUI$UpdateHandler),其行为类似。 Matlab并没有像JVM那样智能自动将这些内部'$'转换成'.'。 因此,在这些情况下,我们不能在 Matlab 中使用常规的简单点符号,并且由于 '$' 在 Matlab 语法中是无效字符,因此我们必须在 javaObject, javaMethodawtinvoke 及其亲属。 例如:

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

枚举需要类似的“.”转换 到Matlab中的'$'。 但是 MATLAB 的 javaObject 函数调用类构造函数,并且由于枚举没有构造函数,因此我们收到以下错误:

Java 类中不存在具有适当签名的构造函数

幸运的是 enum 有内置方法 valueOf(),我们可以将其与 javaMethod 一起使用:

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

同样:

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

静态字段可以使用简单的方法直接在 Matlab 中获取点表示法:

redColor = java.awt.Color.red;

可以使用 Matlab 的内置 struct 函数获取静态字段的完整列表:

>> 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

如果默认构造函数是私有(隐藏)的,MATLAB 的函数 javaObject 可能无法工作,并且 javaMethod 可能也不起作用。 如果具有静态方法的类是嵌套的,那么您可能会运气不好。
对于文件交换上的系统托盘实用程序,我使用了反射方法,如本文中所述:http://UndocumentedMatlab.com/blog/setting-system-tray-popup-messages/

信用:由 马克·米科夫斯基

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

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 as smart as the JVM to automatically convert these internal '$' into '.'. Therefore, we can't use the regular simple dot-notation in these cases in Matlab, and since '$' is an invalid character in Matlab syntax, we must resort to using the '$' within javaObject, javaMethod, awtinvoke and their relatives. For example:

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

Enums require similar conversion of '.' to '$' in Matlab. But MATLAB's javaObject function calls the class constructor, and since enums have no constructor, we get the following error:

No constructor with appropriate signature exists in Java class

Luckily enum has the builtin method valueOf() 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 gotten directly in Matlab using simple dot notation:

redColor = java.awt.Color.red;

The full list of static fields can be gotten using Matlab's built-in 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

MATLAB's function javaObject may not work if the default constructor is private (hidden), and javaMethod probably won't work either. If the class with the static methods is nested you may be out of luck.
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/

Credit: edited by Mark Mikofski

一曲琵琶半遮面シ 2024-08-06 11:06:43

您可以使用 package.class.FIELD 语法从 Matlab 引用 Java 枚举常量,就像任何其他静态 Java 字段一样。 假设您有一个枚举。

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

您可以使用直接引用获取 Matlab 中的枚举常量。 (当然,Java 类必须位于 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

如果您想要在运行时确定“动态”引用,您可以构建一个包含等效静态引用的字符串并将其传递给 eval()。 这几乎适用于任何 Matlab 代码。

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

如果您想变得更有趣,您可以使用 Java 反射在运行时获取所有枚举常量。 制作一个薄包装来与其他自定义类放在一起,以解决 Matlab 类加载器的怪癖。 (没有 Matlab javaClass() 等效项;恕我直言,这是 Matlab 的疏忽。)

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

然后您可以枚举 Matlab 中的常量。

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

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();
夜无邪 2024-08-06 11:06:43

编辑:
这里听起来像是常规方式就可以了。 或者由于某种原因,枚举与其他具有静态的类不同?

可以调用带有参数的 Java 方法吗?

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

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")
还在原地等你 2024-08-06 11:06:43

我面临着与原始问题第一部分相同的枚举问题。 将此处帖子中的一些信息放在一起,我意识到使用 MATLAB 的 javaMethod 函数调用 valueOf() 是可行的:

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

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

至于第二个问题,关于 static变量,我已经能够使用 MATLAB 2009b 中的正常表示法访问公共静态变量,因此我无法提供太多帮助。 假设 MAX_FOO 被声明为 Foo 类中的 public static int,我可以这样做:

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

MATLAB: < code>n = com.example.test.Foo.MAX_FOO;

也许关于美元符号的其他答案之一以及对 fooVar.getClass().getFields() 的调用会减少一些光。

I was facing the same enum issue as the first part of the original question. Putting some information together from posts like those here, I realized that using MATLAB's 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');

As for the second question, about static variables, I have been able to access public static variables using the normal notation in MATLAB 2009b, so I can't be of much help there. Assuming MAX_FOO is declared as a public 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 along with calls to fooVar.getClass().getFields() will shed some light.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文