java 1.8的枚举类型,如何从Javascript(Java的1.8)访问Java枚举

In Java 1.7, prior to it's removal, one could use 'Packages' to access Java Enums in the following way from Javascript on an HTML page viewed a browser:

var enumvar1 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.YES

var enumvar2 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.NO

I'm upgrading these HTML pages to use Java 1.8 (which now uses the Nashorn javascript engine), and I cannot seem to figure out how to access the Enum members.

I've rewritten the Java applet to return a new MyClass object to a javascript variable, and I can access all the methods and fields in MyClass from the JavaScript variable, but I can't figure out the syntax to get at the Enums. The errors are of the type "property is null/undefined". I've tried various combinations of the package name, class name, variable holding the class, and applet ID variable.

I haven't found any examples for Java 1.8 in googling around, although in reading the Nashorn documentation from Oracle, it implies that Enums can be accessed.

Could someone provide an example syntax? Probably something simple that I am just overlooking...

Thanks!

解决方案

Here is an example how you can use it

var retPolicy = Java.type("java.lang.annotation.RetentionPolicy");

print(retPolicy.RUNTIME);

Also, if you're looking the ways to migrate to Nashorn from Rhino consider following manual https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值