[Kotlin] Get the argument name of the constructor by reflection

TL;DR --From KClass which can be taken by reflection of Kotlin, the argument name of the constructor can be taken with the original name. --From the Class that can be taken by the reflection of Java, the argument name of the constructor can only be taken as "ʻarg0, ʻarg1, ʻarg2... " --Even if you get theKClass from the code whose original is Java, the argument name of the constructor will be "ʻarg0, ʻarg1, ʻarg2 ..." and the original argument name cannot be taken.

things to do

Extract the argument name of the constructor from the following class.

data class Data(val sampleInt: Int, val sampleString: String)

manner

You can do it in the following two lines.

val clazz: KClass<Data> = Data::class
val parameterNames: List<String> = clazz.constructors.first().parameters.map { it.name }

println(parameterNames) // -> [sampleInt, sampleString]

Supplement

I will supplement it with the reflection of Java (I am doing it with Java8, it seems that there are changes around the reflection in later versions, but I can not follow it).

What happens if you do it with Java reflection

It will be as follows.

val clazz: Class<Data> = Data::class.java
val parameterNames: List<String> = clazz.constructors.first().parameters.map { it.name }

println(parameterNames) // -> [arg0, arg1]

What happens if you do it for a Java class

Do it for the following Java class.

@Getter //Assumption generated by lombok
public class JavaData {
    private final Integer sampleInt;
    private final String sampleStr;

    public JavaData(Integer sampleInt, String sampleStr) {
        this.sampleInt = sampleInt;
        this.sampleStr = sampleStr;
    }
}

As shown below, the argument name cannot be taken.

val clazz: KClass<JavaData> = JavaData::class
val parameterNames: List<String> = clazz.constructors.first().parameters.map { it.name }

println(parameterNames) // -> [arg0, arg1]

Recommended Posts

[Kotlin] Get the argument name of the constructor by reflection
Get the object name of the instance created by the new operator
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Get the anime name for this term by scraping
Get the name of the test case in the JUnit test class
Ruby, Nokogiri: Get the element name of the selected node
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
When using the constructor of Java's Date class, the date advances by 1900.
Get only the ID of the container specified by docker ps
Add -parameters option to javac to get argument names by reflection
Get the ID of automatic numbering
[Ruby] Specify the argument name of the method. Meaning of the colon (:) at the end
Email sent by JavaMail Gmail cannot restore the file name of the attached file
I want to get the field name of the [Java] field. (Old tale tone)
Get the value of enum saved in DB by Rails with attribute_before_type_cast
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Get the result of POST in Java
[Java] Get the day of the specific day of the week
Get the column name from the Model instance
The process of understanding Gemfile by non-engineers
[Swift] Termination of the program by assertion
[Swift] Get the height of Safe Area
[Rails] Change the label name of f.label
About the treatment of BigDecimal (with reflection)
The contents of the data saved by CarrierWave.
I want to get a list of only unique character strings by excluding fixed character strings from the file name
graphql-ruby: How to get the name of query or mutation in controller Note
[Rails] Get the path name of the URL before the transition and change the link destination
Call a method of the parent class by explicitly specifying the name in Ruby
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
Preventing mistakes in the Logger name by copying
Verify the contents of the argument object with Mockito
[Java] Dynamic method call by reflection of enum (enum)
When you get lost in the class name
[Java] Get the length of the surrogate pair string
[JAVA] Get only the file name, excluding the extension
Customize the bean name set by Spring component-scan
How to get today's day of the week
Rename the package name of the existing jar file
[Java / Kotlin] Resize considering the orientation of the image
Get Enum by reverse lookup from the contents
The official name of Spring MVC is Spring Web MVC
[Java] How to get the authority of the folder
[Swift] Termination of the program by the fatalError function
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
[Kotlin] The mapping library by reflection based on Java basically does not work with Kotlin.