Skip to content

Commit fdb8ec8

Browse files
kuanyingchouDagger Team
authored and
Dagger Team
committed
Ban constructor injection on Kotlin inline value classes
RELNOTES=N/A PiperOrigin-RevId: 585117945
1 parent 7121b67 commit fdb8ec8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

java/dagger/internal/codegen/validation/InjectValidator.java

+7
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
255255
constructorElement);
256256
}
257257

258+
if (enclosingElement.isValueClass()) {
259+
builder.addError(
260+
String.format("@%s constructors on Kotlin inline value classes is not supported",
261+
injectAnnotation.simpleName()),
262+
constructorElement);
263+
}
264+
258265
// Note: superficial validation of the annotations is done as part of getting the scopes.
259266
ImmutableSet<Scope> scopes =
260267
injectionAnnotations.getScopes(constructorElement.getEnclosingElement());

javatests/dagger/internal/codegen/InjectConstructorFactoryGeneratorTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,27 @@ public final class InjectConstructorFactoryGeneratorTest {
147147
});
148148
}
149149

150+
@Test public void injectConstructorOnInlineValueClass() {
151+
Source file =
152+
CompilerTests.kotlinSource(
153+
"test.InlineValueClass.kt",
154+
"package test",
155+
"",
156+
"import javax.inject.Inject;",
157+
"",
158+
"@JvmInline value class InlineValueClass",
159+
" @Inject constructor(private val v: Int)");
160+
CompilerTests.daggerCompiler(file)
161+
.compile(
162+
subject -> {
163+
subject.hasErrorCount(1);
164+
subject.hasErrorContaining(
165+
"@Inject constructors on Kotlin inline value classes is not supported")
166+
.onSource(file)
167+
.onLine(6);
168+
});
169+
}
170+
150171
@Test public void fieldAndMethodGenerics() {
151172
Source file =
152173
CompilerTests.javaSource(

0 commit comments

Comments
 (0)