支持多个版本,无需在 JavaME 中单独构建

发布于 2024-08-25 01:07:50 字数 504 浏览 5 评论 0原文

我希望能够支持多个版本的 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 技术交流群。

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

发布评论

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

评论(1

染年凉城似染瑾 2024-09-01 01:07:50

如果您只需要通过您知道可以访问的接口来访问类C,那么就很简单:

MyInterface provider=null;
try{
    Class myClass= Class.forName("sysPackage.C");
    provider = (MyInterface)(myClass.newInstance());  
}catch(Exception ex){
}
if(provide!=null){
    //Use provider
}

如果C没有可以访问的接口如果使用的话,我们可以创建一个包装类 S ,它将成为接口的成员。

class S implements MyInterface{
    static {
        try {
            Class.forName("sysPackage.C");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    } 
    public static void forceExceptionIfUnavailable() {}
    //TODO: Methods that use C. Class C will only be used within this class
}   

S 有一个静态块,因此如果 C 不可用,则在类解析期间会引发异常。加载类后,我们立即调用forceExceptionIfUnavailable来确保静态块立即运行。如果没有崩溃,那么我们可以使用S中的方法来间接使用C类。

或者,我们可以使用此处的方法:

基本上,您创建一个新包P,具有公共抽象类 A 和包私有的具体子类 SA 有一个静态方法 getS,如果在实例化过程中抛出异常,该方法将返回 S 的实例或 nullS 的每个实例都有一个 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:

MyInterface provider=null;
try{
    Class myClass= Class.forName("sysPackage.C");
    provider = (MyInterface)(myClass.newInstance());  
}catch(Exception ex){
}
if(provide!=null){
    //Use provider
}

If C does not have an interface that can be used, then we can instead create a wrapper class S that will be a member of the interface instead.

class S implements MyInterface{
    static {
        try {
            Class.forName("sysPackage.C");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    } 
    public static void forceExceptionIfUnavailable() {}
    //TODO: Methods that use C. Class C will only be used within this class
}   

S has a static block so that an exception is thrown during class resolution if C is unavailable. Immediately after loading the class, we call forceExceptionIfUnavailable to make sure that the static block is run immediately. If it doesn't crash, then we can use the methods in S to indirectly use class C.

Alternatively, we can use the method here:

Basically, you create a new package P, with a public abstract class A and a concrete subclass S private to the package. A has a static method getS that returns an instance of S or null if an exception is thrown during instantiation. Each instance of S has an instance of C so it will fail to instantiate when C is unavailable - otherwise it will succeed. This method seems to be a bit safer as S (and hence all the C APIs) are package private.

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