question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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<T> or TypeLiteral<T> 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<T> also store the names

Alternative 2

FactoryProvider2:259

  • Change TypeLiteral<T> 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"

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
brunoaiscommented, Dec 20, 2017

@dimo414 That’s my initial proposal with the limited knowledge I have of Guice. Please comment on it and call for attention to problems I didn’t notice.

0reactions
brunoaiscommented, Jun 30, 2018

@ronshapiro Any comments about this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AssistedInject for a type with generic parameter produces ...
Dagger 2.31 The following class generates code that doesn't compile: package com.example import dagger.assisted.
Read more >
Dagger 2: Injecting user inputted parameter into object
The most basic answer is that you cannot. What you are looking for is something that is called assisted injection, and it is...
Read more >
Working with Native Image efficiently | graalvm - Medium
This article looks at common issues and misunderstandings when using native image and what problems are caused by a missing or incomplete ...
Read more >
How to change Java compiler version for Eclipse project
In this post, I will show you how to change Java version used to compile a project in Eclipse IDE. For example, you...
Read more >
Meet Android Studio | Android Developers
A flexible Gradle-based build system; A fast and feature-rich emulator; A unified environment where you can develop for all Android devices; Apply Changes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found