Skip to content

Commit

Permalink
[Guice 6] Migrate to use com.guice.inject.* for injection annotations
Browse files Browse the repository at this point in the history
Part of cashapp#2863.

This replaces usages to enable migration to [Guice 7+](https://github.com/google/guice/wiki/Guice700). This is the first (only?) step to allow projects using Guice 6.0.0 or Guice 7.0.0 to use misk-inject.

By using `jakarta.inject` instead of `javax.inject`, injections work in both 6+ and 7+ versions. The new project, `:misk-inject-guice7-test` sets up a test project with the classpath forced to Guice 7+. This acts as a guard project for basic usages. As needed, we can add more content.
  • Loading branch information
Nava2 committed Jul 31, 2023
1 parent 8e99af6 commit 67c4984
Show file tree
Hide file tree
Showing 549 changed files with 1,101 additions and 813 deletions.
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object Dependencies {
val guavaTestLib = "com.google.guava:guava-testlib"
val guice = "com.google.inject:guice"
val guiceBom = "com.google.inject:guice-bom:6.0.0"
val guice7Bom = "com.google.inject:guice-bom:7.0.0"
val guiceTestLib = "com.google.inject.extensions:guice-testlib"
val hibernateCore = "org.hibernate:hibernate-core:5.5.3.Final"
val hikariCp = "com.zaxxer:HikariCP:4.0.3"
Expand All @@ -71,6 +72,7 @@ object Dependencies {
val jacksonJsr310 = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2"
val jacksonKotlin = "com.fasterxml.jackson.module:jackson-module-kotlin:2.14.2"
val javaxAnnotation = "javax.annotation:javax.annotation-api:1.3.2"
val jakartaInject = "jakarta.inject:jakarta.inject-api:2.0.1"
val javaxInject = "javax.inject:javax.inject:1"
val javaxPersistenceApi = "javax.persistence:javax.persistence-api:2.2"
val jedis = "redis.clients:jedis:4.1.1"
Expand Down
16 changes: 16 additions & 0 deletions detekt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ complexity:
LongParameterList:
active: false

style:
ForbiddenImport:
active: true
imports:
- value: 'javax.inject.Inject'
reason: 'To be Guice 7+ compliant, `com.google.inject.*` or `jakarta.inject.*` must be used.'
- value: 'javax.inject.Qualifier'
reason: 'To be Guice 7+ compliant, `com.google.inject.*` or `jakarta.inject.*` must be used.'
- value: 'javax.inject.Singleton'
reason: 'To be Guice 7+ compliant, `com.google.inject.*` or `jakarta.inject.*` must be used.'
- value: 'javax.inject.Provider'
reason: 'To be Guice 7+ compliant, `com.google.inject.*` must be used.'
# Until moved to Guice 7, jakarta.inject.Provider causes issues with type overloads in Guice 6
- value: 'jakarta.inject.Provider'
reason: 'To be Guice 6 compliant, `com.google.inject.*` must be used.'

detektive:
active: true
AnnotatePublicApisWithJvmOverloads:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AnnotatePublicApisWithJvmOverloads(config: Config) : Rule(config) {
if (!element.valueParameters.any { it.hasDefaultValue() }) return false

// Is not annotated with @Inject
if (element.hasAnyAnnotation("javax.inject.Inject", "com.google.inject.Inject")) return false
if (element.hasAnyAnnotation("javax.inject.Inject", "jakarta.inject.Inject", "com.google.inject.Inject")) return false

// Is public
val callableMemberDescriptor = descriptor as? CallableMemberDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,23 @@ internal class AnnotatePublicApisWithJvmOverloadsTest(private val env: KotlinCor
NoErrorTestCase(
description = "Public constructor annotated with javax Inject",
code = """
import javax.inject.Inject
import jakarta.inject.Inject
class Subject @Inject constructor(x: String = "", y: Int) {}
"""
),
NoErrorTestCase(
description = "Public constructor annotated with jakarta Inject",
code = """
import jakarta.inject.Inject
class Subject @Inject constructor(x: String = "", y: Int) {}
"""
),
NoErrorTestCase(
description = "Public constructor annotated with guice Inject",
code = """
import com.google.inject.Inject
import jakarta.inject.Inject
class Subject @Inject constructor(x: String = "", y: Int) {}
"""
Expand Down
2 changes: 1 addition & 1 deletion misk-action-scopes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
dependencies {
api(Dependencies.guava)
api(Dependencies.guice)
api(Dependencies.javaxInject)
api(Dependencies.jakartaInject)
api(project(":misk-inject"))
implementation(Dependencies.kotlinReflect)
implementation(Dependencies.kotlinStdLibJdk8)
Expand Down
4 changes: 2 additions & 2 deletions misk-action-scopes/src/main/kotlin/misk/scope/ActionScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.google.inject.Provider
import kotlinx.coroutines.asContextElement
import java.util.UUID
import java.util.concurrent.Callable
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton
import kotlin.coroutines.CoroutineContext
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import misk.inject.parameterizedType
import misk.inject.toKey
import misk.inject.typeLiteral
import java.lang.reflect.Type
import javax.inject.Inject
import jakarta.inject.Inject
import kotlin.reflect.KClass
import kotlin.reflect.jvm.javaMethod

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package misk.scope

import com.google.inject.Key
import javax.inject.Inject
import jakarta.inject.Inject

internal class RealActionScoped<T> @Inject internal constructor(
val key: Key<T>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import javax.inject.Inject
import jakarta.inject.Inject
import kotlin.reflect.KFunction

internal class ActionScopePropagationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.Optional
import javax.inject.Inject
import jakarta.inject.Inject
import kotlin.test.assertFailsWith

internal class ActionScopedTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.inject.TypeLiteral
import com.google.inject.name.Named
import com.google.inject.name.Names
import java.util.Optional
import javax.inject.Inject
import jakarta.inject.Inject

internal class TestActionScopedProviderModule : ActionScopedProviderModule() {
override fun configureProviders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import org.junit.jupiter.api.Test
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton
import kotlin.test.assertFailsWith

internal class ActionScopedExecutorServiceTest {
Expand Down
2 changes: 1 addition & 1 deletion misk-admin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {

dependencies {
api(Dependencies.guice)
api(Dependencies.javaxInject)
api(Dependencies.jakartaInject)
api(Dependencies.kotlinxHtml)
api(Dependencies.moshi)
api(Dependencies.okio)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import misk.inject.KAbstractModule
import misk.security.authz.AccessAnnotationEntry
import misk.web.v2.BaseDashboardV2Module
import misk.web.metadata.config.ConfigMetadataAction
import javax.inject.Qualifier
import com.google.inject.BindingAnnotation
import jakarta.inject.Qualifier

/**
* Installs default Admin Dashboard that runs at multibound DashboardHomeUrl<AdminDashboard>
Expand Down Expand Up @@ -76,5 +77,6 @@ class AdminDashboardTestingModule : KAbstractModule() {

/** Dashboard Annotation used for all tabs bound in the Misk Admin Dashboard */
@Qualifier
@BindingAnnotation
@Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
annotation class AdminDashboard
4 changes: 2 additions & 2 deletions misk-admin/src/main/kotlin/misk/web/dashboard/DashboardTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package misk.web.dashboard

import misk.security.authz.AccessAnnotationEntry
import misk.web.dashboard.ValidWebEntry.Companion.slugify
import javax.inject.Inject
import javax.inject.Provider
import jakarta.inject.Inject
import com.google.inject.Provider
import kotlin.reflect.KClass

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import misk.web.PathParam
import misk.web.ResponseContentType
import misk.web.actions.WebAction
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Kotlin backed tab loader, equivalent to /_tab/slug/index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import misk.web.dashboard.DashboardTheme
import misk.web.dashboard.MiskWebTheme
import misk.web.mediatype.MediaTypes
import misk.web.metadata.DashboardMetadataAction.DashboardTabMetadata.Companion.toMetadata
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Serve metadata related to a Misk-Web Dashboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import misk.web.ResponseContentType
import misk.web.actions.WebAction
import misk.web.mediatype.MediaTypes
import wisp.deployment.Deployment
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Service Metadata used for front end dashboards including App Name and Misk.Deployment name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package misk.web.metadata.config

import com.google.inject.Inject
import jakarta.inject.Inject
import misk.config.AppName
import misk.config.MiskConfig
import misk.resources.ResourceLoader
Expand All @@ -13,7 +13,7 @@ import misk.web.mediatype.MediaTypes
import misk.web.metadata.jvm.JvmMetadataAction
import wisp.config.Config
import wisp.deployment.Deployment
import javax.inject.Singleton
import jakarta.inject.Singleton

@Singleton
class ConfigMetadataAction @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import misk.web.ResponseContentType
import misk.web.actions.WebAction
import misk.web.dashboard.AdminDashboardAccess
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/** Display available queries and their types */
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import misk.web.actions.WebAction
import misk.web.dashboard.AdminDashboardAccess
import misk.web.mediatype.MediaTypes
import java.lang.management.RuntimeMXBean
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Conveys information about the current JVM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import misk.web.ResponseContentType
import misk.web.actions.WebAction
import misk.web.dashboard.AdminDashboardAccess
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

@Singleton
class WebActionMetadataAction @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import misk.web.dashboard.AdminDashboardAccess
import misk.web.dashboard.DashboardTabLoader
import misk.web.dashboard.DashboardTabLoaderEntry
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Builds dashboard UI and loads Hotwire tab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import misk.web.dashboard.AdminDashboardAccess
import misk.web.dashboard.DashboardTabLoader
import misk.web.dashboard.DashboardTabLoaderEntry
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton

/**
* Builds dashboard UI and loads IFrame tab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import misk.web.ResponseContentType
import misk.web.actions.WebAction
import misk.web.dashboard.AdminDashboardAccess
import misk.web.mediatype.MediaTypes
import javax.inject.Inject
import javax.inject.Singleton
import jakarta.inject.Inject
import jakarta.inject.Singleton
import kotlin.reflect.KClass

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import misk.web.dashboard.DashboardNavbarItem
import misk.web.dashboard.DashboardTab
import misk.web.v2.DashboardIndexAction.Companion.titlecase
import wisp.deployment.Deployment
import javax.inject.Inject
import jakarta.inject.Inject

/**
* Builds dashboard UI for index homepage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import misk.web.WebActionModule
import misk.web.WebServerTestingModule
import misk.web.mediatype.MediaTypes
import misk.web.toResponseBody
import javax.inject.Inject
import jakarta.inject.Inject

// Common module for web action-related tests to use to use that bind up some sample web actions
class TestWebActionModule : KAbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import misk.web.jetty.JettyService
import okhttp3.OkHttpClient
import okhttp3.Request
import org.junit.jupiter.api.Test
import javax.inject.Inject
import jakarta.inject.Inject
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import misk.web.dashboard.DashboardTabProvider
import misk.web.dashboard.DashboardTheme
import misk.web.dashboard.MiskWebTheme
import wisp.config.Config
import javax.inject.Qualifier
import com.google.inject.BindingAnnotation
import jakarta.inject.Qualifier

// Common test module used to be able to test admin dashboard WebActions
class MetadataTestingModule : KAbstractModule() {
Expand Down Expand Up @@ -64,5 +65,6 @@ class MetadataTestingModule : KAbstractModule() {
class TestAdminDashboardConfig : Config

@Qualifier
@BindingAnnotation
@Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
annotation class DashboardMetadataActionTestDashboard
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import javax.inject.Inject
import jakarta.inject.Inject

@MiskTest(startService = true)
class ServiceMetadataActionTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import wisp.config.Config
import wisp.deployment.TESTING
import javax.inject.Inject
import jakarta.inject.Inject
import kotlin.test.assertEquals

@MiskTest(startService = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import misk.web.metadata.jvm.JvmMetadataAction.JvmRuntimeResponse
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.lang.management.RuntimeMXBean
import javax.inject.Inject
import jakarta.inject.Inject
import javax.management.ObjectName

@MiskTest(startService = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import misk.web.mediatype.MediaTypes
import misk.web.metadata.MetadataTestingModule
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import javax.inject.Inject
import jakarta.inject.Inject

@MiskTest(startService = true)
class WebActionMetadataActionTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import misk.web.HttpCall
import misk.web.actions.WebAction
import misk.web.metadata.MetadataTestingModule
import org.junit.jupiter.api.Test
import javax.inject.Inject
import javax.inject.Provider
import jakarta.inject.Inject
import com.google.inject.Provider
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

Expand Down
Loading

0 comments on commit 67c4984

Please sign in to comment.