/** * 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()); }
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()); } }
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()); }
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()); }
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()); }
/** 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()); }
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()); } }
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()); }
/** * 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()); }
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); }
private static Object appendModules(Object source, ElementSource elementSource) { String modules = moduleSourceString(elementSource); if (modules.length() == 0) { return source; } else { return source + modules; } }
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(); }
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); } }
/** * 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()); }
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); }
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(); }
public 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 { formatter.format(" at %s%s%n", source, modules); } }
public void testNormalBinding() throws CreationException { final Foo foo = new Foo(); Injector injector = Guice.createInjector(new AbstractModule() { 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()); }
public void testConstantBinding() throws CreationException { Injector injector = Guice.createInjector(new AbstractModule() { 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()); }
public static Object convert(Object o, ElementSource source) { return Messages.convert(o, source); }
/** * Turns the given Guice {@link Binding}s into HK2 {@link Binder}s. */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Set<Binder> toBinders(Map<Key<?>, Binding<?>> bindings) { Set<Binder> binders = new HashSet<>(); for (Map.Entry<Key<?>, Binding<?>> entry : bindings.entrySet()) { Key<?> key = entry.getKey(); Binding<?> binding = entry.getValue(); Object source = binding.getSource(); if (!(source instanceof ElementSource)) { // Things like the Injector itself don't have an ElementSource. if (LOG.isTraceEnabled()) { LOG.trace("Adding binding: key={}, source={}", key, source); } binders.add(new GuiceBinder(key, binding)); continue; } ElementSource element = (ElementSource)source; List<String> names = element.getModuleClassNames(); String name = names.get(0); // Skip everything that is declared in a JerseyModule try { Class<?> module; // Attempt to load the classes via the context class loader first, in order to support // environments that enforce tighter constraints on class loading (such as in an OSGi container) ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if(classLoader != null) { module = classLoader.loadClass(name); } else { module = Class.forName(name); } if (JerseyModule.class.isAssignableFrom(module)) { if (LOG.isTraceEnabled()) { LOG.trace("Ignoring binding {} in {}", key, module); } continue; } } catch (ClassNotFoundException err) { // Some modules may not be able to be instantiated directly here as a class if we're running // in a container that enforcer tighter class loader constraints (such as the // org.ops4j.peaberry.osgi.OSGiModule Guice module when running in an OSGi container), // so we're only logging a warning here instead of throwing a hard exception if (LOG.isWarnEnabled()) { LOG.warn("Unavailable to load class in order to validate module: name={}", name); } } binders.add(new GuiceBinder(key, binding)); } return binders; }