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

A dagger binding methods may be declared as a kotlin property, and we need a representation of it in DaggerExecutableElement. #4289

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.validation;

import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toKS;
Expand Down Expand Up @@ -46,7 +47,8 @@
import com.google.devtools.ksp.symbol.KSAnnotated;
import com.google.devtools.ksp.symbol.KSAnnotation;
import com.google.devtools.ksp.symbol.KSClassDeclaration;
import com.google.devtools.ksp.symbol.KSFunctionDeclaration;
import com.google.devtools.ksp.symbol.KSDeclaration;
import com.google.devtools.ksp.symbol.KSPropertyDeclaration;
import com.google.devtools.ksp.symbol.KSType;
import dagger.internal.codegen.xprocessing.XAnnotations;
import dagger.internal.codegen.xprocessing.XElements;
Expand Down Expand Up @@ -575,9 +577,12 @@ public ExecutableElement javac() {
}

@Override
public KSFunctionDeclaration ksp() {
public KSDeclaration ksp() {
checkIsKsp(backend());
return toKS(executableElement());
return isMethod(executableElement())
&& XElements.asMethod(executableElement()).isKotlinPropertyMethod()
? (KSPropertyDeclaration) toKS((XElement) executableElement())
: toKS(executableElement());
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions java/dagger/spi/model/DaggerExecutableElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package dagger.spi.model;

import com.google.devtools.ksp.symbol.KSFunctionDeclaration;
import com.google.devtools.ksp.symbol.KSDeclaration;
import com.google.errorprone.annotations.DoNotMock;
import javax.lang.model.element.ExecutableElement;

Expand All @@ -31,11 +31,12 @@ public abstract class DaggerExecutableElement {
public abstract ExecutableElement javac();

/**
* Returns the KSP representation for the executable element.
* Returns the KSP representation for the executable element. Can be either KSFunctionDeclaration
* or KSPropertyDeclaration.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract KSFunctionDeclaration ksp();
public abstract KSDeclaration ksp();

/** Returns the backend used in this compilation. */
public abstract DaggerProcessingEnv.Backend backend();
Expand Down
33 changes: 33 additions & 0 deletions javatests/dagger/internal/codegen/MultibindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@

@RunWith(JUnit4.class)
public class MultibindingTest {
@Test
public void multibindingContributedWithKotlinProperty_compilesSucessfully() {
Source component =
CompilerTests.javaSource(
"test.MyComponent",
"package test;",
"",
"import dagger.Component;",
"import java.util.Set;",
"",
"@Component(modules = TestModule.class)",
"interface MyComponent {",
" Set<String> getStrs();",
"}");
Source moduleSrc =
CompilerTests.kotlinSource(
"test.TestModule.kt",
"package test",
"",
"import dagger.Module",
"import dagger.Provides",
"import dagger.multibindings.IntoSet",
"",
"@Module",
"object TestModule {",
"@get:IntoSet",
"@get:Provides",
"val helloString: String",
" get() = \"hello\"",
"}");

CompilerTests.daggerCompiler(component, moduleSrc).compile(subject -> subject.hasErrorCount(0));
}

@Test
public void providesWithTwoMultibindingAnnotations_failsToCompile() {
Expand Down