Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package com.google.samples.apps.nowinandroid.core.common.network

import javax.inject.Qualifier
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER

@Qualifier
@Target(FUNCTION, VALUE_PARAMETER)
Comment on lines +21 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When explicitly defining annotation targets for a Dagger qualifier in Kotlin, it is important to include FIELD and PROPERTY in addition to FUNCTION and VALUE_PARAMETER. This ensures that the qualifier can be used for field injection (e.g., in Activities or Fragments using lateinit var), which is a common pattern in Android development. Restricting it to only FUNCTION and VALUE_PARAMETER might break existing or future field injections.

Suggested change
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
@Qualifier
@Target(FUNCTION, VALUE_PARAMETER)
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
@Qualifier
@Target(FIELD, VALUE_PARAMETER, FUNCTION, PROPERTY)

Copy link
Copy Markdown
Author

@ois0886 ois0886 May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I kept the target list scoped to the current usages in this project and to #2001.

@Dispatcher is currently used on Hilt provider functions and injected value parameters, so this PR includes FUNCTION and VALUE_PARAMETER. I left out FIELD and PROPERTY because there are no current field/property injection usages for this qualifier, and adding them would broaden the allowed targets beyond the issue's requested restriction.

Comment on lines +21 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To support field injection (common in Android components like Activities and Fragments) and to ensure compatibility with various Kotlin property usage patterns, it is recommended to include FIELD and PROPERTY in the @Target list. This prevents potential compilation errors when the qualifier is used on properties outside of constructor parameters.

Suggested change
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
@Qualifier
@Target(FUNCTION, VALUE_PARAMETER)
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
@Qualifier
@Target(FIELD, FUNCTION, PROPERTY, VALUE_PARAMETER)

@Retention(RUNTIME)
annotation class Dispatcher(val niaDispatcher: NiaDispatchers)

Expand Down