From 3f005e8e156552aab1f14c47bf3a2471a418cf0b Mon Sep 17 00:00:00 2001 From: Kuan-Ying Chou Date: Fri, 24 Nov 2023 10:29:38 -0800 Subject: [PATCH] Ban constructor injection on Kotlin inline value classes RELNOTES=N/A PiperOrigin-RevId: 585117945 --- .../codegen/validation/InjectValidator.java | 7 +++++++ ...InjectConstructorFactoryGeneratorTest.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/java/dagger/internal/codegen/validation/InjectValidator.java b/java/dagger/internal/codegen/validation/InjectValidator.java index f223f6a9a08..6056a849aee 100644 --- a/java/dagger/internal/codegen/validation/InjectValidator.java +++ b/java/dagger/internal/codegen/validation/InjectValidator.java @@ -255,6 +255,13 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem constructorElement); } + if (enclosingElement.isValueClass()) { + builder.addError( + String.format("@%s constructors on Kotlin inline value classes is not supported", + injectAnnotation.simpleName()), + constructorElement); + } + // Note: superficial validation of the annotations is done as part of getting the scopes. ImmutableSet scopes = injectionAnnotations.getScopes(constructorElement.getEnclosingElement()); diff --git a/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java b/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java index bd2886bde2b..5381c8ffcba 100644 --- a/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java +++ b/javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java @@ -147,6 +147,27 @@ public final class InjectConstructorFactoryGeneratorTest { }); } + @Test public void injectConstructorOnInlineValueClass() { + Source file = + CompilerTests.kotlinSource( + "test.InlineValueClass.kt", + "package test", + "", + "import javax.inject.Inject;", + "", + "@JvmInline value class InlineValueClass", + " @Inject constructor(private val v: Int)"); + CompilerTests.daggerCompiler(file) + .compile( + subject -> { + subject.hasErrorCount(1); + subject.hasErrorContaining( + "@Inject constructors on Kotlin inline value classes is not supported") + .onSource(file) + .onLine(6); + }); + } + @Test public void fieldAndMethodGenerics() { Source file = CompilerTests.javaSource(