简体   繁体   English

Kotlin反射与Java的互操作性

[英]Kotlin reflection interoperability with Java

What are the caveats that a developer should be aware of while writing reflective code that works both with Java and Kotlin? 在编写适用于Java和Kotlin的反射代码时,开发人员应该注意哪些警告?

For example, I have an existing library that uses reflection and it works well with Java. 例如,我有一个使用反射的现有库,它适用于Java。 However, when I use the same with Kotlin, my reflective code doesn't seem to pick up the annotations on fields. 但是,当我在Kotlin中使用相同的代码时,我的反射代码似乎没有在字段上获取注释。

Here are some of the differences that I noticed. 以下是我注意到的一些差异。

1. Acquiring a Class instance 1.获取Class实例

// Example 1.1 - Java
Class<?> userClass = User.class;         // From a class name
userClass = userInstance.getClass();     // OR from an instance

Getting a Java class instance in Kotlin 在Kotlin中获取Java类实例

// Example 1.2 - Kotlin
val userClass = userInstance.javaClass   // From an instance

I'm unable to use the .class facility or the .getClass() method in Kotlin as we do in Java. 我不能像在Java中那样在Kotlin中使用.class工具或.getClass()方法。

2. Delegates 2.代表们

When I use delegated properties in a Kotlin class, the properties that I retrieve have the $delegate suffix. 当我在Kotlin类中使用委托属性时,我检索的属性具有$delegate后缀。 This is a bit contrary to the fields that we get in Java (I do understand Kotlin does not have fields, only properties). 这与我们在Java中获得的字段有点相反(我知道Kotlin没有字段,只有属性)。 How does this affect meta-programming? 这对元编程有何影响?

However, with delegates I see that most of the methods retain their behavior as they do in Java. 但是,对于委托,我发现大多数方法都像Java一样保留了它们的行为。 Are there any other differences that I have to be aware of? 我还需要注意其他差异吗?

Making Java and Kotlin interoperable for me would require understanding about 1 discussed above, plus other limitations / differences that Kotlin brings to meta-programming. 让Java和Kotlin可以与我互操作需要理解上面讨论的1 ,以及Kotlin为元编程带来的其他限制/差异。

For example, I have an existing library that uses reflection and it works well with Java. 例如,我有一个使用反射的现有库,它适用于Java。 However, when I use the same with Kotlin, my reflective code doesn't seem to pick up the annotations on fields. 但是,当我在Kotlin中使用相同的代码时,我的反射代码似乎没有在字段上获取注释。

Can it be because the fields are private now? 可能因为这些字段现在是私有的吗?

Anyway, there are issues with annotations on fields at the moment, this will be fixed in on of the upcoming milestones. 无论如何,目前在字段上存在注释问题,这将在即将到来的里程碑中得到修复。

Some other relevant issues: 其他一些相关问题:

I'm unable to use the .class facility or the .getClass() method in Kotlin as we do in Java. 我不能像在Java中那样在Kotlin中使用.class工具或.getClass()方法。

Only the syntax is different: javaClass<C>() works exactly the same as C.class , and x.javaClass does the same thing as x.getClass() 只有语法不同: javaClass<C>()C.class ,而x.javaClassx.getClass()

When I use delegated properties in a Kotlin class, the properties that I retrieve have the $delegate suffix. 当我在Kotlin类中使用委托属性时,我检索的属性具有$ delegate后缀。

Minor correction: the fields have the $delegate suffix, not the properties. 次要更正: 字段具有$delegate后缀,而不是属性。

However, with delegates I see that most of the methods retain their behavior as they do in Java. 但是,对于委托,我发现大多数方法都像Java一样保留了它们的行为。 Are there any other differences that I have to be aware of? 我还需要注意其他差异吗?

The docs here give you a detailed description of how delegated properties are implemented. 这里的文档为您提供了如何实现委派属性的详细说明。

Making Java and Kotlin interoperable for me would require understanding about 1 discussed above, plus other limitations / differences that Kotlin brings to meta-programming. 让Java和Kotlin可以与我互操作需要理解上面讨论的1,以及Kotlin为元编程带来的其他限制/差异。

The more your Kotlin code resembles Java code, the smaller is the difference from the reflection point of view. 你的Kotlin代码越像Java代码,与反射观点的差异就越小。 If you write idiomatic Kotlin, eg use default parameter values, traits, properties, delegates, top-level functions, extensions etc, the classes you get differ from idiomatic Java, otherwise they are closely aligned. 如果您编写惯用的Kotlin,例如使用默认参数值,特征,属性,委托,顶级函数,扩展等,您获得的类与惯用Java不同,否则它们是紧密对齐的。

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

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