Revision: 8f196358f5a4
Author:   Christian Edward Gruber <[email protected]>
Date:     Thu May 16 11:13:21 2013
Log: Add logging for when AssistedInject falls into non-optimized mode, so people can take action and fix it.

An @Assisted Provider in a class param:
WARNING: AssistedInject factory com.google.inject.assistedinject.FactoryProvider2Test$ColoredCarFactory will be slow because class com.google.inject.assistedinject.FactoryProvider2Test$Subaru has Assisted Provider dependencies or injects the Injector. Stop injecting Assisted Provider<T> (instead use Assisted T) or Injector to speed things up. (It will be a ~6500% speed bump!) The exact offending deps are: [Key[type=com.google.inject.Provider<com.google.inject.assistedinject.FactoryProvider2Test$Color>, [email protected](value=)]@com.google.inject.assistedinject.FactoryProvider2Test$Subaru.colorProvider]

The injector in class param:
WARNING: AssistedInject factory com.google.inject.assistedinject.FactoryProvider2Test$ColoredCarFactory will be slow because class com.google.inject.assistedinject.FactoryProvider2Test$Segway has Assisted Provider dependencies or injects the Injector. Stop injecting Assisted Provider<T> (instead use Assisted T) or Injector to speed things up. (It will be a ~6500% speed bump!) The exact offending deps are: [Key[type=com.google.inject.Injector, annotation=[none]]@com.google.inject.assistedinject.FactoryProvider2Test$Segway.injector]

An @Assisted Provider in a constructor arg:
WARNING: AssistedInject factory com.google.inject.assistedinject.FactoryProvider2Test$ColoredCarFactory will be slow because class com.google.inject.assistedinject.FactoryProvider2Test$Flamingbird has Assisted Provider dependencies or injects the Injector. Stop injecting Assisted Provider<T> (instead use Assisted T) or Injector to speed things up. (It will be a ~6500% speed bump!) The exact offending deps are: [Key[type=com.google.inject.Provider<com.google.inject.assistedinject.FactoryProvider2Test$Color>, [email protected](value=)]@com.google.inject.assistedinject.FactoryProvider2Test$Flamingbird.<init>()[1]]

----------------
Manually Synced.
COMMIT=45725763

http://code.google.com/p/google-guice/source/detail?r=8f196358f5a4

Modified:
/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java

=======================================
--- /extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java Thu May 16 11:06:08 2013 +++ /extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java Thu May 16 11:13:21 2013
@@ -25,6 +25,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.google.inject.AbstractModule;
 import com.google.inject.Binder;
 import com.google.inject.Binding;
@@ -66,6 +67,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;

 /**
* The newer implementation of factory provider. This implementation uses a child injector to
@@ -82,6 +85,9 @@
/** A constant annotation to denote the return value, instead of creating a new one each time. */
   static final Annotation RETURN_ANNOTATION = UniqueAnnotations.create();

+  // use the logger under a well-known name, not FactoryProvider2
+ static final Logger logger = Logger.getLogger(AssistedInject.class.getName());
+
/** if a factory method parameter isn't annotated, it gets this annotation. */
   static final Assisted DEFAULT_ANNOTATION = new Assisted() {
     public String value() {
@@ -272,7 +278,7 @@
// all injections directly inject the object itself (and not a Provider of the object, // or an Injector), because it caches a single child injector and mutates the Provider
         // of the arguments in a ThreadLocal.
-        if(isValidForOptimizedAssistedInject(deps)) {
+ if(isValidForOptimizedAssistedInject(deps, implementation.getRawType(), factoryType)) { ImmutableList.Builder<ThreadLocalProvider> providerListBuilder = ImmutableList.builder();
           for(int i = 0; i < params.size(); i++) {
             providerListBuilder.add(new ThreadLocalProvider());
@@ -500,12 +506,26 @@
* the assisted bindings are immediately provided. This looks for hints that the values may be * lazily retrieved, by looking for injections of Injector or a Provider for the assisted values.
    */
- private boolean isValidForOptimizedAssistedInject(Set<Dependency<?>> dependencies) { + private boolean isValidForOptimizedAssistedInject(Set<Dependency<?>> dependencies,
+      Class<?> implementation, TypeLiteral<?> factoryType) {
+    Set<Dependency<?>> badDeps = null; // optimization: create lazily
     for (Dependency<?> dep : dependencies) {
       if (isInjectorOrAssistedProvider(dep)) {
-        return false;
+        if (badDeps == null) {
+          badDeps = Sets.newHashSet();
+        }
+        badDeps.add(dep);
       }
     }
+    if (badDeps != null && !badDeps.isEmpty()) {
+      logger.log(Level.WARNING, "AssistedInject factory {0} will be slow "
+ + "because {1} has assisted Provider dependencies or injects the Injector. " + + "Stop injecting @Assisted Provider<T> (instead use @Assisted T) " + + "or Injector to speed things up. (It will be a ~6500% speed bump!) "
+          + "The exact offending deps are: {2}",
+          new Object[] {factoryType, implementation, badDeps} );
+      return false;
+    }
     return true;
   }

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


Reply via email to