Guice, Groovy, @ Canonical and inheritance does not play well together

I have the following Groovy classes:

enum Protocol {
    File,
    Ftp,
    Sftp,
    Http,
    Https
}

@Canonical
abstract class Endpoint {
    String name
    Protocol protocol
}

@Canonical
@TupleConstructor(includeFields=true, includeSuperFields=true)
class LocalEndpoint extends Endpoint {
}

class MyAppModule extends AbstractModule {
    @Override
    protected void configure() {
        // Lots of stuff...
    }

  // Lots of other custom providers

  @Provides
    Endpoint providesEndpoint() {
        new LocalEndpoint('fileystem', Protocol.File)
    }
}

Don't worry about why I'm using a custom provider for Endpointinstead of just:

bind(Endpoint).toInstance(new LocalEndpoint('fileystem', Protocol.File))

I am 99.999% sure that it is outside of this problem and encoded in this way due to the fact that the full (very large) code is connected.

My problem is that Guice and / or Groovy cannot find a constructor for LocalEndpointwhich takes arguments Stringand Protocol:

1) Error in custom provider, groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.example.myapp.model.LocalEndpoint(java.lang.String, com.example.myapp.model.Protocol)
  at com.example.myapp.inject.MyAppModule.providesEndpoint(MyAppModule.groovy:130)
  while locating com.example.myapp.model.Endpoint
    for parameter 2 at com.example.myapp.inject.MyAppModule.providesConfig(MyAppModule.groovy:98)
  at com.example.myapp.inject.MyAppModule.providesConfig(MyAppModule.groovy:98)
  while locating com.example.myapp.config.MyAppConfig

Then it spills out a large stack trace, for the reason of which the following is indicated:

Caused by: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.example.myapp.model.LocalEndpoint(java.lang.String, com.example.myapp.model.Protocol)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1731)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1534)

Hopefully this is something that I can tweak by changing Endpointand / or LocalEndpointmaybe I need to pass some special parameters into annotations @Canonical/ @TupleConstructoror something else. Any ideas?

+4
1

, includeSuperProperties TupleConstructor, , , , :

@TupleConstructor(includeSuperProperties=true)

, :

@Canonical
abstract class Endpoint {
    String name
    Protocol protocol
}

@Canonical // You may not need this anymore
@TupleConstructor(includeSuperProperties=true) 
class LocalEndpoint extends Endpoint {
}
+3

Source: https://habr.com/ru/post/1665255/


All Articles