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.

Kotlin - unrecommended usage of private members warning for public field injection

See original GitHub issue

Describe 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?

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:closed
  • Created a year ago
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
geoandcommented, Sep 8, 2022

That’s what I thought.

Using lateinit is actually the “proper” way to do field injection in Kotlin, so I propose this being closed.

0reactions
geoandcommented, Sep 9, 2022

Great, thank you!

Read more comments on GitHub >

github_iconTop 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 >

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