如何在Clojure中实现具体类型参数的接口?

时间:2017-04-30 18:08:28

标签: java amazon-web-services clojure

我正在尝试使用来自Clojure的AWS Lambda。这是有效的Java代码:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Dbmgmt implements RequestHandler<Object, Object> {

    @Override
    public String handleRequest(Object in, Context ctx) {
        System.out.println("ok");
        ctx.getLogger().log("ok");
        return "ok";
    }
}

这是Clojure代码不起作用。

(defn -handleRequest [this in ctx]
  (reify RequestHandler
    (handleRequest [this in ctx] "ok")))

在两个类上使用Javap:

爪哇:

Compiled from "Dbmgmt.java"
public class com.streambright.Dbmgmt implements com.amazonaws.services.lambda.runtime.RequestHandler<java.lang.Object, java.lang.Object> {
  public com.streambright.Dbmgmt();
  public java.lang.String handleRequest(java.lang.Object, com.amazonaws.services.lambda.runtime.Context);
  public java.lang.Object handleRequest(java.lang.Object, com.amazonaws.services.lambda.runtime.Context);
}

Clojure的:

public class dbmgmt implements com.amazonaws.services.lambda.runtime.RequestHandler {
  public static {};
  public dbmgmt();
  public boolean equals(java.lang.Object);
  public java.lang.String toString();
  public int hashCode();
  public java.lang.Object clone();
  public java.lang.Object handleRequest(java.lang.Object, com.amazonaws.services.lambda.runtime.Context);
  public static void main(java.lang.String[]);
}

我想知道实现具体返回类型接口的最佳方法是什么。使用当前代码AWS抱怨:

"errorMessage": "Class dbmgmt does not implement RequestHandler with concrete type parameters"

2 个答案:

答案 0 :(得分:3)

您似乎没有使用返回类handleRequest实现String方法,而只使用Object。您可以将其作为&#34;类型提示&#34;方法名称:

(reify RequestHandler
  (^String handleRequest [this ^Object in ^Context ctx] "ok")))

EDIT :还原编辑,但添加完整的类型提示,这似乎是必需的。)

答案 1 :(得分:0)

我能解决的最接近的问题如下:

(ns dbmgmt
  (:gen-class
    :implements [com.amazonaws.services.lambda.runtime.RequestHandler]
    :methods [[handleRequest [Object com.amazonaws.services.lambda.runtime.Context] String]]))

这将生成以下类:

public class dbmgmt implements com.amazonaws.services.lambda.runtime.RequestHandler {
  public static {};
  public dbmgmt();
  public boolean equals(java.lang.Object);
  public java.lang.String toString();
  public int hashCode();
  public java.lang.Object clone();
  public java.lang.Object handleRequest(java.lang.Object, com.amazonaws.services.lambda.runtime.Context);
  public java.lang.String handleRequest(java.lang.Object, com.amazonaws.services.lambda.runtime.Context);
  public static void main(java.lang.String[]);
}

但是它仍然没有Java版本具有的RequestHandler。我认为现在不可能在Clojure中实现这一点。