AllOpen: Add IDE integration

This commit is contained in:
Yan Zhulanow
2016-12-01 17:43:00 +03:00
committed by Yan Zhulanow
parent 4d638c2cfd
commit 6abde4223b
53 changed files with 677 additions and 175 deletions
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="frontend" />
</component>
</module>
@@ -0,0 +1,91 @@
/*
* Copyright 2010-2016 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.allopen
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.types.TypeUtils
class CliAllOpenDeclarationAttributeAltererExtension(
private val allOpenAnnotationFqNames: List<String>
) : AbstractAllOpenDeclarationAttributeAltererExtension() {
override fun getAllOpenAnnotationFqNames(modifierListOwner: KtModifierListOwner) = allOpenAnnotationFqNames
}
abstract class AbstractAllOpenDeclarationAttributeAltererExtension : DeclarationAttributeAltererExtension {
abstract fun getAllOpenAnnotationFqNames(modifierListOwner: KtModifierListOwner): List<String>
override fun refineDeclarationModality(
modifierListOwner: KtModifierListOwner,
declaration: DeclarationDescriptor?,
containingDeclaration: DeclarationDescriptor?,
currentModality: Modality,
bindingContext: BindingContext
): Modality? {
if (currentModality != Modality.FINAL) {
return null
}
// Explicit final
if (modifierListOwner.hasModifier(KtTokens.FINAL_KEYWORD)) {
return null
}
val descriptor = declaration ?: containingDeclaration ?: return null
if (descriptor.hasAllOpenAnnotation(modifierListOwner)) return Modality.OPEN
return null
}
private fun DeclarationDescriptor.hasAllOpenAnnotation(modifierListOwner: KtModifierListOwner): Boolean {
if (annotations.any { it.isAllOpenAnnotation(modifierListOwner) }) return true
if (this is ClassDescriptor) {
for (superType in TypeUtils.getAllSupertypes(defaultType)) {
val superTypeDescriptor = superType.constructor.declarationDescriptor as? ClassDescriptor ?: continue
if (superTypeDescriptor.annotations.any { it.isAllOpenAnnotation(modifierListOwner) }) return true
}
}
return false
}
private fun AnnotationDescriptor.isAllOpenAnnotation(
modifierListOwner: KtModifierListOwner,
allowMetaAnnotations: Boolean = true
): Boolean {
val annotationType = type.constructor.declarationDescriptor ?: return false
if (annotationType.fqNameSafe.asString() in getAllOpenAnnotationFqNames(modifierListOwner)) return true
if (allowMetaAnnotations) {
for (metaAnnotation in annotationType.annotations) {
if (metaAnnotation.isAllOpenAnnotation(modifierListOwner, allowMetaAnnotations = false)) {
return true
}
}
}
return false
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2016 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.allopen
import com.intellij.mock.MockProject
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
object AllOpenConfigurationKeys {
val ANNOTATION: CompilerConfigurationKey<List<String>> =
CompilerConfigurationKey.create("annotation qualified name")
}
class AllOpenCommandLineProcessor : CommandLineProcessor {
companion object {
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
required = false, allowMultipleOccurrences = true)
val PLUGIN_ID = "org.jetbrains.kotlin.allopen"
}
override val pluginId = PLUGIN_ID
override val pluginOptions = listOf(ANNOTATION_OPTION)
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
ANNOTATION_OPTION -> {
val paths = configuration.getList(AllOpenConfigurationKeys.ANNOTATION).toMutableList()
paths.add(value)
configuration.put(AllOpenConfigurationKeys.ANNOTATION, paths)
}
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
}
}
class AllOpenComponentRegistrar : ComponentRegistrar {
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val annotations = configuration.get(AllOpenConfigurationKeys.ANNOTATION) ?: return
if (annotations.isEmpty()) return
DeclarationAttributeAltererExtension.registerExtension(project, CliAllOpenDeclarationAttributeAltererExtension(annotations))
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
@@ -0,0 +1 @@
org.jetbrains.kotlin.allopen.AllOpenComponentRegistrar
@@ -0,0 +1,19 @@
@AllOpen
annotation class AllOpen
@AllOpen
interface Intf
@AllOpen
object Obj
@AllOpen
enum class Enum
class MyClass {
@AllOpen
fun method() {}
@field:AllOpen @get:AllOpen @set:AllOpen
var prop: String = ""
}
@@ -0,0 +1,35 @@
@AllOpen
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@AllOpen
@kotlin.Metadata
public enum class Enum {
private synthetic final static field $VALUES: Enum[]
static method <clinit>(): void
protected method <init>(p0: java.lang.String, p1: int): void
public static method valueOf(p0: java.lang.String): Enum
public static method values(): Enum[]
}
@AllOpen
@kotlin.Metadata
public interface Intf
@kotlin.Metadata
public final class MyClass {
private @AllOpen @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public final @AllOpen @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public final @AllOpen method method(): void
public final @AllOpen method setProp(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
}
@AllOpen
@kotlin.Metadata
public final class Obj {
public final static field INSTANCE: Obj
static method <clinit>(): void
private method <init>(): void
}
@@ -0,0 +1,16 @@
annotation class AllOpen
@AllOpen
open class Test1
@AllOpen
open class Test2 {
open fun method() {}
val prop: String = ""
}
@AllOpen
class Test3 {
fun method() {}
open val prop: String = ""
}
@@ -0,0 +1,27 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@AllOpen
@kotlin.Metadata
public class Test1 {
public method <init>(): void
}
@AllOpen
@kotlin.Metadata
public class Test2 {
private final @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public method method(): void
}
@AllOpen
@kotlin.Metadata
public class Test3 {
private final @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public method method(): void
}
@@ -0,0 +1,18 @@
annotation class AllOpen
@AllOpen
final class Test1
@AllOpen
class Test2 {
fun method1() {}
val prop1: String = ""
final fun method2() {}
final val prop2: String = ""
final var prop3: String = ""
// Modifier 'final' is not applicable to 'setter'
// var prop4: String = ""
// final set
}
@@ -0,0 +1,24 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@AllOpen
@kotlin.Metadata
public final class Test1 {
public method <init>(): void
}
@AllOpen
@kotlin.Metadata
public class Test2 {
private final @org.jetbrains.annotations.NotNull field prop1: java.lang.String
private final @org.jetbrains.annotations.NotNull field prop2: java.lang.String
private @org.jetbrains.annotations.NotNull field prop3: java.lang.String
public method <init>(): void
public @org.jetbrains.annotations.NotNull method getProp1(): java.lang.String
public final @org.jetbrains.annotations.NotNull method getProp2(): java.lang.String
public final @org.jetbrains.annotations.NotNull method getProp3(): java.lang.String
public method method1(): void
public final method method2(): void
public final method setProp3(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
}
@@ -0,0 +1,33 @@
annotation class AllOpen
@AllOpen
annotation class MyComponent
@MyComponent // Double-transitive annotations is not supported
annotation class OtherComponent
class TestWithoutAnnotations_ShouldBeFinal
@AllOpen
class TestAllOpen_ShouldBeOpen
@MyComponent
class TestMyComponent_ShouldBeOpen
@OtherComponent
class TestOtherComponent_ShouldBeFinal
@MyComponent
abstract class MyComponentBase
class MyComponentImpl_ShouldBeOpen : MyComponentBase() {
fun method() {}
}
final class MyComponentImpl2_ShouldBeFinal : MyComponentBase() {
fun method() {}
}
class MyComponentImpl3_ShouldBeOpen : MyComponentBase() {
final fun method_ShouldBeFinal() {}
}
@@ -0,0 +1,60 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@AllOpen
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class MyComponent
@MyComponent
@kotlin.Metadata
public abstract class MyComponentBase {
public method <init>(): void
}
@kotlin.Metadata
public final class MyComponentImpl2_ShouldBeFinal {
public method <init>(): void
public method method(): void
}
@kotlin.Metadata
public class MyComponentImpl3_ShouldBeOpen {
public method <init>(): void
public final method method_ShouldBeFinal(): void
}
@kotlin.Metadata
public class MyComponentImpl_ShouldBeOpen {
public method <init>(): void
public method method(): void
}
@MyComponent
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class OtherComponent
@AllOpen
@kotlin.Metadata
public class TestAllOpen_ShouldBeOpen {
public method <init>(): void
}
@MyComponent
@kotlin.Metadata
public class TestMyComponent_ShouldBeOpen {
public method <init>(): void
}
@OtherComponent
@kotlin.Metadata
public final class TestOtherComponent_ShouldBeFinal {
public method <init>(): void
}
@kotlin.Metadata
public final class TestWithoutAnnotations_ShouldBeFinal {
public method <init>(): void
}
@@ -0,0 +1,14 @@
annotation class AllOpen
@AllOpen
class Test {
fun testMethod() {}
class Nested {
fun nestedMethod() {}
}
inner class Inner {
fun innerMethod() {}
}
}
@@ -0,0 +1,27 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@kotlin.Metadata
public final class Test$Inner {
synthetic final field this$0: Test
inner class Test$Inner
public method <init>(p0: Test): void
public final method innerMethod(): void
}
@kotlin.Metadata
public final class Test$Nested {
inner class Test$Nested
public method <init>(): void
public final method nestedMethod(): void
}
@AllOpen
@kotlin.Metadata
public class Test {
inner class Test$Inner
inner class Test$Nested
public method <init>(): void
public method testMethod(): void
}
@@ -0,0 +1,4 @@
class Test {
val prop: String = ""
fun method() {}
}
@@ -0,0 +1,7 @@
@kotlin.Metadata
public final class Test {
private final @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public final @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public final method method(): void
}
@@ -0,0 +1,14 @@
annotation class AllOpen
@AllOpen
sealed class Sealed {
class C1 : Sealed() {}
class C2 : Sealed() {}
}
sealed class Sealed2 {
@AllOpen
class C1 : Sealed2() {}
class C2 : Sealed2() {}
}
@@ -0,0 +1,45 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@kotlin.Metadata
public class Sealed$C1 {
inner class Sealed$C1
public method <init>(): void
}
@kotlin.Metadata
public class Sealed$C2 {
inner class Sealed$C2
public method <init>(): void
}
@AllOpen
@kotlin.Metadata
public abstract class Sealed {
inner class Sealed$C1
inner class Sealed$C2
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
}
@AllOpen
@kotlin.Metadata
public class Sealed2$C1 {
inner class Sealed2$C1
public method <init>(): void
}
@kotlin.Metadata
public final class Sealed2$C2 {
inner class Sealed2$C2
public method <init>(): void
}
@kotlin.Metadata
public abstract class Sealed2 {
inner class Sealed2$C1
inner class Sealed2$C2
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
}
@@ -0,0 +1,9 @@
annotation class AllOpen
annotation class AllOpen2
@AllOpen
@AllOpen2
class Test {
val prop: String = ""
fun method() {}
}
@@ -0,0 +1,17 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen2
@AllOpen
@AllOpen2
@kotlin.Metadata
public class Test {
private final @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public method method(): void
}
@@ -0,0 +1,7 @@
annotation class AllOpen
@AllOpen
class Test {
val prop: String = ""
fun method() {}
}
@@ -0,0 +1,12 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@AllOpen
@kotlin.Metadata
public class Test {
private final @org.jetbrains.annotations.NotNull field prop: java.lang.String
public method <init>(): void
public @org.jetbrains.annotations.NotNull method getProp(): java.lang.String
public method method(): void
}
@@ -0,0 +1,28 @@
annotation class AllOpen
@AllOpen
abstract class Base_ShouldBeOpen {
fun baseMethod() {}
}
open class BaseImpl : Base_ShouldBeOpen() {
fun baseImplMethod_ShouldBeOpen() {}
}
class BaseImpl2_ShouldBeOpen : BaseImpl() {
fun baseImpl2Method_ShouldBeOpen() {}
val baseImpl2Property_ShouldBeOpen = ""
}
@AllOpen
interface Intf {
fun intfMethod() {}
}
open class IntfImpl : Intf {
fun intfImplMethod_ShouldBeOpen() {}
}
class IntfImpl2_ShouldBeOpen : IntfImpl() {
fun intfImpl2Method_ShouldBeOpen() {}
}
@@ -0,0 +1,50 @@
@java.lang.annotation.Retention
@kotlin.Metadata
public annotation class AllOpen
@kotlin.Metadata
public class BaseImpl {
public method <init>(): void
public method baseImplMethod_ShouldBeOpen(): void
}
@kotlin.Metadata
public class BaseImpl2_ShouldBeOpen {
private final @org.jetbrains.annotations.NotNull field baseImpl2Property_ShouldBeOpen: java.lang.String
public method <init>(): void
public method baseImpl2Method_ShouldBeOpen(): void
public @org.jetbrains.annotations.NotNull method getBaseImpl2Property_ShouldBeOpen(): java.lang.String
}
@AllOpen
@kotlin.Metadata
public abstract class Base_ShouldBeOpen {
public method <init>(): void
public method baseMethod(): void
}
@kotlin.Metadata
public final class Intf$DefaultImpls {
inner class Intf$DefaultImpls
public static method intfMethod(p0: Intf): void
}
@AllOpen
@kotlin.Metadata
public interface Intf {
inner class Intf$DefaultImpls
public abstract method intfMethod(): void
}
@kotlin.Metadata
public class IntfImpl {
public method <init>(): void
public method intfImplMethod_ShouldBeOpen(): void
public method intfMethod(): void
}
@kotlin.Metadata
public class IntfImpl2_ShouldBeOpen {
public method <init>(): void
public method intfImpl2Method_ShouldBeOpen(): void
}