简体   繁体   English

如何使用gen-class定义静态初始化程序

[英]How can a static initializer be defined using gen-class

I am generating a Java class from Clojure that implements a JDBC Driver, the problem is that usually JDBC drivers register themselves with the DriverManager in a static initializer like so: 我正在从Clojure生成一个实现JDBC驱动程序的Java类,问题是通常JDBC驱动程序在静态初始化程序中使用DriverManager注册自己,如下所示:

public class MyDriver implements java.sql.Driver {
    ...
    static { 
        ...
        try {
            DriverManager.registerDriver(new MyDriver());
        } catch (SQLException s) {
            throw (RuntimeException) new RuntimeException
                ("could not register MyDriver driver!").initCause(s);
        }
        ...
     }
}

What do I have to put in the gen-class declaration and which name should the function that implements it have? 我需要在gen-class声明中添加什么以及实现它的函数应该具有哪个名称?

Thanks. 谢谢。

I don't believe this is possible in pure gen-class at present. 我认为目前在纯粹gen-class中这是不可能的。 clojure's gen-class always generates a static initializer where it ties the generated class to the clojure runtime with a call to clojure的gen-class总是生成一个静态初始化器,它将生成的类与clojure运行时绑定,并调用

clojure.lang.Var#internPrivate(String, String)

There is some flexibility in the generation of that static initializer (see the :load-impl-ns option to gen-class ), but there isn't a way to add your own custom code to the initializer that I can see after reviewing genclass.clj. 生成静态初始化程序有一些灵活性(参见:load-impl-ns gen-class :load-impl-ns选项),但是没有办法将自己的自定义代码添加到初始化程序中,我可以在查看genclass后看到.clj。

btw - as a slight aside, in the JVM the name of the static initializer 'method' is <clinit> 顺便说一下 - 在JVM中,静态初始化程序'方法'的名称是<clinit>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM