[英]clojure gen-class with annotation that require enum parameter
I want to write a clojure lib and exposes generated classes, so that other java projects could use it.我想编写一个clojure lib并公开生成的类,以便其他java项目可以使用它。 I read and followed the gen-class doc, and everything works as my expect, except class annotation with enum parameters.我阅读并遵循了 gen-class 文档,除了带有枚举参数的类注释外,一切都按我的预期工作。
(ns common.exception.unauthorized
(:gen-class :name
^{org.springframework.web.bind.annotation.ResponseStatus
org.springframework.http.HttpStatus/UNAUTHORIZED} ; <- here
com.lalala.common.exception.Unauthorized
:extends java.lang.RuntimeException
:init init
:constructors {[String] [String]}
:main false
:prefix "unauthorized-"))
(defn unauthorized-init [^String message]
[[message] nil])
This exception class is generated without any error, and it also able be used as an Exception
.这个异常类的生成没有任何错误,它也可以用作Exception
。 However, this exception is intended to be used as a http response along with spring web.但是,此异常旨在与 Spring Web 一起用作 http 响应。 The spring framework read the annotation, and find that it is HttpStatus/UNAUTHORIZED
then response 401. But the spring framework throws exception complaining that java.lang.EnumConstantNotPresentException: org.springframework.http.HttpStatus
. spring 框架读取注解,发现是HttpStatus/UNAUTHORIZED
然后响应 401。但是 spring 框架抛出异常抱怨java.lang.EnumConstantNotPresentException: org.springframework.http.HttpStatus
。
And I had a look at the generated class, it's something like this:我查看了生成的类,它是这样的:
@ResponseStatus(HttpStatus.401)
public class Unauthorized extends RuntimeException {
private static final Var init__var = Var.internPrivate("common.exception.unauthorized", "unauthorized-init");
private static final Var getStackTrace__var = Var.internPrivate("common.exception.unauthorized", "unauthorized-getStackTrace");
// ...... ellipsis
}
As it shown, the HttpStatus/UNAUTHORIZED
is compiled into HttpStatus.401
which is invalid.如图所示, HttpStatus/UNAUTHORIZED
被编译成无效的HttpStatus.401
。
I also tried with {:code org.springframework.http.HttpStatus/UNAUTHORIZED}
, {:value org.springframework.http.HttpStatus/UNAUTHORIZED}
, it can be compiled into @ResponseStatus(code/value = HttpStatus.401)
, but the enum value itself still in invalid form HttpStatus.401
.我也试过{:code org.springframework.http.HttpStatus/UNAUTHORIZED}
, {:value org.springframework.http.HttpStatus/UNAUTHORIZED}
@ResponseStatus(code/value = HttpStatus.401)
{:value org.springframework.http.HttpStatus/UNAUTHORIZED}
,它可以编译成@ResponseStatus(code/value = HttpStatus.401)
,但是枚举值本身仍然是无效的HttpStatus.401
形式。
Am I use class annotation for gen-class in a wrong way?我是否以错误的方式为 gen-class 使用了类注释? or just Clojure compiler just have this bug?或者只是 Clojure 编译器有这个错误?
PS tried with Clojure 1.9, 1.10, 1.10.1 PS 尝试使用 Clojure 1.9、1.10、1.10.1
Finally, I use native Java code instead.最后,我改用原生 Java 代码。 I realized that writing classes in clojure with only ctor forwarded to super class is making trouble for myself.我意识到在 clojure 中编写类,只将 ctor 转发到超类是给自己带来麻烦。 I embedded java code in the project along with :java-source-paths
configured in defproject
, got my problem solved.我在项目中嵌入了 java 代码以及在defproject
配置的:java-source-paths
,解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.