I am trying to make a interface Parcelable, as such I need a interface like this
interface AB : Parcelable {
companion object {
val CREATOR : Parcelable.Creator<AB>
}
}
and my two classes A and B looking like
data class A (...): Parcelable{
...
companion object {
val CREATOR : Parcelable.Creator<AB> = object : Parcelable.Creator<AB> {
override fun newArray(size: Int): Array<AB?> {
return arrayOfNulls(size)
}
override fun createFromParcel(parcel: Parcel): AB {
return A(parcel)
}
}
}
I am struggling to implement such a interface in kotlin. It seems the interface class does not allow for the CREATOR
Perhaps I am taking the wrong approach,
I have a parcelable that contains a list of classes that are either A or B
so I am doing
parcel.readTypedList(this.list, AB.CREATOR)
I require that the list be either A or B and that is why I am using an interface.
Anyone have any advice or a possible solution?
In Kotlin, an interface can have a companion object but it is not part of the contract that must be implemented by classes that implement the interface. It is just an object associated to the interface that has one singleton instance. So it is a place you can store things, but doesn't mean anything to the implementation class.
You can however, have an interface that is implemented by a companion object of a class. Maybe you want something more like this:
interface Behavior {
fun makeName(): String
}
data class MyData(val data: String) {
companion object: Behavior { // interface used here
override fun makeName(): String = "Fred"
}
}
Note that the data class does not implement the interface, but its companion object does.
A companion object on an interface would be useful for storing constants or helper functions related to the interface, such as:
interface Redirector {
fun redirectView(newView: String, redirectCode: Int)
companion object {
val REDIRECT_WITH_FOCUS = 5
val REDIRECT_SILENT = 1
}
}
// which then can be accessed as:
val code = Redirector.REDIRECT_WITH_FOCUS
By convention classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.
You need to annotate CREATOR property with #JvmField annotation to expose it as a public static field in containing data class.
Also you can take a look at https://github.com/grandstaish/paperparcel — an annotation processor that automatically generates type-safe Parcelable wrappers for Kotlin and Java.
Related
I'm working with ViewBindings on Android, with code generated by the compiler from the xml that must look like that
class ViewBinding {...}
class MyBinding : ViewBinding {
companion object {
fun inflate (...) {
...
}
}
}
To avoid copy/pasting code, I want to create a class, accepting a class child of ViewBinding as a generic type argument, and with a companion object having the method inflate.
class MyClass<T:ViewBinding having the method inflate in the companion object of the class> { ... }
What's the smartest way to do that ?
As it is, what you're trying to do is impossible. The type parameter restrictions apply on the type itself, not its companion object, which isn't actually related to the class (it gets compiled as a separate class with no relation with the original class). The Java equivalent, implementing a static method from an interface, also isn't possible.
What you could do however, is to use the companion itself as a type parameter, the companion implementing an interface with the inflate method:
interface ViewBindingCompanion {
fun inflate(): ViewBinding
}
class MyBinding : ViewBinding {
companion object : ViewBindingCompanion {
fun inflate(): ViewBinding {
...
}
}
}
class MyClass<T : ViewBindingCompanion>
And then:
MyClass<MyBinding.Companion>()
Leave a comment if I missed something.
It's hard to explain but I basically want to be able to have an interface which my subclasses need to implement, which mandates a static constant. How would I achieve this in Kotlin?
I have the following class/interface:
interface BaseFragmentInterface {
val TAG: String
}
class BaseFragment: Fragment() {
/* this, of course, doesn't compile right now: */
companion object: BaseFragmentInterface {}
func<T: BaseFragment> push(fragment: T) {
/* Here I want to access TAG */
Log.d(T.TAG, "Push Fragment")
}
}
My subclasses need to be required to implement the interface. And my base companion object would need to be somehow abstract.
Any idea how to achieve this?
Let me precise few things.
My subclasses need to be required to implement the interface.
There's no way to achieve this, you can't force a subclass' companion object to implement your interface since the companion object of a class is not related to the companion object of its superclass.
And my base companion object would need to be somehow abstract.
Since the companion object is an object, it can't be abstract, because an object can't be abstract too.
Said that, you have two options to still achieve your desired behavior:
Use TAG as a non-static final property inside BaseFragment:
abstract class BaseFragment : Fragment() {
protected abstract val TAG: String
fun <T : BaseFragment> push(fragment: T) {
/* Here I want to access TAG */
Log.d(TAG, "Push Fragment")
}
}
class FragmentImpl : BaseFragment() {
override val TAG: String = "fragment-impl"
}
Use companion objects to hold the tag, make the type T of push() as reified and access the TAG property inside the companion object of T using the reflection.
I have a few cases where I want to add static functions or values in a base class so that I can use them in all subclasses that inherits from it.
One such case is when i want to create generic tags for each class to use in data mapping as a key, like when i want to find fragments or pass data between activities.
For example:
open class BaseClass(){
companionObject{
val TAG: String = this.javaClass.simpleName
}
}
class ChildClass: BaseClass()
class Main: Activity(){
fun startActivity(){
val intent = Intent(this, ChildClass::class.java)
intent.putExtra(ChildClass.TAG, data)
startActivity(intent)
finish()
}
}
Can this be done or am I forced to create an companion object for each class?
I don't know a solution with companions. But you could use a global reified inline function for the specific use case, you mentioned in your question:
open class BaseClass()
class ChildClass: BaseClass()
inline fun <reified T> tagOf() = T::class.java.simpleName
fun main(args: Array<String>) {
println(tagOf<BaseClass>())
println(tagOf<ChildClass>())
}
Hm... I think, you can't do it. As mentioned in this article: https://proandroiddev.com/a-true-companion-exploring-kotlins-companion-objects-dbd864c0f7f5
companion object is really a public static final class in your BaseClass. So, I think, you can't do this.
What is the most convenient way to use SLF4J or other logging approaches with kotlin?
Usually the developer is busy with boilerplate code like
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
in each and every class to get a proper logger?
What are the most convenient ways to unify/simplify this with Kotlin?
You can define an extension property on every type:
val <T : Any> T.logger: Logger
get() = LoggerFactory.getLogger(this::class.java)
use it as follows:
class X {
init {
logger.debug("init")
}
}
Here's a simple example which returns a lazily-initialized logger from a bound callable reference or a standard property. I prefer calling from a callable reference because the :: denotes reflection (related to logging).
The class which provides the Lazy<Logger>:
class LoggingProvider<T : Any>(val clazz: KClass<T>) {
operator fun provideDelegate(inst: Any?, property: KProperty<*>) =
lazy { LoggerFactory.getLogger(clazz.java) }
}
Inline functions to call them:
inline fun <reified T : Any> KCallable<T>.logger() =
LoggingProvider(T::class)
inline fun <reified T : Any> T.logger() =
LoggingProvider(T::class)
Here's an example of using them. The require assertion in the initializer shows that the loggers share a reference:
class Foo {
val self: Foo = this
val logger by this.logger()
val callableLogger by this::self.logger()
init {
require(logger === callableLogger)
}
}
I define this function in my projects to make defining a logger easier for me. It takes advantage of Kotlin's reified types.
// Defined in Utilities.kt
inline fun <reified T:Any> logFor() =
LoggerFactory.getLogger(T::class.java)
Usage:
class MyClass {
private val log = logFor<MyClass>()
...
}
Or if you are creating a lot of them:
class MyClass {
companion object {
private val log = logFor<MyClass>()
}
...
}
if you don't like the boilerplate, you can always wrap the log.info with your own logger helper:
mylog.info(this, "data that needs to be logged")
Then in the background, have some sort of hashmap that keeps track of classes of the this param that can instantiate a logger for that class.
Other options might be using AspectJ Weaving to weave a logger into each class, but this is overkill in my opinion.
I have defined a utility method for this
fun getLogger(cl: KClass<*>): Logger {
return LoggerFactory.getLogger(cl.java)!!
}
and now in each class I can use the logger like this
companion object {
private val logger = getLogger(MyClass::class)
}
This question already has answers here:
Singleton with parameter in Kotlin
(14 answers)
Closed 2 years ago.
The Kotlin reference says that I can create a singleton using the object keyword like so:
object DataProviderManager {
fun registerDataProvider(provider: DataProvider) {
//
}
}
However, I would like to pass an argument to that object. For example an ApplicationContext in an Android project.
Is there a way to do this?
Since objects do not have constructors what I have done the following to inject the values on an initial setup. You can call the function whatever you want and it can be called at any time to modify the value (or reconstruct the singleton based on your needs).
object Singleton {
private var myData: String = ""
fun init(data: String) {
myData = data
}
fun singletonDemo() {
System.out.println("Singleton Data: ${myData}")
}
}
Kotlin has a feature called Operator overloading, letting you pass arguments directly to an object.
object DataProviderManager {
fun registerDataProvider(provider: String) {
//
}
operator fun invoke(context: ApplicationContext): DataProviderManager {
//...
return this
}
}
//...
val myManager: DataProviderManager = DataProviderManager(someContext)
With most of the existing answers it's possible to access the class members without having initialized the singleton first. Here's a thread-safe sample that ensures that a single instance is created before accessing any of its members.
class MySingleton private constructor(private val param: String) {
companion object {
#Volatile
private var INSTANCE: MySingleton? = null
#Synchronized
fun getInstance(param: String): MySingleton = INSTANCE ?: MySingleton(param).also { INSTANCE = it }
}
fun printParam() {
print("Param: $param")
}
}
Usage:
MySingleton.getInstance("something").printParam()
There are also two native Kotlin injection libraries that are quite easy to use, and have other forms of singletons including per thread, key based, etc. Not sure if is in context of your question, but here are links to both:
Injekt (mine, I'm the author): https://github.com/kohesive/injekt
Kodein (similar to Injekt): https://github.com/SalomonBrys/Kodein
Typically in Android people are using a library like this, or Dagger, et al to accomplish parameterizing singletons, scoping them, etc.
I recommend that you use this form to pass arguments in a singleton in Kotlin debit that the object your constructor is deprived and blocked:
object Singleton {
fun instance(context: Context): Singleton {
return this
}
fun SaveData() {}
}
and you call it this way in the activity
Singleton.instance(this).SaveData()
If you looking for a base SingletonHolder class with more than one argument. I had created the SingletonHolder generic class, which supports to create only one instance of the singleton class with one argument, two arguments, and three arguments.
link Github of the base class here
Non-argument (default of Kotlin):
object AppRepository
One argument (from an example code in the above link):
class AppRepository private constructor(private val db: Database) {
companion object : SingleArgSingletonHolder<AppRepository, Database>(::AppRepository)
}
// Use
val appRepository = AppRepository.getInstance(db)
Two arguments:
class AppRepository private constructor(private val db: Database, private val apiService: ApiService) {
companion object : PairArgsSingletonHolder<AppRepository, Database, ApiService>(::AppRepository)
}
// Use
val appRepository = AppRepository.getInstance(db, apiService)
Three arguments:
class AppRepository private constructor(
private val db: Database,
private val apiService: ApiService,
private val storage : Storage
) {
companion object : TripleArgsSingletonHolder<AppRepository, Database, ApiService, Storage>(::AppRepository)
}
// Use
val appRepository = AppRepository.getInstance(db, apiService, storage)
More than 3 arguments:
To implement this case, I suggest creating a config object to pass to the singleton constructor.