Hi All,

I'd like to use AssistedInject with classes whose names are only known
at runtime [note: I'm using Guice3.0rc1].

With standard Guice injection this is simple; just using
"injector.getInstance(someClass)" will dynamically create a just-in-time
binding for that specified class, and instantiate the object with
appropriate injection of dependencies. But I'm not sure how to do the
equivalent with classes using @Assisted.

After my app starts up, it reads an xml file which contains a list of
names of classes which subclass "Job", and creates an instance of each
of the specified classes. The constructor for each specified class
contains one "manual" parameter and some Guice singletons. 

== current code

Currently I manually write a factory class for each Job, as described in
"factories by hand" here:
  http://code.google.com/p/google-guice/wiki/AssistedInject
and then use this in a loop:
  String factoryClassName = ....; // read from config file
  Class<JobFactory> factoryClass = Class.forName(factoryClassName);
  JobFactory factory = injector.getInstance(factoryClass);
  Job job = factory.create(manualParam);

This works, but writing the factory classes by hand is a nuisance.

== Guice 2.0-style attempt

I've tried using the Guice2.0 class FactoryProvider (deprecated in
3.0rc2) as follows:

  Class<? extends Job> jobClass = (Class<? extends Job>)
    Class.forName(jobClassName);
  Provider<JobFactory> provider =
   FactoryProvider.newFactory(JobFactory.class, jobClass);
  Job job = provider.get().newInstance(manualParam);

This code is nicer (no manual factory), but I get the following error:
  java.lang.IllegalStateException: Factories.create() factories
    cannot be used until they're initialized by Guice.

== Guice 3.0-style attempt

I can't see how to "dynamically" create factories at all with Guice3.0;
the "FactoryProvider2" class is package-scoped. Using a Module isn't
possible, as the classes are being created after Guice initialisation is
complete.


Any suggestions are very welcome...

Thanks,
Simon

Good planets are hard to find - please think of the environment before you 
print this email.
____________________________________________________________________
CAUTION - This message may contain privileged and confidential 
information intended only for the use of the addressee named above.
If you are not the intended recipient of this message you are hereby 
notified that any use, dissemination, distribution or reproduction 
of this message is prohibited. If you have received this message in 
error please notify Air New Zealand immediately. Any views expressed 
in this message are those of the individual sender and may not 
necessarily reflect the views of Air New Zealand.
_____________________________________________________________________
For more information on the Air New Zealand Group, visit us online
at http://www.airnewzealand.com 
_____________________________________________________________________

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

<<winmail.dat>>

Reply via email to