Allow inject annotation on records
- Dominant language
- Java
- Stars
- 27
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
The following is invalid:
```java
@Inject
record Heater(Logger logger) {
void heat() {
logger.log("~ ~ ~ heating ~ ~ ~");
}
}
```
To make it work, we have to convert the record to a class and annotate the constructor instead:
```java
final class Heater {
private final Logger logger;
@Inject
Heater(Logger logger) {
this.logger = logger;
}
void heat() {
logger.log("~ ~ ~ heating ~ ~ ~");
}
}
```
This is tedious. We should be able to inject into records, it would be equivalent to constructor injection.
Currently the `@Inject` annotation has `@Target({ METHOD, CONSTRUCTOR, FIELD })`. I think if we add `TYPE` to that list, then the annotation would be allowed on records too and injection frameworks like dagger and spring can start supporting that. `TYPE` has been around since `1.5`, so a Java version update would not be necessary.
Contributor guide
Research direction
Start by locating the @Inject annotation declaration and reviewing its existing @Target values. Check the project’s validation or compatibility tests before changing the supported target set; done means records can legally use @Inject while existing annotation targets continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100