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

Add support for Kotlin Objects as services for autoService #785

Open
ColinHebert opened this issue Nov 10, 2019 · 4 comments
Open

Add support for Kotlin Objects as services for autoService #785

ColinHebert opened this issue Nov 10, 2019 · 4 comments

Comments

@ColinHebert
Copy link

ColinHebert commented Nov 10, 2019

Kotlin has a nice singleton system thanks to kotlin objects, it creates a class that works automatically as a singleton, which from the perspective of Java looks like a class with a static field INSTANCE which contains the only instance of the class.

This is particularly helpful in the context of service loaders as it gives more control over the instance used as a service.

Unfortunately, the way ServiceLoader works currently requires classes that can be instantiated using their default constructor, which objects do not have.

But thanks to the magic of annotation processing this is actually fixable.
Without any processing this is what someone could write to make "their object" work with ServiceLoader:

interface MyInterface {
    fun myMethod() : String
}

object MyObject : MyInterface {
    override fun myMethod() : String = "hello"
}

@AutoService(MyInterface::class)
class MyObjectServiceLoaderProxy : MyInterface by MyObject

This uses the delegation mechanism also provided in Kotlin; in short, if you are implementing an interface, using by anImplementationOfSaidInterface, instances of the class will delegate the non-explicitely implemented functions to anImplementationOfSaidInterface.
Note, this does not work with abstract classes, too bad but not the end of the world.

There, now we can use ServiceLoader "with objects", we just need to tidy it up a bit, having the annotation on the object and generating the class automatically should be doable and would provide all the benefits without requiring user intervention (as long as the "service" is represented by an interface). We can also give a non standard name to the class to avoid people using it by mistake, after all this is only meant to be for the eyes of the ServiceLoader.

Thoughts?

@kluever
Copy link
Member

kluever commented Nov 10, 2019

/cc @lowasser

@lowasser
Copy link
Contributor

Seems logical, plausible, and doable. I approve, and have a sketch of an implementation, but adapting the tests might be more difficult.

@sschuberth
Copy link

I approve, and have a sketch of an implementation

Any update on that?

@tinder-cesardiez
Copy link

This works for me on AutoService 1.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants