diff --git a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt index e496c29f266..b211ff1546f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt @@ -63,6 +63,10 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( } } + val samWithReceiverAnnotations: List? by lazy { + template.annotations.firstIsInstanceOrNull()?.annotations?.toList() + } + private val acceptedAnnotations: List> by lazy { val resolveMethod = ScriptDependenciesResolver::resolve val resolverMethodAnnotations = @@ -146,9 +150,10 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate( override val text: CharSequence? by lazy { getFileContents(myFile) } } - override fun toString(): String { - return "KotlinScriptDefinitionFromAnnotatedTemplate - ${template.simpleName}" - } + override fun toString(): String = "KotlinScriptDefinitionFromAnnotatedTemplate - ${template.simpleName}" + + override val annotationsForSamWithReceivers: List + get() = samWithReceiverAnnotations ?: super.annotationsForSamWithReceivers companion object { internal val log = Logger.getInstance(KotlinScriptDefinitionFromAnnotatedTemplate::class.java) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/script/scriptTemplate.kt b/compiler/frontend/src/org/jetbrains/kotlin/script/scriptTemplate.kt index 9f4076bc371..ad6278d9496 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/script/scriptTemplate.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/script/scriptTemplate.kt @@ -30,6 +30,10 @@ const val DEFAULT_SCRIPT_FILE_PATTERN = ".*\\.kts" annotation class ScriptTemplateDefinition(val resolver: KClass = BasicScriptDependenciesResolver::class, val scriptFilePattern: String = DEFAULT_SCRIPT_FILE_PATTERN) +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +annotation class SamWithReceiverAnnotations(vararg val annotations: String) + interface ScriptContents { data class Position(val line: Int, val col: Int) diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index f287961a109..0f6dfe02f43 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -164,6 +164,7 @@ import org.jetbrains.kotlin.shortenRefs.AbstractShortenRefsTest import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.types.AbstractTypeBindingTest import org.jetbrains.kotlin.idea.refactoring.AbstractNameSuggestionProviderTest +import org.jetbrains.kotlin.samWithReceiver.AbstractSamWithReceiverScriptTest import org.jetbrains.kotlin.samWithReceiver.AbstractSamWithReceiverTest import java.io.File import java.lang.IllegalArgumentException @@ -1178,6 +1179,9 @@ fun main(args: Array) { testClass { model("diagnostics") } + testClass { + model("script", extension = "kts") + } } testGroup("plugins/android-extensions/android-extensions-idea/tests", "plugins/android-extensions/android-extensions-idea/testData") { diff --git a/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt b/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt new file mode 100644 index 00000000000..c8903789d0f --- /dev/null +++ b/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * 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.jetbrains.kotlin.samWithReceiver + +import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest +import org.jetbrains.kotlin.config.JVMConfigurationKeys +import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor +import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate +import org.jetbrains.kotlin.script.SamWithReceiverAnnotations +import org.jetbrains.kotlin.script.ScriptTemplateDefinition + +abstract class AbstractSamWithReceiverScriptTest : AbstractDiagnosticsTest() { + private companion object { + private val TEST_ANNOTATIONS = emptyList() + } + + override fun createEnvironment() = super.createEnvironment().apply { + StorageComponentContainerContributor.registerExtension(project, CliSamWithReceiverComponentContributor(TEST_ANNOTATIONS)) + val def = KotlinScriptDefinitionFromAnnotatedTemplate(ScriptForSamWithReceivers::class, null, null, emptyMap()) + configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, def) + } +} + +@ScriptTemplateDefinition +@SamWithReceiverAnnotations("SamWithReceiver1", "SamWithReceiver2") +abstract class ScriptForSamWithReceivers diff --git a/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptTestGenerated.java b/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptTestGenerated.java new file mode 100644 index 00000000000..cd0dc584991 --- /dev/null +++ b/plugins/plugins-tests/tests/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptTestGenerated.java @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * 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.jetbrains.kotlin.samWithReceiver; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TargetBackend; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class SamWithReceiverScriptTestGenerated extends AbstractSamWithReceiverScriptTest { + public void testAllFilesPresentInScript() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script"), Pattern.compile("^(.+)\\.kts$"), TargetBackend.ANY, true); + } + + @TestMetadata("samConversionSimple.kts") + public void testSamConversionSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts"); + doTest(fileName); + } +} diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts new file mode 100644 index 00000000000..5cae9fe5dd5 --- /dev/null +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts @@ -0,0 +1,25 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE + +// FILE: SamWithReceiver1.java +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface SamWithReceiver1 { +} + +// FILE: Sam.java +@SamWithReceiver1 +public interface Sam { + void run(String a); +} + +// FILE: Exec.java +public class Exec { + void exec(Sam sam) {} +} + +// FILE: test.kts +val e = Exec() + +e.exec { System.out.println(this) } diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt new file mode 100644 index 00000000000..f20d9436d4a --- /dev/null +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt @@ -0,0 +1,34 @@ +package + +@SamWithReceiver1 public /*synthesized*/ fun Sam(/*0*/ function: (a: kotlin.String!) -> kotlin.Unit): Sam + +public open class Exec { + public constructor Exec() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public/*package*/ open fun exec(/*0*/ sam: Sam!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +@SamWithReceiver1 public interface Sam { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public abstract fun run(/*0*/ a: kotlin.String!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +@kotlin.annotation.Retention(value = AnnotationRetention.RUNTIME) public final annotation class SamWithReceiver1 : kotlin.Annotation { + public constructor SamWithReceiver1() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Test : kotlin.script.templates.standard.ScriptTemplateWithArgs { + public constructor Test(/*0*/ args: kotlin.Array) + public final override /*1*/ /*fake_override*/ val args: kotlin.Array + public final val e: Exec + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +}