Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p> An enum class cannot be constructed, its only valid instances are its enum values. That menas you have to bind a specific enum value that will be injected into whatever field or parameter of that enum type.</p> <p>Guice/GIN encourages you to use binding annotations for constants, so you can possibly inject different constant values depending on context; e.g.</p> <pre class="lang-java prettyprint-override"><code>@Named("foo") @Inject MyEnum myEnum; – bindConstant().annotatedWith(Names.named("foo")).to(MyEnum.FOO); </code></pre> <p>If you don't want to use a binding annotation (because you know you'll only ever want a single enum value throughout your app), you cannot use <code>bindConstant()</code>, but you can use <code>toInstance</code>:</p> <pre class="lang-java prettyprint-override"><code>@Inject MyEnum myEnum; … bind(MyEnum.class).toInstance(MyEnum.FOO); </code></pre> <p>This will only work in Guice though, not in GIN, which <a href="https://code.google.com/p/google-gin/wiki/GinFaq#What_bindings_don%27t_work?" rel="nofollow">doesn't have <code>toInstance</code></a>. In GIN, you have to use a <code>Provider</code> class or a <code>@Provides</code> method:</p> <pre class="lang-java prettyprint-override"><code>class MyEnumProvider implements Provider&lt;MyEnum&gt; { @Override public MyEnum get() { return MyEnum.FOO; } } … bind(MyEnum.class).toProvider(MyEnumProvider.class); </code></pre> <p>or</p> <pre class="lang-java prettyprint-override"><code>@Provides MyEnum provideMyEnum() { return MyEnum.FOO; } </code></pre> <p>Both approaches above will also work with Guice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload