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.

IllegalArgumentException via checkArgument in `d.i.c.writing.InjectionMethod` could benefit from a message.

See original GitHub issue

I just started working in a new project and am hitting a possible blocker for teams trying to update from older versions of dagger. I went from 2.9 to 2.25.2 and got this:

e: [kapt] An exception occurred: java.lang.IllegalArgumentException
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128)
	at dagger.internal.codegen.writing.InjectionMethod.invoke(InjectionMethod.java:98)
	at dagger.internal.codegen.writing.InjectionMethods$InjectionSiteMethod.invoke(InjectionMethods.java:347)
	at dagger.internal.codegen.writing.InjectionMethods$InjectionSiteMethod.lambda$invokeAll$0(InjectionMethods.java:315)

I set a breakpoint at the throw in the checkArgument and was able to back track to the source of the issue. What i found was something like this:

object SomeObject {
  lateinit var statefulThing: Foo
    @Inject set //params based on `setStatefulThing(SomeObject, Foo)` but expects `setStatefulThing(Foo)`
}

@Component(modules = [FooModule::class])
interface SomeObjComponent {
  fun inject(someObj: SomeObject)
}

@Module
class FooModule(val bar: String) {
  @Provides
  fun theFoo(): Foo = Foo(bar)
}

class Foo(val bar: String)

DaggerSomeObjComponent.create(FooModule("barValue"))
  .inject(SomeObject)

once I figured out where the issue was , I updated the injected property to:

object SomeObject {
  @Inject
  lateinit var statefulThing: Foo
}

Which results in the expected: injection into static fields is not supported

Yes the stateful nature of the property is less than ideal and the best solution is probably a scoped instance rather than language singleton but at the least a more helpful info dump should ease the pain of the up and coming.

When debugging all the necessary info is in scope, so this _should_™ be a fairly straight forward addition once message formatting was decided.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
danysantiagocommented, Feb 11, 2020

I’m on board with disallowing injection of object classes and companion objects, encouraging a scoped binding and if that doesn’t work, as a migration path one can still have a provision method in the component to set the object’s property. @trevjonez if you make a pull request I’ll gladly pick it up and merge it internally.

1reaction
trevjonezcommented, Feb 11, 2020

I’d be happy to make the changes I was just hoping for some direction from the project maintaintainers before doing so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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