Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ban constructor injection on Kotlin inline value classes #4159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions java/dagger/internal/codegen/validation/InjectValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scope> scopes =
injectionAnnotations.getScopes(constructorElement.getEnclosingElement());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down