# SAM-with-receiver compiler plugin The sam-with-receiver compiler plugin makes the first parameter of the annotated Java "single abstract method" (SAM) interface method a receiver in Kotlin. This conversion only works when the SAM interface is passed as a Kotlin lambda, both for SAM adapters and SAM constructors (see the [SAM conversions documentation](java-interop.html#sam-conversions) for more details). Here is an example: ```JAVA public @interface SamWithReceiver {} @SamWithReceiver public interface TaskRunner { void run(Task task); } ``` ```KOTLIN fun test(context: TaskContext) { val runner = TaskRunner { // Here 'this' is an instance of 'Task' println("$name is started") context.executeTask(this) println("$name is finished") } } ``` ## Gradle The usage is the same to [all-open](all-open-plugin.html) and [no-arg](no-arg-plugin.html), except the fact that sam-with-receiver does not have any built-in presets, and you need to specify your own list of special-treated annotations. Kotlin: ```KOTLIN plugins { kotlin("plugin.sam.with.receiver") version "2.4.21" } ``` Groovy: ```GROOVY plugins { id "org.jetbrains.kotlin.plugin.sam.with.receiver" version "2.4.21" } ``` Then specify the list of SAM-with-receiver annotations: ```GROOVY samWithReceiver { annotation("com.my.SamWithReceiver") } ``` ## Maven ```XML kotlin-maven-plugin org.jetbrains.kotlin ${kotlin.version} sam-with-receiver org.jetbrains.kotlin kotlin-maven-sam-with-receiver ${kotlin.version} ``` ## Command-line compiler Add the plugin JAR file to the compiler plugin classpath and specify the list of sam-with-receiver annotations: ```BASH -Xplugin=$KOTLIN_HOME/lib/sam-with-receiver-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.samWithReceiver:annotation=com.my.SamWithReceiver ```