diff --git a/CHANGELOG.md b/CHANGELOG.md index 42ce1b7..72cb85c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Add ClassMetadata.kind * Add ConstructorMetadata.isPrimary * Add DeclarationMetadata.visibility +* Add ParameterMetadata.hasDefaultValue * Update to Kotlin 1.2.0 ## 1.0.0 (Feb 15, 2016) diff --git a/src/main/java/kotlinx/reflect/lite/ParameterMetadata.kt b/src/main/java/kotlinx/reflect/lite/ParameterMetadata.kt index 5b18199..14413fe 100644 --- a/src/main/java/kotlinx/reflect/lite/ParameterMetadata.kt +++ b/src/main/java/kotlinx/reflect/lite/ParameterMetadata.kt @@ -29,4 +29,11 @@ interface ParameterMetadata { * Metadata for the type of this parameter. */ val type: TypeMetadata + + /** + * `true` if this parameter declares a default value in the source code. + * + * Note that in case the parameter inherits a default value from a super function, it's not considered to be declaring a default value. + */ + val hasDefaultValue: Boolean } diff --git a/src/main/java/kotlinx/reflect/lite/impl/ParameterMetadataImpl.kt b/src/main/java/kotlinx/reflect/lite/impl/ParameterMetadataImpl.kt index 3fc1956..d1fd458 100644 --- a/src/main/java/kotlinx/reflect/lite/impl/ParameterMetadataImpl.kt +++ b/src/main/java/kotlinx/reflect/lite/impl/ParameterMetadataImpl.kt @@ -18,6 +18,7 @@ package kotlinx.reflect.lite.impl import kotlinx.reflect.lite.ParameterMetadata import kotlinx.reflect.lite.TypeMetadata +import org.jetbrains.kotlin.serialization.Flags import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.NameResolver @@ -30,4 +31,7 @@ internal class ParameterMetadataImpl( override val type: TypeMetadata get() = TypeMetadataImpl(proto.type, nameResolver) + + override val hasDefaultValue: Boolean + get() = Flags.DECLARES_DEFAULT_VALUE.get(proto.flags) } diff --git a/src/test/java/kotlinx/reflect/lite/test/SmokeTest.kt b/src/test/java/kotlinx/reflect/lite/test/SmokeTest.kt index 6dc2b51..e69c4be 100644 --- a/src/test/java/kotlinx/reflect/lite/test/SmokeTest.kt +++ b/src/test/java/kotlinx/reflect/lite/test/SmokeTest.kt @@ -182,4 +182,18 @@ class SmokeTest { assertFalse(classMetadata.getProperty(klass.fieldByName("const"))!!.isLateinit) assertFalse(classMetadata.getProperty(klass.fieldByName("lateinit"))!!.isConst) } + + @Test + fun testParameterDefaultValue() { + val klass = ParameterDefaultValue::class.java + val subclass = ParameterDefaultValueSubclass::class.java + + val parameters = ReflectionLite.loadClassMetadata(klass)!!.getFunction(klass.methodByName("foo"))!!.parameters + assertFalse(parameters[0].hasDefaultValue) + assertTrue(parameters[1].hasDefaultValue) + + val subclassParameters = ReflectionLite.loadClassMetadata(subclass)!!.getFunction(subclass.methodByName("foo"))!!.parameters + assertFalse(subclassParameters[0].hasDefaultValue) + assertFalse(subclassParameters[1].hasDefaultValue) + } } diff --git a/src/test/java/kotlinx/reflect/lite/test/testData.kt b/src/test/java/kotlinx/reflect/lite/test/testData.kt index 0541cbb..7df7d58 100644 --- a/src/test/java/kotlinx/reflect/lite/test/testData.kt +++ b/src/test/java/kotlinx/reflect/lite/test/testData.kt @@ -106,3 +106,11 @@ object CallableModifiers { lateinit var lateinit: String const val const = 42 } + +open class ParameterDefaultValue { + open fun foo(x: Int, y: Int? = null) {} +} + +class ParameterDefaultValueSubclass : ParameterDefaultValue() { + override fun foo(x: Int, y: Int?) {} +}