Kotlin companion object as a provider of static methods#217
Kotlin companion object as a provider of static methods#217eutkin wants to merge 8 commits intoeclipse-vertx:masterfrom
Conversation
ExamplesProxyFactoryInterfase: @VertxGen
@ProxyGen
interface SampleService {
companion object Factory {
const val address : String = "sample.service.address"
fun createProxy(vertx: Vertx) : SampleService {
return ServiceProxyBuilder(vertx)
.setAddress(address)
.build(SampleService::class.java)
}
}
@Fluent
fun get(handler: Handler<AsyncResult<@Nullable String>>): SampleService
}Generated: @io.vertx.lang.reactivex.RxGen(net.eutkin.cluster.service.SampleService.class)
public class SampleService {
...
public static SampleService createProxy(Vertx vertx) {
SampleService ret = SampleService.newInstance(net.eutkin.cluster.service.SampleService.Factory.createProxy(vertx.getDelegate()));
return ret;
}
...
}Generic Proxy Factoryopen class ProxyFactory<T>(val address: String) {
fun createProxy(vertx: Vertx): T {
val javaClass = javaClass
val serviceType : Class<T> = when (javaClass) {
is ParameterizedType -> javaClass.actualTypeArguments[0] as Class<T>
else -> throw RuntimeException()
}
return ServiceProxyBuilder(vertx)
.setAddress(address)
.build(serviceType)
}
}
@VertxGen
@ProxyGen
interface SampleService {
companion object Factory : ProxyFactory<SampleService>("sample-service") {
}
@Fluent
fun get(handler: Handler<AsyncResult<@Nullable String>>): SampleService
}Generated: @io.vertx.lang.reactivex.RxGen(net.eutkin.cluster.service.SampleService.class)
public class SampleService {
...
public static SampleService createProxy(Vertx vertx) {
SampleService ret = SampleService.newInstance(net.eutkin.cluster.service.SampleService.Factory.createProxy(vertx.getDelegate()));
return ret;
}
...
} |
|
And reworked common.templ |
|
I see two issues currently with this PR:
|
|
To remove duplication, need to transfer the logic from ClassModel to separate services; it will take time |
|
that's a refactoring :-) |
|
@vietj, hi! I have some problem. Kotlin kapt (annotation processor) transform kotlin code to java (or generate stubs). And I can't repeate this operation in my tests. I am almost finished without tests for my code (this is bad, I know) |
# Conflicts: # src/main/java/io/vertx/codegen/ClassModel.java # src/main/java/io/vertx/codegen/CodeGenProcessor.java
|
Hi, @vietj. I refactored, as promised. Can you see the new code, at least without the companion for Kotlin? I don’t want to waste my strength on writing support |
|
@vietj @gmariotti Any news on this? This (or #229) would be a huge QoL improvement for Kotlin. Since 3.6.0 there is no Kotlin equivalent for static factory methods. |
|
@rgmz My request pool did not want to be merged due to the lack of tests. But the work of |
In kotlin there is a companion object in which it would be convenient to
store methods for creating an implementation object or a proxy object.
But codegen does not know how to handle the companion object, and therefore people have to
write different solutions on their own.
I added the ability to turn methods and fields of the companion object into static methods of the generated class.
P.S. Since 3.6.0 breaks backwards compatibility, it will be quite difficult to test my solution,
I used a slightly reworked template for rx java 2