gpt4 book ai didi

clojure - 从 Clojure 访问在抽象基类上定义的公共(public)方法

转载 作者:行者123 更新时间:2023-12-04 00:10:16 24 4
gpt4 key购买 nike

我正在尝试使用 clojure 中的 DNSJava 库。我尝试以下操作:

dmarced.dns> (def results (.run (Lookup. "google.com" Type/TXT)))
#'dmarced.dns/results
dmarced.dns> (def r (get results 0))
#'dmarced.dns/r
dmarced.dns> r
#object[org.xbill.DNS.TXTRecord 0x687a3556 "google.com.\t\t3599\tIN\tTXT\t\"v=spf1 include:_spf.google.com ~all\""]
dmarced.dns> (class r)
org.xbill.DNS.TXTRecord
dmarced.dns> (instance? TXTRecord r)
true

太棒了!我从docs知道我应该能够使用 .getStrings 获取记录的内容。

dmarced.dns> (.getStrings r)
Reflection warning, *cider-repl dmarced*:150:13 - reference to field getStrings can't be resolved.
IllegalArgumentException Can't call public method of non-public class: public java.util.List org.xbill.DNS.TXTBase.getStrings() clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:88)

好吧,谷歌很快告诉我这可以通过类型提示来解决:

dmarced.dns> (.getStrings ^TXTRecord r)
Reflection warning, *cider-repl dmarced*:153:13 - call to method getStrings on org.xbill.DNS.TXTRecord can't be resolved (argument types: ).
IllegalArgumentException Can't call public method of non-public class: public java.util.List org.xbill.DNS.TXTBase.getStrings() clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:88)

嗯。在我进一步搜索谷歌,并阅读了 interop 上的整页,但我没有任何运气。

查看 TXTRecord 的来源我看到它扩展了 TXTBase这是一个抽象类,其中 getStrings实现。由于 TXTRecord 扩展了它,我应该可以通过它访问 getStrings(我什至编写了一个 Java 程序来验证它。

有人知道如何通过 Clojure 访问它吗?

编辑 工作 java 程序

import org.xbill.DNS.*;

class Main {
public static void main(String [] args) throws TextParseException {
Lookup l = new Lookup("google.com", Type.TXT);
Record [] rs = l.run();
for(int i = 0; i < rs.length; i++) {
TXTRecord tr = (TXTRecord)rs[i];
for(int j = 0; j < tr.getStrings().size(); j ++) {
System.out.println(tr.getStrings().get(j));
}
}

}
}

最佳答案

看起来这是一个已知的错误CLJ-1243 .您可以通过阅读 Netty issue description 找到更多详细信息和可能的根本原因。 :

The public methods inherited from AbstractBootstrap cannot be invoked via the Java reflection API due to long-standing bugs such as JDK-4283544.

This is a serious problem for alternative JVM languages which use reflection to discover Java methods. For example, Clojure uses reflection in its compiler, and it cannot invoke these methods at all, as reported in CLJ-1243.

部分描述来自JDK bug :

java.lang.reflect.Field (get* and set*) and Method (invoke) base their access check on the declaring class. This is contrary to the JLS, which defines accessibility in terms of the reference type.

关于clojure - 从 Clojure 访问在抽象基类上定义的公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com