google-code-export / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
2 stars 1 forks source link

@ProvidedBy doesn't work with enums #295

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Thierry reports that @ProvidedBy doesn't work with enums. @ImplementedBy and 
enums doesn't 
make sense, but this should definitely work.

Original issue reported on code.google.com by limpbizkit on 29 Dec 2008 at 8:22

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 26 Apr 2009 at 9:46

GoogleCodeExporter commented 9 years ago
This bug is over 2 years old, and AFAIK, the fix is a (less then) one-liner. 
There's a (possibly useless) test in InjectorImpl.createUninitializedBinding 
commented as "Don't try to inject arrays, or enums." - the probable reason for 
this is the wrong assumption that it makes no sense at all. The test only makes 
it fail a bit faster in case of something is wrong, and may IMHO be completely 
omitted. Could you please do it?

Original comment by Maaarti...@gmail.com on 5 Mar 2011 at 12:20

Attachments:

GoogleCodeExporter commented 9 years ago
Here's an use-case if you need one. I have an enum that defines the various 
runtime environments of my software and I use the system properties to define 
the environment at startup. I'm currently using Module but @ProvidedBy would be 
a much cleaner approach.

@ProvidedBy(EnvProvider.class)
public enum Env {
  DEVELOPMENT,STAGING,PRODUCTION;

  public static Env environment() {
    String env = System.getProperty("my.env");
    if (env != null) {
      return Env.valueOf(env.toUpperCase());
    }
    return DEVELOPMENT;
  }

  public static class EnvProvider implements Provider<Env> {
    public Env get() {
      return Env.environment();
    }
  }
}

Original comment by rka...@gmail.com on 25 Sep 2011 at 7:01

GoogleCodeExporter commented 9 years ago
I'd propose something like

public enum Env {
  @DefaultInstance DEVELOPMENT,
  STAGING,
  PRODUCTION;
}

but I can see that the Provider-based approach is much more general (and needs 
no new annotation). My use case is exactly the same. In a few days we can 
celebrate the 5th birthday of this bug.

Original comment by Maaarti...@gmail.com on 16 Dec 2013 at 9:38