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

Kotlin companion object as a provider of static methods #217

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

eutkin
Copy link

@eutkin eutkin commented Oct 26, 2018

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

@eutkin
Copy link
Author

eutkin commented Oct 26, 2018

Examples

ProxyFactory

Interfase:

@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 Factory

open 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;
  }

...

}

@eutkin
Copy link
Author

eutkin commented Oct 26, 2018

And reworked common.templ

@vietj
Copy link
Member

vietj commented Oct 26, 2018

I see two issues currently with this PR:

  • it seems to duplicate code that is already in codegen
  • there are no tests which prevent supporting this without breaking

@eutkin
Copy link
Author

eutkin commented Oct 26, 2018

To remove duplication, need to transfer the logic from ClassModel to separate services; it will take time

@vietj
Copy link
Member

vietj commented Oct 27, 2018

that's a refactoring :-)

@eutkin
Copy link
Author

eutkin commented Nov 14, 2018

@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)

eutkin added 2 commits December 5, 2018 13:10
# Conflicts:
#	src/main/java/io/vertx/codegen/ClassModel.java
#	src/main/java/io/vertx/codegen/CodeGenProcessor.java
@eutkin
Copy link
Author

eutkin commented Dec 5, 2018

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

@rgmz
Copy link
Contributor

rgmz commented Jun 5, 2019

@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.

@eutkin
Copy link
Author

eutkin commented Jun 5, 2019

@rgmz My request pool did not want to be merged due to the lack of tests. But the work of kapt is practically impossible to test (I discussed this with the guys from jetbrains). Since the pull request is quite voluminous, I did not have enough time to merge with the master branch. You can fork my fork and bring changes to the end, since I don’t want to do any more contributing to this project, because only those pull requests that are acceptable to the project lead are merged, regardless of the reasoning. Wish good luck, link to my fork with changes

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

Successfully merging this pull request may close these issues.

4 participants