2016-12-29 9 views
4

をプレイしていない私は、次のGroovyクラスがあります。Guiceの、Groovyの、@Canonicalと継承一緒にうまく

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

を私はちょうどEndpointの代わりにカスタムプロバイダーを使用している理由を心配しないでください。

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

99.999%はこの問題の範囲外であり、完全な(非常に大きい)コードがどのように配線されているかによってコード化されています。

私の問題は、Guiceの、および/またはGroovyのはStringProtocol引数を取るLocalEndpointのコンストラクタを見つけることができないということです。

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 

それはその後、原因として以下の上場を持つ大規模なスタックトレースを出してくれる。

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) 

うまくいけば、これはおそらく、私はにいくつかの特別なパラメータを渡す必要があり、私はEndpointおよび/またはLocalEndpointを変更することで微調整ができるものです/@TupleConstructor注釈などです。何か案は?

+2

予選コンストラクタ内のパラメータnew LocalEndpoint(name: 'fileystem'、protocol:Protocol.File) –

+0

ありがとう@ToddWCrone(+1)ですが、Groovyマップコンストラクタが嫌いです。 '@ Canonical'、' @ TupleConstructor'などのおかげで、通常の "Javaスタイル"のコンストラクタ呼び出しを使用できるはずです。どのようにこのsansマップのコンストラクタを取得するための任意の提案?再度、感謝します! – smeeb

+0

修飾されたパラメータ名はより良いIMOです。より曖昧なコンストラクタを動作させるために多くの努力を払うつもりはありません。ごめんなさい。 –

答えて

3

私はあなたがTupleConstructor注釈でincludeSuperPropertiesを追加する必要があると思うし、それはさらにそれ自体で、それを解決しているようだ:

@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 { 
} 
+0

ありがとう@Igor(+1) - **うまくいった** **あなたに賞金を授与する前に、迅速なフォローアップの質問を1つ:以前は 'includeSuperFields = true'を設定しました。なぜ(私の場合は) 'includeSuperProperties'は動作しますが、' includeSuperFields'ではないのですか? +これらのそれぞれをどこで使うべきかの違いは何ですか?再度、感謝します!! – smeeb

+0

@smeebそれは奇妙な行動です。私は 'includeSuperProperties'によって捕捉されたものも' includeSuperFields'に何かを得ることを期待します。ドキュメンテーションはこれもあまり詳しく書かれていないので、わかりません。あなたの最善の策はおそらく実装を調べることです: - \ – Igor

関連する問題