Skip to content

Commit

Permalink
Ban @module(subcomponents) from DaggerAdapter until we properly suppo…
Browse files Browse the repository at this point in the history
…rt it

Supporting it correctly requires a fair amount of thought - Guice and Dagger scopes are different, multibindings are different, and implementing subcomponent factories+builders would be non-trivial. (We could possibly take advantage of child injectors to help?) Anyway, there's more to considered before traversing down this path.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=246354857
  • Loading branch information
ronshapiro committed May 5, 2019
1 parent c53f70d commit 785ed8d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.inject.spi.ModuleAnnotatedMethodScanner;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
* A utility to adapt classes annotated with {@link @dagger.Module} such that their
Expand Down Expand Up @@ -88,7 +89,10 @@ private DaggerCompatibilityModule(ImmutableList<Object> daggerModuleObjects) {

@Override
public void configure(Binder binder) {
binder = binder.skipSources(getClass());
for (Object module : daggerModuleObjects) {
validateNoSubcomponents(binder, module);

binder.install(ProviderMethodsModule.forModule(module, DaggerMethodScanner.INSTANCE));
checkUnsupportedDaggerAnnotations(module, binder);
}
Expand Down Expand Up @@ -116,6 +120,17 @@ private static Iterable<Method> allDeclaredMethods(Class<?> clazz) {
return methods.build();
}

private void validateNoSubcomponents(Binder binder, Object module) {
Class<?> moduleClass = module instanceof Class ? (Class<?>) module : module.getClass();
dagger.Module moduleAnnotation = moduleClass.getAnnotation(dagger.Module.class);
if (moduleAnnotation.subcomponents().length > 0) {
binder.addError(
"Subcomponents cannot be configured for modules used with DaggerAdapter. %s specifies:"
+ " %s",
moduleClass, Arrays.toString(moduleAnnotation.subcomponents()));
}
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("modules", daggerModuleObjects).toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.inject.daggeradapter;

import static com.google.common.truth.Truth.assertThat;

import com.google.inject.CreationException;
import com.google.inject.Guice;
import dagger.Module;
import dagger.Subcomponent;
import junit.framework.TestCase;

/** Tests for {@code @Module(subcomponents = Foo.class)} */

public class ModuleSubcomponentsTest extends TestCase {

@Module(subcomponents = TestSubcomponent.class)
static class ModuleWithSubcomponents {}

@Subcomponent
interface TestSubcomponent {
@Subcomponent.Builder
interface Builder {
TestSubcomponent build();
}
}

public void testModuleSubcomponentsNotSupported() {
try {
Guice.createInjector(DaggerAdapter.from(ModuleWithSubcomponents.class));
fail();
} catch (CreationException expected) {
assertThat(expected)
.hasMessageThat()
.contains("Subcomponents cannot be configured for modules used with DaggerAdapter");
assertThat(expected).hasMessageThat().contains("ModuleWithSubcomponents specifies: ");
assertThat(expected).hasMessageThat().contains("TestSubcomponent");
}
}
}

0 comments on commit 785ed8d

Please sign in to comment.