google / google/guice

Allow custom implementations of NameFactory

Open
#938 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

The grapher extension is pretty nice, and it seems it is possible to provide a custom implementation of `NameFactory` to augment rendering of the various instance. One reason might be to work around bugs in third-party libraries with broken `#toString()` implementations, such as a project using GWT/AutoBeans (see https://github.com/gwtproject/gwt/issues/9150 for an example)

Unfortunately that actually doesn't work, because the injection of `NameFactory` requires the `Graphviz` binding annotation, which is package protected.

There's reasonably easy ways around that:

```
Class graphvizAnnotation = Class.forName("com.google.inject.grapher.graphviz.Graphviz").asSubclass(Annotation.class);
Injector grapherInjector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(NameFactory.class).annotatedWith(graphvizAnnotation).toInstance(new ShortNameFactory() {
@Override
public String getInstanceName(Object instance) {
if (instance instanceof AutoBeanFactory) {
return instance.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(instance));
}
return super.getInstanceName(instance);
}
});
bind(PortIdFactory.class).annotatedWith(graphvizAnnotation).to(PortIdFactoryImpl.class);
}
});
```

... but, that's pretty ugly. It would be nice to just have the `Graphviz` annotation public. :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.