Examples of Reflection in Kotlin (#4483)

* Examples of Reflection in Kotlin

* Some article updates to make better use of Assert

* Replaced printlines with log statements

* Added @Ignore to so tests with no assertions
This commit is contained in:
Graham Cox 2018-06-26 07:30:55 +01:00 committed by pauljervis
parent 7e0d553340
commit d1e092b850
3 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.kotlin.reflection
import org.junit.Ignore
import org.junit.Test
import org.slf4j.LoggerFactory
@Ignore
class JavaReflectionTest {
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
@Test
fun listJavaClassMethods() {
Exception::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
@Test
fun listKotlinClassMethods() {
JavaReflectionTest::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
@Test
fun listKotlinDataClassMethods() {
data class ExampleDataClass(val name: String, var enabled: Boolean)
ExampleDataClass::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.kotlin.reflection
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import org.slf4j.LoggerFactory
import java.math.BigDecimal
import kotlin.reflect.full.*
class KClassTest {
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
@Test
fun testKClassDetails() {
val stringClass = String::class
Assert.assertEquals("kotlin.String", stringClass.qualifiedName)
Assert.assertFalse(stringClass.isData)
Assert.assertFalse(stringClass.isCompanion)
Assert.assertFalse(stringClass.isAbstract)
Assert.assertTrue(stringClass.isFinal)
Assert.assertFalse(stringClass.isSealed)
val listClass = List::class
Assert.assertEquals("kotlin.collections.List", listClass.qualifiedName)
Assert.assertFalse(listClass.isData)
Assert.assertFalse(listClass.isCompanion)
Assert.assertTrue(listClass.isAbstract)
Assert.assertFalse(listClass.isFinal)
Assert.assertFalse(listClass.isSealed)
}
@Test
fun testGetRelated() {
LOG.info("Companion Object: {}", TestSubject::class.companionObject)
LOG.info("Companion Object Instance: {}", TestSubject::class.companionObjectInstance)
LOG.info("Object Instance: {}", TestObject::class.objectInstance)
Assert.assertSame(TestObject, TestObject::class.objectInstance)
}
@Test
fun testNewInstance() {
val listClass = ArrayList::class
val list = listClass.createInstance()
Assert.assertTrue(list is ArrayList)
}
@Test
@Ignore
fun testMembers() {
val bigDecimalClass = BigDecimal::class
LOG.info("Constructors: {}", bigDecimalClass.constructors)
LOG.info("Functions: {}", bigDecimalClass.functions)
LOG.info("Properties: {}", bigDecimalClass.memberProperties)
LOG.info("Extension Functions: {}", bigDecimalClass.memberExtensionFunctions)
}
}
class TestSubject {
companion object {
val name = "TestSubject"
}
}
object TestObject {
val answer = 42
}

View File

@ -0,0 +1,88 @@
package com.baeldung.kotlin.reflection
import org.junit.Assert
import org.junit.Test
import java.io.ByteArrayInputStream
import java.nio.charset.Charset
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.starProjectedType
class KMethodTest {
@Test
fun testCallMethod() {
val str = "Hello"
val lengthMethod = str::length
Assert.assertEquals(5, lengthMethod())
}
@Test
fun testReturnType() {
val str = "Hello"
val method = str::byteInputStream
Assert.assertEquals(ByteArrayInputStream::class.starProjectedType, method.returnType)
Assert.assertFalse(method.returnType.isMarkedNullable)
}
@Test
fun testParams() {
val str = "Hello"
val method = str::byteInputStream
method.isSuspend
Assert.assertEquals(1, method.parameters.size)
Assert.assertTrue(method.parameters[0].isOptional)
Assert.assertFalse(method.parameters[0].isVararg)
Assert.assertEquals(Charset::class.starProjectedType, method.parameters[0].type)
}
@Test
fun testMethodDetails() {
val codePoints = String::codePoints
Assert.assertEquals("codePoints", codePoints.name)
Assert.assertFalse(codePoints.isSuspend)
Assert.assertFalse(codePoints.isExternal)
Assert.assertFalse(codePoints.isInline)
Assert.assertFalse(codePoints.isOperator)
val byteInputStream = String::byteInputStream
Assert.assertEquals("byteInputStream", byteInputStream.name)
Assert.assertFalse(byteInputStream.isSuspend)
Assert.assertFalse(byteInputStream.isExternal)
Assert.assertTrue(byteInputStream.isInline)
Assert.assertFalse(byteInputStream.isOperator)
}
val readOnlyProperty: Int = 42
lateinit var mutableProperty: String
@Test
fun testPropertyDetails() {
val roProperty = this::readOnlyProperty
Assert.assertEquals("readOnlyProperty", roProperty.name)
Assert.assertFalse(roProperty.isLateinit)
Assert.assertFalse(roProperty.isConst)
Assert.assertFalse(roProperty is KMutableProperty<*>)
val mProperty = this::mutableProperty
Assert.assertEquals("mutableProperty", mProperty.name)
Assert.assertTrue(mProperty.isLateinit)
Assert.assertFalse(mProperty.isConst)
Assert.assertTrue(mProperty is KMutableProperty<*>)
}
@Test
fun testProperty() {
val prop = this::mutableProperty
Assert.assertEquals(String::class.starProjectedType, prop.getter.returnType)
prop.set("Hello")
Assert.assertEquals("Hello", prop.get())
prop.setter("World")
Assert.assertEquals("World", prop.getter())
}
}