[FIR-PLUGIN] Add dummy implementation for member generator extension
This commit is contained in:
-8
@@ -1,8 +0,0 @@
|
||||
/*
|
||||
* 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.plugin
|
||||
|
||||
annotation class WithClass
|
||||
+4
-1
@@ -5,4 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.plugin
|
||||
|
||||
annotation class AllOpen
|
||||
annotation class AllOpen
|
||||
annotation class WithGenerated
|
||||
annotation class WithHello
|
||||
annotation class WithNestedFoo
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* 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.plugin
|
||||
|
||||
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.FirEffectiveVisibilityImpl
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
|
||||
import org.jetbrains.kotlin.fir.extensions.FirClassGenerationExtension
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.has
|
||||
import org.jetbrains.kotlin.fir.resolve.firProvider
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.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 <T> generateClass(
|
||||
containingFile: FirFile,
|
||||
annotatedDeclaration: T
|
||||
): List<GeneratedClass> where T : FirDeclaration, T : FirAnnotationContainer {
|
||||
if (annotatedDeclaration !is FirRegularClass) return emptyList()
|
||||
val klass = buildRegularClass {
|
||||
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 predicate: DeclarationPredicate = has(FqName("org.jetbrains.kotlin.fir.plugin.WithClass"))
|
||||
|
||||
override val key: FirPluginKey
|
||||
get() = AllOpenPluginKey
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.plugin
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
|
||||
import org.jetbrains.kotlin.fir.extensions.FirExistingClassModificationExtension
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.and
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.has
|
||||
import org.jetbrains.kotlin.fir.extensions.predicate.under
|
||||
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.GeneratedNestedClass
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class AllOpenMemberGenerator(session: FirSession) : FirExistingClassModificationExtension(session) {
|
||||
override fun generateNestedClasses(
|
||||
annotatedDeclaration: FirDeclaration,
|
||||
owners: List<FirAnnotatedDeclaration>
|
||||
): List<GeneratedDeclaration<FirRegularClass>> {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
override fun generateMembersForNestedClasses(generatedNestedClass: GeneratedNestedClass): List<FirDeclaration> {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
override fun generateMembers(
|
||||
annotatedDeclaration: FirDeclaration,
|
||||
owners: List<FirAnnotatedDeclaration>
|
||||
): List<GeneratedDeclaration<*>> {
|
||||
if (annotatedDeclaration !is FirProperty) return emptyList()
|
||||
val owner = owners.last() as? FirRegularClass ?: return emptyList()
|
||||
val propertyName = annotatedDeclaration.name.identifier
|
||||
val function = buildSimpleFunction {
|
||||
session = this@AllOpenMemberGenerator.session
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
origin = FirDeclarationOrigin.Plugin(key)
|
||||
returnTypeRef = annotatedDeclaration.returnTypeRef
|
||||
status = annotatedDeclaration.status
|
||||
name = Name.identifier("hello${propertyName.capitalize()}")
|
||||
symbol = FirNamedFunctionSymbol(CallableId(owner.classId, name), isFakeOverride = false)
|
||||
}
|
||||
return listOf(GeneratedDeclaration(function, owner))
|
||||
}
|
||||
|
||||
override val predicate: DeclarationPredicate = under("WithGenerated".fqn()) and has("WithHello".fqn())
|
||||
|
||||
override val key: FirPluginKey
|
||||
get() = AllOpenPluginKey
|
||||
}
|
||||
|
||||
private fun String.fqn(): FqName = FqName("org.jetbrains.kotlin.fir.plugin.$this")
|
||||
+1
-1
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar
|
||||
class FirAllOpenComponentRegistrar : FirExtensionRegistrar() {
|
||||
override fun ExtensionRegistrarContext.configurePlugin() {
|
||||
+::AllOpenStatusTransformer
|
||||
+::AllOpenClassGenerator
|
||||
+::AllOpenMemberGenerator
|
||||
+::AllOpenAdditionalCheckers
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import org.jetbrains.kotlin.fir.plugin.WithClass
|
||||
import org.jetbrains.kotlin.fir.plugin.AllOpen
|
||||
|
||||
@WithClass
|
||||
class A {
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
FILE: simple.kt
|
||||
@R|org/jetbrains/kotlin/fir/plugin/WithClass|() public final class A : R|kotlin/Any| {
|
||||
public constructor(): R|A| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import org.jetbrains.kotlin.fir.plugin.WithHello
|
||||
import org.jetbrains.kotlin.fir.plugin.WithGenerated
|
||||
import org.jetbrains.kotlin.fir.plugin.AllOpen
|
||||
|
||||
@WithGenerated
|
||||
class A {
|
||||
@WithHello
|
||||
val x: Int = 1
|
||||
}
|
||||
|
||||
class B {
|
||||
@WithHello
|
||||
val x: Int = 1
|
||||
}
|
||||
|
||||
fun test_1(a: A) {
|
||||
a.helloX() // should be OK
|
||||
}
|
||||
|
||||
fun test_2(b: B) {
|
||||
b.<!UNRESOLVED_REFERENCE!>helloX<!>() // should be an error
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
FILE: functionForProperty.kt
|
||||
@R|org/jetbrains/kotlin/fir/plugin/WithGenerated|() public final class A : R|kotlin/Any| {
|
||||
public constructor(): R|A| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
@R|org/jetbrains/kotlin/fir/plugin/WithHello|() public final val x: R|kotlin/Int| = Int(1)
|
||||
public get(): R|kotlin/Int|
|
||||
|
||||
public final fun helloX(): R|kotlin/Int|
|
||||
|
||||
}
|
||||
public final class B : R|kotlin/Any| {
|
||||
public constructor(): R|B| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
@R|org/jetbrains/kotlin/fir/plugin/WithHello|() public final val x: R|kotlin/Int| = Int(1)
|
||||
public get(): R|kotlin/Int|
|
||||
|
||||
}
|
||||
public final fun test_1(a: R|A|): R|kotlin/Unit| {
|
||||
R|<local>/a|.R|/A.helloX|()
|
||||
}
|
||||
public final fun test_2(b: R|B|): R|kotlin/Unit| {
|
||||
R|<local>/b|.<Unresolved name: helloX>#()
|
||||
}
|
||||
+7
-7
@@ -46,21 +46,21 @@ public class FirAllOpenDiagnosticTestGenerated extends AbstractFirAllOpenDiagnos
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("plugins/fir/fir-plugin-prototype/testData/classGen")
|
||||
@TestMetadata("plugins/fir/fir-plugin-prototype/testData/memberGen")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ClassGen extends AbstractFirAllOpenDiagnosticTest {
|
||||
public static class MemberGen 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("plugins/fir/fir-plugin-prototype/testData/classGen"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
public void testAllFilesPresentInMemberGen() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/fir/fir-plugin-prototype/testData/memberGen"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("plugins/fir/fir-plugin-prototype/testData/classGen/simple.kt");
|
||||
@TestMetadata("functionForProperty.kt")
|
||||
public void testFunctionForProperty() throws Exception {
|
||||
runTest("plugins/fir/fir-plugin-prototype/testData/memberGen/functionForProperty.kt");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user