我有一个用Java编写的框架,该框架使用反射来获取批注中的字段并根据它们做出一些决策。在某些时候,我还可以创建注释的临时实例并自己设置字段。这部分看起来像这样:
public @interface ThirdPartyAnnotation{
String foo();
}
class MyApp{
ThirdPartyAnnotation getInstanceOfAnnotation(final String foo)
{
ThirdPartyAnnotation annotation = new ThirdPartyAnnotation()
{
@Override
public String foo()
{
return foo;
}
};
return annotation;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试在Kotlin中做确切的事情。请记住,注释位于第三方jar中。无论如何,这是我在Kotlin中尝试的方法:
class MyApp{
fun getAnnotationInstance(fooString:String):ThirdPartyAnnotation{
return ThirdPartyAnnotation(){
override fun foo=fooString
}
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨:注释类无法实例化
所以问题是:我应该如何在Kotlin中这样做?
kotlin ×1