“This is the seventh day of my participation in the First Challenge 2022.

preface

In fact, about the reflection of this content in our peacetime at least as Android development or use quite little, but when reading the source code or development of some SDK encountered a lot, especially when seeing what KProperty type similar parameters, always unfamiliar, so I am ready to comb this part of the knowledge point.

There are Java reflections and Kotlin reflections, both of which are pretty much the same, but with different apis, which we’ll cover later.

The body of the

Since we are going to make a comprehensive review, let’s start with the concept of reflection and start slowly.

What is the reflection

There are a lot of articles on the web that tell you how to use it straight up, but you don’t know why. Let’s start with the concept of reflection: a way to dynamically access object properties and methods at runtime, without having to determine what those properties are beforehand.

What is run-time

If you’re a developer familiar with Java, you know that when you write.Java code, it gets compiled into a.class file, and it gets loaded by the JVM, and then your code is at runtime, so if you define an object like People, People, at runtime you can get some information about the People class.

The Class that holds this information is the familiar Class Class, which we’ll discuss later.

Why the need

If I define a class called People, I already know its properties at compile time so why reflect them

Here for an example of A very common, that is, the serialization library, if you have developed A serialization library XXJSON, then when you develop you don’t know what others want to convert json to type, so only at run time to get the others want to change the type of A, then what are the properties of type A, can transform, then you can use reflection to implementation.

The idea behind this serialization library is found in many Java frameworks, the idea of flexible configuration, where you just pass in a particular type or define a particular type in XML and the library handles it at run time.

What information can be picked up by reflection

Now that we know what reflection is, what information can be obtained from reflection about a class, including type information, superclass information, methods, properties, including method modifiers, parameters, etc., but it is very powerful, we will talk about it later.

Kotlin and Java reflection

As those of you familiar with Kotlin know that Kotlin is also compiled into bytecode and runs in the JVM, did Kotlin optimize or develop reflective content

The answer is yes, when you use Kotlin, you can use The Java reflection and API, but Kotlin has added libraries to add apis that fully incorporate the functionality of the Java reflection API, but also have some unique Kotlin features. Nullable types, whether they are data classes.

Not only that, but Kotlin’s reflection API is much better for people to use, which IS something I personally think we’ll talk about later when we break down its approach.

If you want to use Kotlin’s reflection library, Kotlin does not include it by default, because of reduced memory usage. This can be done by adding the following dependencies:

// The kotlin version is the same as the used version
implementation "Org. Jetbrains. Kotlin: kotlin - reflect: 1.5.21"
Copy the code

Specific use, we will continue in the following article.

conclusion

This is just an introduction to what reflection and reflection means, and to let Android developers know that if they use Kotlin, they can use the Reflection API in the Kotlin library.

We’ll step through and compare Java and Kotlin’s reflection implementation and specific use examples of reflection.