Skip to content

Commit

Permalink
Add ParameterMetadata.hasDefaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
udalov committed Dec 14, 2017
1 parent c4c20bf commit e7af2cb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/kotlinx/reflect/lite/ParameterMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
14 changes: 14 additions & 0 deletions src/test/java/kotlinx/reflect/lite/test/SmokeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions src/test/java/kotlinx/reflect/lite/test/testData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {}
}

0 comments on commit e7af2cb

Please sign in to comment.