Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop in createInstance() caused by Copy Constructors #99

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ dokka {
suffix = "#L"
}
}
javadoc.dependsOn dokka
javadoc.dependsOn dokka
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,26 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
private fun <T : Any> KClass<T>.easiestConstructor(): KFunction<T> {
return constructors
.sortedBy { it.parameters.size }
// Filter out any constructor that uses this class as a parameter
// (e.g., a copy constructor) so we don't get into an infinite loop.
.withoutParametersOfType(this.defaultType)
.withoutArrayParameters()
.firstOrNull() ?: constructors.sortedBy { it.parameters.size }.first()
.firstOrNull() ?: constructors.sortedBy { it.parameters.size }
.withoutParametersOfType(this.defaultType)
.first()
}

private fun <T> List<KFunction<T>>.withoutArrayParameters() = filter {
it.parameters.filter { parameter -> parameter.type.toString().toLowerCase().contains("array") }.isEmpty()
}

/**
* Filters out functions with the given type.
*/
private fun <T: Any> List<KFunction<T>>.withoutParametersOfType(type: KType) = filter {
it.parameters.filter { it.type == type }.isEmpty()
}

@Suppress("SENSELESS_COMPARISON")
private fun KClass<*>.hasObjectInstance() = objectInstance != null

Expand Down
31 changes: 31 additions & 0 deletions mockito-kotlin/src/test/kotlin/CreateInstanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,27 @@ class CreateInstanceTest {
expect(result).toNotBeNull()
}

/**
* Bug: When the copy constructor is selected, we end up with an infinite
* loop. Instead, we want to select a constructor that doesn't
* take in a parameter with the same type as the one we are building.
*
* GIVEN a class with a copy constructor (in the case of the bug, the copy
* constructor has to be selected, so it must have fewer parameters
* than all other constructors)
* WHEN we make an instance of the given class
* THEN we expect that the new, non-null instance will be created without
* an exception
*/
@Test
fun copyConstructorDoesNotCauseException() {
/* When */
val result = createInstance(WithCopyConstructor::class)

/* Then */
expect(result).toNotBeNull()
}

private class PrivateClass private constructor(val data: String)

class ClosedClass
Expand Down Expand Up @@ -475,5 +496,15 @@ class CreateInstanceTest {
constructor(c: ForbiddenConstructor) : this()
}

/**
* Bug: When the copy constructor is selected, then create instance gets
* into an infinite loop. We should never use the copy constructor in
* createInstance.
*/
data class WithCopyConstructor private constructor(val x: String,
val y: String) {
constructor(other: WithCopyConstructor): this(other.x, other.y)
}

enum class MyEnum { VALUE, ANOTHER_VALUE }
}