google / google/guice

[Feature request] Assisted inject in jdk8+ compiled with -parameters not requiring @Assisted

Open
#1,142 5 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

# Introduction

I suggest to build a feature into guice that allows, when jdk8+ is used and `-parameters` flag is used with the compiler, guice can correlate the builder's methods with the right constructor based only on the names of the parameters which get stored in the `.class` file.
Allowing not requiring `@Assisted` at all.
It could work like this:
(Disclaimer: I don't know guice internals in detail so I may mention some things wrong. Just point me out and I will fix)

The high level idea works like this:

# Proposal

## API
I have two possibilities, you may implement both or even suggest an alternative one

### Possibility 1
marching by param name is done by (for example):
- `new FactoryModuleBuilder().withParamNameMatching()`

### Possibility 2
Change FactoryProvider2's constructor so it can get flags. So far, only has one flag. If matching by param name is enabled (see above how it is set)
Alternatively, add a parameter to AssistedInject telling to match parameter names (E.g. @AssistedInject("MatchInterfaceParamNames"))

## Changes
### Store param names
#### Intro
Change Key or TypeLiteral so it also stores the param name when it is present.
Use:
- `Parameter::isNamePresent()`
- `Method::getParameters` gives the wanted `Parameter[]`

#### Alternative 1
FactoryProvider2:264 (281 in master branch):
- Let the Key also store the names

#### Alternative 2
FactoryProvider2:259
- Change TypeLiteral so it also stores parameter names

### Manipulate param names

#### Manipulation 1
FactoryProvider2:496 (530 in master branch) -> FactoryProvider2.constructorHasMatchingParams:544
- before "// If any keys remain and their annotation is Assisted, we can't use it."
- If flag (mentioned above) is set and if Keys have parameter names:
-- Try matching the remaining name vs name
--- apply `constructorKeys.remove(key)` to the ones that matched

#### Manipulation 2
FactoryProvider2:498 (533 in master branch)
- `"%s has more than one constructor annotated with @AssistedInject"`...
If the flag is active (either from the annotation or the method call), add the text:
`"Confirm that you are compiling the code with '-parameters' in a jdk8+ compiler that supports the flag"`

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.