google / google/guice

Feature Request : Ability to specify additional annotation as injecetion points

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

Description

Hi Guys,

A few of the newer libraries are mixing and matching other annotations for injection points, such as MicroProfile Config,
Whereby the ```@ConfigProperty(name = "test")``` is both a named, and an injection point.

I have overcome this through shading the guice-core library, and adding a very simple SPI Interface to InjectionPoint class,

```
static Annotation getAtInject(AnnotatedElement member) {
Annotation a = member.getAnnotation(jakarta.inject.Inject.class);
a = a == null ? member.getAnnotation(Inject.class) : a;
//#GedMarc update to allow alternative injection pointers
if(a == null) {
List> annotations = new ArrayList<>();
ServiceLoader load = ServiceLoader.load(InjectionPointProvider.class);
load.forEach(iPoint -> {
annotations.add(iPoint.injectionPoint(member));
});
for (Class annotation : annotations) {
//noinspection ConstantValue
if (a == null) {
a= member.getAnnotation(annotation);
if (a != null) {
a = new Inject(){
@Override
public Class annotationType() {
return annotation;
}
@Override
public boolean optional() {
return member.isAnnotationPresent(Nullable.class);
}
};
break;
}
}
}
}
return a;
}
```

While I am sure there is probably a much cleaner way to implement this functionality, this rather simple extension works perfectly so far across the spectrum (of my visibility)

Would it be possible to consider such an update to the Guice framework for enabling this modification of Injectables, to allow any custom annotation binding to be used as an injection point according to a client service provider?

This has been done in the GuicedEE framework, to enable MicroProfile and meet the TCK requirements,

Many thanks

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.