支持多个版本,无需在 JavaME 中单独构建
我希望能够支持多个版本的 Java ME,而无需进行多个构建。我已经知道 如何检测配置文件/配置/支持的 JSR< /a>.我的问题是,知道运行时是否支持 JSR 并不能让我使用所有功能,因为 Java-ME 不提供反射支持。
因为如果我在代码中的任何位置调用在更高版本中添加的函数 - 即使是永远不会运行的位置,那么这可能会在某些 JVM 上的解析过程中导致错误。有什么办法解决这个问题吗?
相关问题
I want to be able to support multiple versions of Java ME without having to have multiple builds. I already know how to detect the profile/configuration/supported JSRs. My problem is that knowing whether the JSR is supported at run time doesn't allow me to use all the features as Java-ME does not provide support for reflection.
For if I call a function added in a later version anywhere in the code - even a location that will never be run, then this could cause an error during resolution on some JVMs. Is there any way round this?
Related Questions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要通过您知道可以访问的接口来访问类
C
,那么就很简单:如果
C
没有可以访问的接口如果使用的话,我们可以创建一个包装类S
,它将成为接口的成员。S
有一个静态块,因此如果C
不可用,则在类解析期间会引发异常。加载类后,我们立即调用forceExceptionIfUnavailable来确保静态块立即运行。如果没有崩溃,那么我们可以使用S
中的方法来间接使用C
类。或者,我们可以使用此处的方法:
基本上,您创建一个新包
P
,具有公共抽象类A
和包私有的具体子类S
。A
有一个静态方法getS
,如果在实例化过程中抛出异常,该方法将返回S
的实例或null
。S
的每个实例都有一个C
实例,因此当C
不可用时它将无法实例化 - 否则它将成功。此方法似乎更安全一些,因为S
(因此所有C
API)都是包私有的。If you only need to access the class
C
through an interface which you know you will have access to, then it is simple enough:If
C
does not have an interface that can be used, then we can instead create a wrapper classS
that will be a member of the interface instead.S
has a static block so that an exception is thrown during class resolution ifC
is unavailable. Immediately after loading the class, we callforceExceptionIfUnavailable
to make sure that the static block is run immediately. If it doesn't crash, then we can use the methods inS
to indirectly use classC
.Alternatively, we can use the method here:
Basically, you create a new package
P
, with a public abstract classA
and a concrete subclassS
private to the package.A
has a static methodgetS
that returns an instance ofS
ornull
if an exception is thrown during instantiation. Each instance ofS
has an instance ofC
so it will fail to instantiate whenC
is unavailable - otherwise it will succeed. This method seems to be a bit safer asS
(and hence all theC
APIs) are package private.