Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #428 from cgruber/improvederror
Browse files Browse the repository at this point in the history
Fix up a case where we report errors poorly when state is wrong, usually...
  • Loading branch information
JakeWharton committed Aug 1, 2014
2 parents 1d46e2f + 10d3417 commit ed5c936
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2014 Google, Inc.
* Copyright (C) 2014 Square, 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 dagger.tests.integration.operation;

import dagger.Module;
import dagger.ObjectGraph;
import javax.inject.Inject;
import javax.inject.Qualifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static junit.framework.Assert.fail;
import static org.fest.assertions.Assertions.assertThat;

@RunWith(JUnit4.class)
public final class FailureModeErrorsTest {

@Module
static class CompleteModule {}

static class ArrayFoo {
@Inject ArrayFoo(String[] ignored) {}
}

@Module(injects = ArrayFoo.class, complete = false)
static class ArrayFooModule {}

@Test public void failOnMissingModule_arrayorgenerics() {
// Generics here are crazy to try to test for, but this code path is legit regardless.
try {
ObjectGraph.create(new CompleteModule(), new ArrayFooModule()).get(ArrayFoo.class);
fail("Should have thrown.");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains(
"java.lang.String[] is a generic class or an array and can only be bound with "
+ "concrete type parameter(s) in a @Provides method. required by class "
+ "dagger.tests.integration.operation.FailureModeErrorsTest$ArrayFoo");
}
}

@Qualifier @interface MyFoo {}

static class QualifyingFoo {
@Inject QualifyingFoo(@MyFoo String ignored) {}
}

@Module(injects = QualifyingFoo.class, complete = false)
static class QualifyingFooModule {}

@Test public void failOnMissingModule_qualified() {
try {
ObjectGraph.create(new CompleteModule(), new QualifyingFooModule()).get(QualifyingFoo.class);
fail("Should have thrown.");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains(
"@dagger.tests.integration.operation.FailureModeErrorsTest$MyFoo()/java.lang.String "
+ "is a @Qualifier-annotated type and must be bound by a @Provides method. required by "
+ "class dagger.tests.integration.operation.FailureModeErrorsTest$QualifyingFoo");
}
}
}
11 changes: 8 additions & 3 deletions core/src/main/java/dagger/internal/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,14 @@ private Binding<?> createBinding(String key, Object requiredBy, ClassLoader clas
}

String className = Keys.getClassName(key);
if (className == null || Keys.isAnnotated(key)) {
// Cannot jit-bind annotated keys or generic types.
throw new IllegalArgumentException(key);
if (className == null) {
throw new InvalidBindingException(key,
"is a generic class or an array and can only be bound with concrete type parameter(s) "
+ "in a @Provides method.");
}
if (Keys.isAnnotated(key)) {
throw new InvalidBindingException(key,
"is a @Qualifier-annotated type and must be bound by a @Provides method.");
}
Binding<?> binding =
plugin.getAtInjectBinding(key, className, classLoader, mustHaveInjections);
Expand Down

0 comments on commit ed5c936

Please sign in to comment.