我正在编写CRaSH插件,它在 Java 中看起来像
public class AuthPlugin extends CRaSHPlugin<AuthenticationPlugin> implements AuthenticationPlugin<String> {
@Override
public String getName() {
return "auth";
}
@Override
public Class<String> getCredentialType() {
return String.class;
}
@Override
public boolean authenticate(String username, String credential) throws Exception {
System.out.println("authenticate " + username + " : " + credential);
return false;
}
@Override
public void init() {
System.out.println("init");
}
@Override
public AuthenticationPlugin getImplementation() {
return this;
}
}
但是,我想用 Kotlin 来表达这个类。问题是AuthenticationPlugin
哪个具有通用参数。省略 is 会导致 Kotlin 编译失败,所以我使用了星形投影,比如
class AuthPlugin : CRaSHPlugin<AuthenticationPlugin<*>>(), AuthenticationPlugin<String> {
override fun getImplementation(): AuthenticationPlugin<String> = this
override fun getName(): String = "auth"
override fun authenticate(username: String?, credential: String?): Boolean {
println("authenticate ${username}, ${credential}")
return true
}
override fun getCredentialType(): Class<String> = String::class.java
}
编译得很好,但不幸的是,CRaSH 似乎使用它自己的加载插件的方式,我得到了运行时异常
Caused by: java.lang.UnsupportedOperationException: Type resolution of org.crsh.auth.AuthenticationPlugin<?> not yet implemented
at org.crsh.util.Utils.resolveToClass(Utils.java:576) ~[crash.shell-85c8d21173b71a0275b599a3a4444723dd64c6af.jar:?]
at org.crsh.util.Utils.resolveToClass(Utils.java:560) ~[crash.shell-85c8d21173b71a0275b599a3a4444723dd64c6af.jar:?]
at org.crsh.plugin.CRaSHPlugin.<init>(CRaSHPlugin.java:59) ~[crash.shell-85c8d21173b71a0275b599a3a4444723dd64c6af.jar:?]
实际上,Java 版本的输入resolveToClass()
是“Class@interface org.crsh.auth.AuthenticationPlugin”,而 Kotlin 的输入是“ParametrizedTypeImpl@org.crsh.auth.AuthenticationPlugin”。我认为 CRaSH 加载程序无法正确处理此问题。我当然可以只使用 Java,但为了坚持使用 Kotlin - 我怎样才能使这个类编译为与 Java 版本完全相同的类型并使其由 CRaSH 正确加载?