本文整理汇总了Java中com.google.inject.spi.ElementSource类的典型用法代码示例。如果您正苦于以下问题:Java ElementSource类的具体用法?Java ElementSource怎么用?Java ElementSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ElementSource类属于com.google.inject.spi包,在下文中一共展示了ElementSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSourceName
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
/**
* Returns a name for a Guice "source" object. This will typically be either a {@link
* StackTraceElement} for when the binding is made to the instance, or a {@link Method} when a
* provider method is used.
*/
@Override
public String getSourceName(Object source) {
if (source instanceof ElementSource) {
source = ((ElementSource) source).getDeclaringSource();
}
if (source instanceof Method) {
source = StackTraceElements.forMember((Method) source);
}
if (source instanceof StackTraceElement) {
return getFileString((StackTraceElement) source);
}
return stripPackages(source.toString());
}
示例2: formatInjectionPoint
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
private static void formatInjectionPoint(
Formatter formatter,
Dependency<?> dependency,
InjectionPoint injectionPoint,
ElementSource elementSource) {
Member member = injectionPoint.getMember();
Class<? extends Member> memberType = Classes.memberType(member);
if (memberType == Field.class) {
dependency = injectionPoint.getDependencies().get(0);
formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource));
formatter.format(" for field at %s%n", StackTraceElements.forMember(member));
} else if (dependency != null) {
formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource));
formatter.format(" for %s%n", formatParameter(dependency));
} else {
formatSource(formatter, injectionPoint.getMember());
}
}
示例3: testNormalBinding
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public void testNormalBinding() throws CreationException {
final Foo foo = new Foo();
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toInstance(foo);
}
});
Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
assertSame(foo, fooBinding.getProvider().get());
ElementSource source = (ElementSource) fooBinding.getSource();
assertNotNull(source.getDeclaringSource());
assertEquals(Key.get(Foo.class), fooBinding.getKey());
}
示例4: testConstantBinding
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public void testConstantBinding() throws CreationException {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(I.class).to(5);
}
});
Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
assertEquals(5, i.getProvider().get());
ElementSource source = (ElementSource) i.getSource();
assertNotNull(source.getDeclaringSource());
assertEquals(Key.get(int.class, I.class), i.getKey());
}
示例5: testLinkedBinding
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public void testLinkedBinding() throws CreationException {
final Bar bar = new Bar();
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Bar.class).toInstance(bar);
bind(Key.get(Foo.class)).to(Key.get(Bar.class));
}
});
Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
assertSame(bar, fooBinding.getProvider().get());
ElementSource source = (ElementSource) fooBinding.getSource();
assertNotNull(source.getDeclaringSource());
assertEquals(Key.get(Foo.class), fooBinding.getKey());
}
示例6: testCombineSources
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
/** The module returned by Modules.combine shouldn't show up in binder sources. */
public void testCombineSources() {
final Module m1 = newModule(1);
final Module m2 = newModule(2L);
final Module combined1 = Modules.combine(m1, m2);
Module skipSourcesModule =
new AbstractModule() {
@Override
protected void configure() {
install(combined1);
}
};
final Module combined2 = Modules.combine(skipSourcesModule);
Injector injector = Guice.createInjector(combined2);
ElementSource source = (ElementSource) injector.getBinding(Integer.class).getSource();
assertEquals(4, source.getModuleClassNames().size());
assertEquals(
ImmutableList.of(
m1.getClass().getName(),
combined1.getClass().getName(),
skipSourcesModule.getClass().getName(),
combined2.getClass().getName()),
source.getModuleClassNames());
StackTraceElement stackTraceElement = (StackTraceElement) source.getDeclaringSource();
assertEquals(skipSourcesModule.getClass().getName(), stackTraceElement.getClassName());
}
示例7: formatInjectionPoint
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency,
InjectionPoint injectionPoint, ElementSource elementSource) {
Member member = injectionPoint.getMember();
Class<? extends Member> memberType = Classes.memberType(member);
if (memberType == Field.class) {
dependency = injectionPoint.getDependencies().get(0);
formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource));
formatter.format(" for field at %s%n", StackTraceElements.forMember(member));
} else if (dependency != null) {
formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource));
formatter.format(" for parameter %s at %s%n",
dependency.getParameterIndex(), StackTraceElements.forMember(member));
} else {
formatSource(formatter, injectionPoint.getMember());
}
}
示例8: testLinkedBinding
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public void testLinkedBinding() throws CreationException {
final Bar bar = new Bar();
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(Bar.class).toInstance(bar);
bind(Key.get(Foo.class)).to(Key.get(Bar.class));
}
});
Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
assertSame(bar, fooBinding.getProvider().get());
ElementSource source = (ElementSource) fooBinding.getSource();
assertNotNull(source.getDeclaringSource());
assertEquals(Key.get(Foo.class), fooBinding.getKey());
}
示例9: testCombineSources
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
/**
* The module returned by Modules.combine shouldn't show up in binder sources.
*/
public void testCombineSources() {
final Module m1 = newModule(1);
final Module m2 = newModule(2L);
final Module combined1 = Modules.combine(m1, m2);
Module skipSourcesModule = new AbstractModule() {
@Override protected void configure() {
install(combined1);
}
};
final Module combined2 = Modules.combine(skipSourcesModule);
Injector injector = Guice.createInjector(combined2);
ElementSource source = (ElementSource) injector.getBinding(Integer.class).getSource();
assertEquals(source.getModuleClassNames().size(), 4);
assertEquals(ImmutableList.of(m1.getClass().getName(),
combined1.getClass().getName(), skipSourcesModule.getClass().getName(),
combined2.getClass().getName()), source.getModuleClassNames());
StackTraceElement stackTraceElement = (StackTraceElement) source.getDeclaringSource();
assertEquals(skipSourcesModule.getClass().getName(), stackTraceElement.getClassName());
}
示例10: convert
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
static Object convert(Object o, ElementSource source) {
for (Converter<?> converter : converters) {
if (converter.appliesTo(o)) {
return appendModules(converter.convert(o), source);
}
}
return appendModules(o, source);
}
示例11: appendModules
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
private static Object appendModules(Object source, ElementSource elementSource) {
String modules = moduleSourceString(elementSource);
if (modules.length() == 0) {
return source;
} else {
return source + modules;
}
}
示例12: moduleSourceString
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
private static String moduleSourceString(ElementSource elementSource) {
// if we only have one module (or don't know what they are), then don't bother
// reporting it, because the source already is going to report exactly that module.
if (elementSource == null) {
return "";
}
List<String> modules = Lists.newArrayList(elementSource.getModuleClassNames());
// Insert any original element sources w/ module info into the path.
while (elementSource.getOriginalElementSource() != null) {
elementSource = elementSource.getOriginalElementSource();
modules.addAll(0, elementSource.getModuleClassNames());
}
if (modules.size() <= 1) {
return "";
}
// Ideally we'd do:
// return Joiner.on(" -> ")
// .appendTo(new StringBuilder(" (via modules: "), Lists.reverse(modules))
// .append(")").toString();
// ... but for some reason we can't find Lists.reverse, so do it the boring way.
StringBuilder builder = new StringBuilder(" (via modules: ");
for (int i = modules.size() - 1; i >= 0; i--) {
builder.append(modules.get(i));
if (i != 0) {
builder.append(" -> ");
}
}
builder.append(")");
return builder.toString();
}
示例13: formatSource
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
String modules = moduleSourceString(elementSource);
if (source instanceof Dependency) {
Dependency<?> dependency = (Dependency<?>) source;
InjectionPoint injectionPoint = dependency.getInjectionPoint();
if (injectionPoint != null) {
formatInjectionPoint(formatter, dependency, injectionPoint, elementSource);
} else {
formatSource(formatter, dependency.getKey(), elementSource);
}
} else if (source instanceof InjectionPoint) {
formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource);
} else if (source instanceof Class) {
formatter.format(" at %s%s%n", StackTraceElements.forType((Class<?>) source), modules);
} else if (source instanceof Member) {
formatter.format(" at %s%s%n", StackTraceElements.forMember((Member) source), modules);
} else if (source instanceof TypeLiteral) {
formatter.format(" while locating %s%s%n", source, modules);
} else if (source instanceof Key) {
Key<?> key = (Key<?>) source;
formatter.format(" while locating %s%n", convert(key, elementSource));
} else if (source instanceof Thread) {
formatter.format(" in thread %s%n", source);
} else {
formatter.format(" at %s%s%n", source, modules);
}
}
示例14: getSourceName
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
/**
* Returns a name for a Guice "source" object. This will typically be either
* a {@link StackTraceElement} for when the binding is made to the instance,
* or a {@link Method} when a provider method is used.
*/
public String getSourceName(Object source) {
if (source instanceof ElementSource) {
source = ((ElementSource) source).getDeclaringSource();
}
if (source instanceof Method) {
source = StackTraceElements.forMember((Method) source);
}
if (source instanceof StackTraceElement) {
return getFileString((StackTraceElement) source);
}
return stripPackages(source.toString());
}
示例15: convert
import com.google.inject.spi.ElementSource; //导入依赖的package包/类
public static Object convert(Object o, ElementSource source) {
for (Converter<?> converter : converters) {
if (converter.appliesTo(o)) {
return appendModules(converter.convert(o), source);
}
}
return appendModules(o, source);
}