Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Tipo de parámetro de verificación de Kotlin

Cuando trabajo con el reflejo de Java, puedo hacer algo como esto: method.getParameterTypes()[i] que me da el tipo de parámetro i ( Class ).

¿Cómo puedo lograr esto usando KCallable de Kotlin? Intenté hacer algo como esto: callable.parameters[i].type pero lo único que encontré fue type.javaType pero devuelve Type que no me ayudó en absoluto. También probé parameters[i].kind pero eso no me ayudó en absoluto.

¿Cómo puedo hacer method.getParameterTypes() de Java usando KCallable de Kotlin?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si está utilizando Kotlin 1.1+,

1) Tiene kotlin-reflect , luego debe usar callable.parameters[i].type.jvmErasure.java .

2) No tienes kotlin-reflect , luego ve a 3)

Si está utilizando una versión anterior, le sugiero encarecidamente que transfiera su código a 1.1. Si por alguna razón tienes que ceñirte al anterior, pasa al 3),

3) Primero debe agregar este fragmento de código, que calcula el tipo sin procesar de un java.lang.reflect.Type dado:

 import java.lang.reflect.* val Type.rawtype: Class<*> get() = when (this) { is Class<*> -> this is ParameterizedType -> rawType as Class<*> is GenericArrayType -> genericComponentType.rawtype.asArrayType() is TypeVariable<*> -> this.bounds[0]?.rawtype ?: Any::class.java // <--- A bug of smart cast here, forcing the use of "this." is WildcardType -> lowerBounds[0].rawtype else -> throw AssertionError("Unexpected type of Type: " + javaClass) } fun Class<*>.asArrayType() = java.lang.reflect.Array.newInstance(this, 0).javaClass

luego simplemente llame a callable.parameters[i].type.javaType.rawtype .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!