diff --git a/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenClassGenerator.kt b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenClassGenerator.kt new file mode 100644 index 00000000000..dbb5a646920 --- /dev/null +++ b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenClassGenerator.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.allopen + +import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.descriptors.Modality +import org.jetbrains.kotlin.descriptors.Visibilities +import org.jetbrains.kotlin.fir.FirAnnotationContainer +import org.jetbrains.kotlin.fir.FirEffectiveVisibility +import org.jetbrains.kotlin.fir.FirEffectiveVisibilityImpl +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.FirDeclaration +import org.jetbrains.kotlin.fir.declarations.FirFile +import org.jetbrains.kotlin.fir.declarations.FirRegularClass +import org.jetbrains.kotlin.fir.declarations.builder.buildClassImpl +import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl +import org.jetbrains.kotlin.fir.extensions.AnnotationFqn +import org.jetbrains.kotlin.fir.extensions.FirClassGenerationExtension +import org.jetbrains.kotlin.fir.resolve.firProvider +import org.jetbrains.kotlin.fir.resolve.impl.FirProviderImpl +import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +class AllOpenClassGenerator(session: FirSession) : FirClassGenerationExtension(session) { + override fun generateClass( + containingFile: FirFile, + annotatedDeclaration: T + ): List where T : FirDeclaration, T : FirAnnotationContainer { + if (annotatedDeclaration !is FirRegularClass) return emptyList() + val klass = buildClassImpl { + session = this@AllOpenClassGenerator.session + origin = FirDeclarationOrigin.Plugin(AllOpenPluginKey) + status = FirResolvedDeclarationStatusImpl(Visibilities.PUBLIC, FirEffectiveVisibilityImpl.Public, Modality.FINAL) + classKind = ClassKind.OBJECT + scopeProvider = (session.firProvider as FirProviderImpl).kotlinScopeProvider + name = Name.identifier("Foo${annotatedDeclaration.name.identifier}") + symbol = FirRegularClassSymbol(ClassId(containingFile.packageFqName, name)) + superTypeRefs += session.builtinTypes.anyType + } + return listOf(GeneratedClass(klass, containingFile)) + } + + override val annotations: Set = + setOf(FqName("org.jetbrains.kotlin.fir.allopen.WithClass")) + + override val metaAnnotations: Set = emptySet() +} \ No newline at end of file diff --git a/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenPluginKey.kt b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenPluginKey.kt new file mode 100644 index 00000000000..719a615d562 --- /dev/null +++ b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/AllOpenPluginKey.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.allopen + +import org.jetbrains.kotlin.fir.declarations.FirPluginMarker + +object AllOpenPluginKey : FirPluginMarker() \ No newline at end of file diff --git a/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/FirAllOpenComponentRegistrar.kt b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/FirAllOpenComponentRegistrar.kt index 0161e9d1496..497fdfcbb51 100644 --- a/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/FirAllOpenComponentRegistrar.kt +++ b/compiler/fir/plugins/allopen-plugin/src/org/jetbrains/kotlin/fir/allopen/FirAllOpenComponentRegistrar.kt @@ -10,5 +10,6 @@ import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar class FirAllOpenComponentRegistrar : FirExtensionRegistrar() { override fun ExtensionRegistrarContext.configurePlugin() { +::AllOpenStatusTransformer + +::AllOpenClassGenerator } } \ No newline at end of file diff --git a/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.kt b/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.kt new file mode 100644 index 00000000000..f7dfb4041df --- /dev/null +++ b/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.kt @@ -0,0 +1,7 @@ +import org.jetbrains.kotlin.fir.allopen.WithClass +import org.jetbrains.kotlin.fir.allopen.AllOpen + +@WithClass +class A { + +} \ No newline at end of file diff --git a/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.txt b/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.txt new file mode 100644 index 00000000000..ac36df9a173 --- /dev/null +++ b/compiler/fir/plugins/allopen-plugin/testData/classGen/simple.txt @@ -0,0 +1,9 @@ +FILE: simple.kt + @R|org/jetbrains/kotlin/fir/allopen/WithClass|() public final class A : R|kotlin/Any| { + public constructor(): R|A| { + super() + } + + } + public final object FooA : R|kotlin/Any| { + } diff --git a/compiler/fir/plugins/allopen-plugin/tests/org/jetbrains/kotlin/fir/allopen/FirAllOpenDiagnosticTestGenerated.java b/compiler/fir/plugins/allopen-plugin/tests/org/jetbrains/kotlin/fir/allopen/FirAllOpenDiagnosticTestGenerated.java index 33c4f80a469..5f434d3a4f4 100644 --- a/compiler/fir/plugins/allopen-plugin/tests/org/jetbrains/kotlin/fir/allopen/FirAllOpenDiagnosticTestGenerated.java +++ b/compiler/fir/plugins/allopen-plugin/tests/org/jetbrains/kotlin/fir/allopen/FirAllOpenDiagnosticTestGenerated.java @@ -28,6 +28,24 @@ public class FirAllOpenDiagnosticTestGenerated extends AbstractFirAllOpenDiagnos KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/plugins/allopen-plugin/testData"), Pattern.compile("^(.+)\\.kt$"), null, true); } + @TestMetadata("compiler/fir/plugins/allopen-plugin/testData/classGen") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ClassGen extends AbstractFirAllOpenDiagnosticTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInClassGen() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/plugins/allopen-plugin/testData/classGen"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + runTest("compiler/fir/plugins/allopen-plugin/testData/classGen/simple.kt"); + } + } + @TestMetadata("compiler/fir/plugins/allopen-plugin/testData/status") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)