Skip to content

Commit

Permalink
A dagger binding methods may be declared as a kotlin property, and we…
Browse files Browse the repository at this point in the history
… need a representation of it in DaggerExecutableElement.

RELNOTES=n/a
PiperOrigin-RevId: 627262828
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Apr 23, 2024
1 parent 5b0aa4a commit efa34f2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
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
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
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

0 comments on commit efa34f2

Please sign in to comment.