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

@GetIgnore doesn't work on companion object #93

Closed
romnovi opened this issue Nov 19, 2018 · 6 comments
Closed

@GetIgnore doesn't work on companion object #93

romnovi opened this issue Nov 19, 2018 · 6 comments

Comments

@romnovi
Copy link
Contributor

romnovi commented Nov 19, 2018

After I upgraded to 3.6.0.CR1 getignore annotation was broken

@ProxyGen
@VertxGen
interface AccountService {

    fun create(account: Account, handler: Handler<AsyncResult<Long>>)
    operator fun get(id: Long, handler: Handler<AsyncResult<Account>>)

    @GenIgnore
    companion object {

        val ADDRESS = "account"

        fun createService(vertx: Vertx): AccountService {
            return AccountServiceImpl(vertx)
        }

        fun createProxy(vertx: Vertx, address: String): AccountService {
            return AccountServiceVertxEBProxy(vertx, address)
        }
    }
}

After I run mvn package I get: Could not generate model for Companion: type mystdeim.vertx_examples.mvn_kotlin_proxy.service.AccountService.Companion is not legal for use for a constant type in code generation

There's a simple project: https://github.com/mystdeim/vertx_examples/tree/master/mvn_kotlin_proxy

@rgmz
Copy link
Contributor

rgmz commented Dec 30, 2018

This is an unfortunate regression since the alternatives to @GenIgnore are quite ugly.
(See this thread)

I'll see if I can pinpoint the cause.

@rgmz
Copy link
Contributor

rgmz commented Dec 30, 2018

This seems to be caused by the newly added checkConstantType method:

https://github.com/vert-x3/vertx-codegen/blob/b075b22b5fb41acb47ce9b15580b5c2d705a4535/src/main/java/io/vertx/codegen/ClassModel.java#L294-L299

This example service:

@VertxGen
interface ExampleService {
    @GenIgnore
    companion object {
        @JvmStatic
        fun create(vertx: Vertx): ExampleService = ExampleServiceImpl(vertx)
    }
}

is compiled into the follow Java code, which has @GenIgnore on the Companion class, but not the companion field:

@VertxGen
public interface ExampleService {
  >>>>
  ExampleService.Companion Companion = ExampleService.Companion.$$INSTANCE;
  <<<<
  static ExampleService create(Vertx vertx) {
        return Companion.create(vertx);
    }

  @GenIgnore
  public static final class Companion {
    static final ExampleService.Companion $$INSTANCE;

    public final ExampleService create(Vertx vertx) {
        return ExampleServiceImpl(vertx);
    }

    static {
      ExampleService.Companion var0 = new ExampleService.Companion();
      $$INSTANCE = var0;
    }
  }
}

@gmariotti
Copy link
Contributor

Hi @mystdeim, I'm looking into this and I will come up with an example of how to use service-proxy with Kotlin. My personal opinion is that you shouldn't use companion object for this use case and prefer, instead, top-level functions and constants

@rgmz
Copy link
Contributor

rgmz commented Jan 30, 2019

My personal opinion is that you shouldn't use companion object for this use case and prefer, instead, top-level functions and constants

To my knowledge, companion objects + @JvmStatic annotations are the only way to have static factory methods that are honoured by codegen.

Workaround solutions (prior to Kotlion 1.3) were discussed here, but they did not provide equivalent functionality:
vert-x3/vertx-guide-for-java-devs#28 (comment)

@rgmz
Copy link
Contributor

rgmz commented May 31, 2021

This will be fixed in 4.1.0, per #329.

@tsegismont
Copy link
Contributor

Fixed by #329

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

No branches or pull requests

4 participants