How and Why is the annotation @HasImplicitReceiver from the gradle API working

Hello everyone,

I came across the annotation @HasImplicitReceiver from gradle. This annotation changes the behavior of how kotlin interoperates with a java SAM interface.

/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Marks a SAM interface as a target for lambda expressions / closures
 * where the single parameter is passed as the implicit receiver of the
 * invocation ({@code this} in Kotlin, {@code delegate} in Groovy) as if
 * the lambda expression was an extension method of the parameter type.
 *
 * <pre>
 *     // copySpec(Action&lt;CopySpec&gt;)
 *     copySpec {
 *         from("./sources") // the given CopySpec is the implicit receiver
 *     }
 * </pre>
 *
 * @since 3.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface HasImplicitReceiver {
}
@HasImplicitReceiver
public interface Action<T> {
    /**
     * Performs this action against the given object.
     *
     * @param t The object to perform the action on.
     */
    void execute(T t);
}
open class WebExtensionPlugin : Plugin<Project> {
    override fun apply(project: Project) {

        project.getTasks()
            .register("copyWebExtensionFiles", Copy::class.java, Action {
                // this is now instance of the Copy class
            })

    }

}

When applied to a SAM interface, kotlin now treats the single parameter of the interface method as beeing a receiver, thus you can use “this” instead of “it” which means the scope within the lambda changes entirely.

Now I’m curious to know, why does this work? Is there a general mechanism behind it which allows us to use java SAM Interfaces in that fashion, or is this a specific kotlin compiler solution just for inter-operating better with gradle? I’m asking because this annotation seems to be specific to gradle, as it belongs to the package org.gradle.api. What am I missing? It is nice that it works, but to me that seems like a lot of magic, so how is it working?

OK I think I found an explanation for that in the documentation:

https://kotlinlang.org/docs/reference/compiler-plugins.html

There is compiler plugin called “SAM-with-receiver” which I get by applying the gradle “kotlin-dsl” plugin.
And I guess the Idea IDE is just compatible with the plugin.