/** * Applies all scanners to the modules we've installed. We skip certain PrivateModules because * store them in more than one Modules map and only want to process them through one of the * maps. (They're stored in both maps to prevent a module from being installed more than once.) */ void scanForAnnotatedMethods() { for (ModuleAnnotatedMethodScanner scanner : scanners) { // Note: we must iterate over a copy of the modules because calling install(..) // will mutate modules, otherwise causing a ConcurrentModificationException. for (Map.Entry<Module, ModuleInfo> entry : Maps.newLinkedHashMap(modules).entrySet()) { Module module = entry.getKey(); ModuleInfo info = entry.getValue(); if (info.skipScanning) { continue; } moduleSource = entry.getValue().moduleSource; try { info.binder.install(ProviderMethodsModule.forModule(module, scanner)); } catch (RuntimeException e) { Collection<Message> messages = Errors.getMessagesFromThrowable(e); if (!messages.isEmpty()) { elements.addAll(messages); } else { addError(e); } } } } moduleSource = null; }
/** * Returns a module which creates bindings for provider methods from the given object. This is * useful notably for <a href="http://code.google.com/p/google-gin/">GIN</a> * * <p>This will skip bytecode generation for provider methods, since it is assumed that callers * are only interested in Module metadata. */ public static Module forObject(Object object) { return forObject(object, true, ProvidesMethodScanner.INSTANCE); }
@Override public void configure(Binder binder) { for (ProviderMethod<?> providerMethod : getProviderMethods(binder)) { providerMethod.configure(binder); } }
for (Class<?> c = delegate.getClass(); c != Object.class; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { Annotation annotation = getAnnotation(binder, method); if (annotation != null) { if (result == null) { result = Lists.newArrayList(); result.add(createProviderMethod(binder, method, annotation)); superMostClass = c; if (overrides(matchingSignature, method)) { String annotationString = provider.getAnnotation().annotationType() == Provides.class
Object delegate = ((ProviderMethodsModule) module).getDelegateModule(); if (moduleSource == null || !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) { binder.install(ProviderMethodsModule.forModule(module));
ProviderMethodsModule.forModule(this); List<ProviderMethod<?>> providerMethodList = providerMethodsModule.getProviderMethods(binder()); for (ProviderMethod<?> providerMethod : providerMethodList) { keysObserved.add(providerMethod.getKey());
private static Module forObject( Object object, boolean skipFastClassGeneration, ModuleAnnotatedMethodScanner scanner) { // avoid infinite recursion, since installing a module always installs itself if (object instanceof ProviderMethodsModule) { return Modules.EMPTY_MODULE; } return new ProviderMethodsModule(object, skipFastClassGeneration, scanner); }
private <T> ProviderMethod<T> createProviderMethod( Binder binder, Method method, Annotation annotation) { binder = binder.withSource(method); Errors errors = new Errors(method); // prepare the parameter providers InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral); @SuppressWarnings("unchecked") // Define T as the method's return type. TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method); Key<T> key = getKey(errors, returnType, method, method.getAnnotations()); try { key = scanner.prepareMethod(binder, annotation, key, point); } catch (Throwable t) { binder.addError(t); } Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations()); for (Message message : errors.getMessages()) { binder.addError(message); } return ProviderMethod.create( key, method, delegate, ImmutableSet.copyOf(point.getDependencies()), scopeAnnotation, skipFastClassGeneration, annotation); }
methodsBySignature.put(new Signature(method), method); Optional<Annotation> annotation = isProvider(binder, method); if (annotation.isPresent()) { result.add(createProviderMethod(binder, method, annotation.get())); if (overrides(matchingSignature, method)) { String annotationString = provider.getAnnotation().annotationType() == Provides.class ? "@Provides" : "@" + provider.getAnnotation().annotationType().getCanonicalName();
public List<ProviderMethod<?>> getProviderMethods(Binder binder) { List<ProviderMethod<?>> result = Lists.newArrayList(); for (Class<?> c = delegate.getClass(); c != Object.class; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { if (method.isAnnotationPresent(Provides.class)) { result.add(createProviderMethod(binder, method)); } } } return result; }
Object delegate = ((ProviderMethodsModule) module).getDelegateModule(); if (moduleSource == null || !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) { binder.install(ProviderMethodsModule.forModule(module));
for (Class<?> c = delegate.getClass(); c != Object.class; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { Annotation annotation = getAnnotation(binder, method); if (annotation != null) { if (result == null) { result = Lists.newArrayList(); result.add(createProviderMethod(binder, method, annotation)); superMostClass = c; if (overrides(matchingSignature, method)) { String annotationString = provider.getAnnotation().annotationType() == Provides.class
ProviderMethodsModule.forModule(this); List<ProviderMethod<?>> providerMethodList = providerMethodsModule.getProviderMethods(binder()); for (ProviderMethod<?> providerMethod : providerMethodList) { keysObserved.add(providerMethod.getKey());
private static Module forObject(Object object, boolean skipFastClassGeneration, ModuleAnnotatedMethodScanner scanner) { // avoid infinite recursion, since installing a module always installs itself if (object instanceof ProviderMethodsModule) { return Modules.EMPTY_MODULE; } return new ProviderMethodsModule(object, skipFastClassGeneration, scanner); }
Annotation[][] parameterAnnotations = method.getParameterAnnotations(); for (int i = 0; i < parameterTypes.size(); i++) { Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]); if(key.equals(Key.get(Logger.class))) { TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method); Key<T> key = getKey(errors, returnType, method, method.getAnnotations()); Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
public List<ProviderMethod<?>> getProviderMethods(Binder binder) { List<ProviderMethod<?>> result = Lists.newArrayList(); for (Class<?> c = delegate.getClass(); c != Object.class; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { if (method.isAnnotationPresent(Provides.class)) { result.add(createProviderMethod(binder, method)); } } } return result; }
/** Returns a module which creates bindings for provider methods from the given module. */ public static Module forModule(Module module) { return forObject(module, false, ProvidesMethodScanner.INSTANCE); }
@Override public void configure(Binder binder) { if (modules.add(module)) { module.configure(createForwardingBinder(binder)); binder.install(ProviderMethodsModule.forModule(module)); } } };
Object delegate = ((ProviderMethodsModule) module).getDelegateModule(); if (moduleSource == null || !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) { binder.install(ProviderMethodsModule.forModule(module));
for (Class<?> c = delegate.getClass(); c != Object.class; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { Annotation annotation = getAnnotation(binder, method); if (annotation != null) { if (result == null) { result = Lists.newArrayList(); result.add(createProviderMethod(binder, method, annotation)); superMostClass = c; if (overrides(matchingSignature, method)) { String annotationString = provider.getAnnotation().annotationType() == Provides.class