Kotlin - unrecommended usage of private members warning for public field injection
See original GitHub issueDescribe the bug
Kotlin - unrecommended usage of private members warning for public field injection
[INFO] [io.quarkus.arc.processor.BeanProcessor] Found unrecommended usage of private members (use package-private instead) in application beans:
- @Inject field org.acme.GreetingResource$ErrorMapper#request
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 1358ms
Equivalent java code has no warning.
Another example are quarkus quickstarts - mvn clean package -DskipTests -pl hibernate-orm-panache-kotlin-quickstart,hibernate-orm-panache-quickstart
CC @mkouba
Expected behavior
No warning
Actual behavior
Found unrecommended usage of private members (use package-private instead) in application beans warning
How to Reproduce?
- get sample kotlin app - https://code.quarkus.io/?e=kotlin&extension-search=origin:platform quarkus-kotlin
- extend GreetingResource.kt
- run
mvn clean package -DskipTests
and check the log
GreetingResource.kt:
package org.acme
import io.quarkus.logging.Log
import io.vertx.core.http.HttpServerRequest
import javax.inject.Inject
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
import javax.ws.rs.ext.ExceptionMapper
import javax.ws.rs.ext.Provider
@Path("/hello")
class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
fun hello() = "Hello from RESTEasy Reactive"
@GET
@Path("/fail")
fun getFail() {
throw WebApplicationException("Custom fail", 404)
}
@Provider
public class ErrorMapper : ExceptionMapper<Exception> {
@Inject
public var request: HttpServerRequest? = null
override fun toResponse(exception: Exception): Response {
Log.info("Exception for client " + request!!.remoteAddress());
var code = 500
if (exception is WebApplicationException) {
code = exception.response.status
}
return Response.status(code).build()
}
}
}
Output of uname -a
or ver
macOS Monterey
Output of java -version
Java 17
GraalVM version (if different from Java)
No response
Quarkus version or git rev
2.12.1.Final
Build tool (ie. output of mvnw --version
or gradlew --version
)
No response
Additional information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:15 (14 by maintainers)
Top Results From Across the Web
Why does Quarkus warn me about injection in private fields?
BeanProcessor] Found unrecommended usage of private members (use package-private instead) in application beans: - @Inject field ...
Read more >Quarkus private field injection : IDEA-224093 - YouTrack
In Quarkus it is discouraged to use private field injection and doing so will result ... Found unrecommended usage of private members (use...
Read more >Contexts and Dependency Injection - Quarkus
That's why Quarkus users are encouraged not to use private members in their beans. This involves injection fields, constructors and initializers, ...
Read more >A Spring Boot field injection gotcha | Medium
Constructor injection is the norm in Kotlin, and is preferred in Java as well. ... it will always be used, even if not...
Read more >Field injection is not recommended – Spring IOC - Marc Nuri
When running a static code analysis tool or inspecting/analyzing your code from your IDE, you may have encountered the following warning ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That’s what I thought.
Using
lateinit
is actually the “proper” way to do field injection in Kotlin, so I propose this being closed.Great, thank you!