繁体   English   中英

带有需要枚举参数的注释的clojure gen-class

[英]clojure gen-class with annotation that require enum parameter

我想编写一个clojure lib并公开生成的类,以便其他java项目可以使用它。 我阅读并遵循了 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])

这个异常类的生成没有任何错误,它也可以用作Exception 但是,此异常旨在与 Spring Web 一起用作 http 响应。 spring 框架读取注解,发现是HttpStatus/UNAUTHORIZED然后响应 401。但是 spring 框架抛出异常抱怨java.lang.EnumConstantNotPresentException: org.springframework.http.HttpStatus

我查看了生成的类,它是这样的:

@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
}

如图所示, HttpStatus/UNAUTHORIZED被编译成无效的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形式。

我是否以错误的方式为 gen-class 使用了类注释? 或者只是 Clojure 编译器有这个错误?

PS 尝试使用 Clojure 1.9、1.10、1.10.1

最后,我改用原生 Java 代码。 我意识到在 clojure 中编写类,只将 ctor 转发到超类是给自己带来麻烦。 我在项目中嵌入了 java 代码以及在defproject配置的:java-source-paths ,解决了我的问题。

暂无
暂无

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

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