Change inheritance to fix stackoverflow in DBObject

DBObject initialization was resulting in a loop causing a StackOverflow, ultimately resulting in the app crashing (issue 1528). Remove recursive class inheritance to hopefully fix this issue.
pull/1583/head
Trevor Terris 4 years ago
parent 5d34496efc
commit 4fa8654625

@ -2,7 +2,7 @@ package com.todoroo.andlib.data
import com.todoroo.andlib.sql.DBObject import com.todoroo.andlib.sql.DBObject
class Table private constructor(private val name: String, alias: String?) : DBObject<Table>(name) { class Table private constructor(private val name: String, alias: String?) : DBObject(name) {
constructor(name: String) : this(name, null) constructor(name: String) : this(name, null)

@ -2,18 +2,10 @@ package com.todoroo.andlib.sql
import java.util.* import java.util.*
abstract class DBObject<T : DBObject<T>> internal constructor(val expression: String) : Cloneable { abstract class DBObject internal constructor(val expression: String) : Cloneable {
var alias: String? = null var alias: String? = null
open fun `as`(newAlias: String): T { abstract fun `as`(newAlias: String): DBObject
return try {
val clone = clone() as T
clone.alias = newAlias
clone
} catch (e: CloneNotSupportedException) {
throw RuntimeException(e)
}
}
protected fun hasAlias() = alias != null protected fun hasAlias() = alias != null
@ -21,7 +13,7 @@ abstract class DBObject<T : DBObject<T>> internal constructor(val expression: St
if (this === other) { if (this === other) {
return true return true
} }
if (other !is DBObject<*>) { if (other !is DBObject) {
return false return false
} }
return expression == other.expression && alias == other.alias return expression == other.expression && alias == other.alias

@ -1,12 +1,22 @@
package com.todoroo.andlib.sql package com.todoroo.andlib.sql
open class Field(expression: String) : DBObject<Field>(expression) { open class Field(expression: String) : DBObject(expression) {
fun eq(value: Any?): Criterion = if (value == null) { fun eq(value: Any?): Criterion = if (value == null) {
UnaryCriterion.isNull(this) UnaryCriterion.isNull(this)
} else { } else {
UnaryCriterion.eq(this, value) UnaryCriterion.eq(this, value)
} }
override fun `as`(newAlias: String): Field {
return try {
val clone = clone() as Field
clone.alias = newAlias
clone
} catch (e: CloneNotSupportedException) {
throw RuntimeException(e)
}
}
fun gt(value: Any?): Criterion = UnaryCriterion.gt(this, value) fun gt(value: Any?): Criterion = UnaryCriterion.gt(this, value)
fun gte(value: Any?): Criterion = UnaryCriterion.gte(this, value) fun gte(value: Any?): Criterion = UnaryCriterion.gte(this, value)

Loading…
Cancel
Save