mirror of
https://github.com/google/guice.git
synced 2024-04-21 12:32:36 +00:00
By setting guice_include_stack_traces flag OFF, Guice does not collect stack traces for identifying the declaring source of a binding. Instead it uses the first non-skipped module class name from the modules stack. As a result, in some cases, error messages can be slightly different with this flag. For example, the file name and line number are not always available. A sample error message with this flag : Guice creation errors: 1) Received null converting foo (bound at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest)) to java.util.Date using CustomConverter which matches only(java.util.Date) (bound at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest)). while locating java.util.Date annotated with @com.google.inject.TypeConversionTest() for field at com.google.inject.TypeConversionTest.date(TypeConversionTest.java:478) at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest) This also changes the DEFAULT flag name to ONLY_FOR_DECLARTION_SOURCE. Adds InternalFlags.getIncludeStackTraceOption() to wrap system property access. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=53610378
170 lines
4.7 KiB
Java
170 lines
4.7 KiB
Java
/**
|
|
* Copyright (C) 2006 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;
|
|
|
|
import static com.google.inject.Asserts.assertContains;
|
|
import static com.google.inject.Asserts.getDeclaringSourcePart;
|
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.annotation.Retention;
|
|
|
|
/**
|
|
* @author crazybob@google.com (Bob Lee)
|
|
*/
|
|
public class BindingAnnotationTest extends TestCase {
|
|
|
|
public void testAnnotationWithValueMatchesKeyWithTypeOnly() throws CreationException {
|
|
Injector c = Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
bindConstant().annotatedWith(Blue.class).to("foo");
|
|
bind(BlueFoo.class);
|
|
}
|
|
});
|
|
|
|
BlueFoo foo = c.getInstance(BlueFoo.class);
|
|
|
|
assertEquals("foo", foo.s);
|
|
}
|
|
|
|
public void testRequireExactAnnotationsDisablesFallback() {
|
|
try {
|
|
Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
binder().requireExactBindingAnnotations();
|
|
bindConstant().annotatedWith(Blue.class).to("foo");
|
|
bind(BlueFoo.class);
|
|
}
|
|
});
|
|
fail();
|
|
} catch (CreationException expected) {
|
|
assertContains(expected.getMessage(), "No implementation for java.lang.String annotated with",
|
|
"BindingAnnotationTest$Blue(value=5) was bound",
|
|
"at " + BindingAnnotationTest.class.getName(),
|
|
getDeclaringSourcePart(getClass()));
|
|
}
|
|
}
|
|
|
|
public void testRequireExactAnnotationsDoesntBreakIfDefaultsExist() {
|
|
Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
binder().requireExactBindingAnnotations();
|
|
bindConstant().annotatedWith(Red.class).to("foo");
|
|
bind(RedFoo.class);
|
|
}
|
|
}).getInstance(RedFoo.class);
|
|
}
|
|
|
|
public void testRequireExactAnnotationsRequireAllOptionals() {
|
|
try {
|
|
Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
binder().requireExactBindingAnnotations();
|
|
bindConstant().annotatedWith(Color.class).to("foo");
|
|
bind(ColorFoo.class);
|
|
}
|
|
});
|
|
fail();
|
|
} catch (CreationException expected) {
|
|
assertContains(expected.getMessage(), "No implementation for java.lang.String annotated with",
|
|
"BindingAnnotationTest$Color",
|
|
"at " + BindingAnnotationTest.class.getName(),
|
|
getDeclaringSourcePart(getClass()));
|
|
}
|
|
}
|
|
|
|
public void testAnnotationWithValueThatDoesntMatch() {
|
|
try {
|
|
Guice.createInjector(new AbstractModule() {
|
|
@Override
|
|
protected void configure() {
|
|
bindConstant().annotatedWith(createBlue(6)).to("six");
|
|
bind(String.class).toInstance("bar");
|
|
bind(BlueFoo.class);
|
|
}
|
|
});
|
|
fail();
|
|
} catch (CreationException expected) {
|
|
assertContains(expected.getMessage(), "No implementation for java.lang.String annotated with",
|
|
"BindingAnnotationTest$Blue(value=5) was bound",
|
|
"at " + BindingAnnotationTest.class.getName(),
|
|
getDeclaringSourcePart(getClass()));
|
|
}
|
|
}
|
|
|
|
static class BlueFoo {
|
|
@Inject @Blue(5) String s;
|
|
}
|
|
|
|
static class RedFoo {
|
|
@Inject @Red String s;
|
|
}
|
|
|
|
static class ColorFoo {
|
|
@Inject @Color(b=2) String s;
|
|
}
|
|
|
|
@Retention(RUNTIME)
|
|
@BindingAnnotation
|
|
@interface Blue {
|
|
int value();
|
|
}
|
|
|
|
@Retention(RUNTIME)
|
|
@BindingAnnotation
|
|
@interface Red {
|
|
int r() default 42;
|
|
int g() default 42;
|
|
int b() default 42;
|
|
}
|
|
|
|
@Retention(RUNTIME)
|
|
@BindingAnnotation
|
|
@interface Color {
|
|
int r() default 0;
|
|
int g() default 0;
|
|
int b();
|
|
}
|
|
|
|
public Blue createBlue(final int value) {
|
|
return new Blue() {
|
|
public int value() {
|
|
return value;
|
|
}
|
|
|
|
public Class<? extends Annotation> annotationType() {
|
|
return Blue.class;
|
|
}
|
|
|
|
@Override public boolean equals(Object o) {
|
|
return o instanceof Blue
|
|
&& ((Blue) o).value() == value;
|
|
}
|
|
|
|
@Override public int hashCode() {
|
|
return 127 * "value".hashCode() ^ value;
|
|
}
|
|
};
|
|
}
|
|
}
|