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
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.allopen
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
@@ -28,10 +27,14 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.types.TypeUtils
class AllOpenDeclarationAttributeAltererExtension(val allOpenAnnotationFqNames: List<String>) : DeclarationAttributeAltererExtension {
private companion object {
private val INHERITED_FQNAME = "java.lang.annotation.Inherited"
}
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,
@@ -40,10 +43,8 @@ class AllOpenDeclarationAttributeAltererExtension(val allOpenAnnotationFqNames:
currentModality: Modality,
bindingContext: BindingContext
): Modality? {
// We alter only 'final' modality
when (currentModality) {
Modality.OPEN, Modality.ABSTRACT, Modality.SEALED -> return null
else -> {}
if (currentModality != Modality.FINAL) {
return null
}
// Explicit final
@@ -52,31 +53,34 @@ class AllOpenDeclarationAttributeAltererExtension(val allOpenAnnotationFqNames:
}
val descriptor = declaration ?: containingDeclaration ?: return null
if (descriptor.hasAllOpenAnnotation()) return Modality.OPEN
if (descriptor.hasAllOpenAnnotation(modifierListOwner)) return Modality.OPEN
return null
}
private fun DeclarationDescriptor.hasAllOpenAnnotation(): Boolean {
if (annotations.any { it.isAllOpenAnnotation() }) return true
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() }) return true
if (superTypeDescriptor.annotations.any { it.isAllOpenAnnotation(modifierListOwner) }) return true
}
}
return false
}
private fun AnnotationDescriptor.isAllOpenAnnotation(allowMetaAnnotations: Boolean = true): Boolean {
private fun AnnotationDescriptor.isAllOpenAnnotation(
modifierListOwner: KtModifierListOwner,
allowMetaAnnotations: Boolean = true
): Boolean {
val annotationType = type.constructor.declarationDescriptor ?: return false
if (annotationType.fqNameSafe.asString() in allOpenAnnotationFqNames) return true
if (annotationType.fqNameSafe.asString() in getAllOpenAnnotationFqNames(modifierListOwner)) return true
if (allowMetaAnnotations) {
for (metaAnnotation in annotationType.annotations) {
if (metaAnnotation.isAllOpenAnnotation(allowMetaAnnotations = false)) {
if (metaAnnotation.isAllOpenAnnotation(modifierListOwner, allowMetaAnnotations = false)) {
return true
}
}
@@ -34,9 +34,11 @@ 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 = "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) {
@@ -54,6 +56,6 @@ class AllOpenComponentRegistrar : ComponentRegistrar {
val annotations = configuration.get(AllOpenConfigurationKeys.ANNOTATION) ?: return
if (annotations.isEmpty()) return
DeclarationAttributeAltererExtension.registerExtension(project, AllOpenDeclarationAttributeAltererExtension(annotations))
DeclarationAttributeAltererExtension.registerExtension(project, CliAllOpenDeclarationAttributeAltererExtension(annotations))
}
}
@@ -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
}
@@ -13,9 +13,12 @@ public final class Test1 {
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 @org.jetbrains.annotations.NotNull method getProp2(): 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
}
@@ -20,9 +20,9 @@ interface Intf {
}
open class IntfImpl : Intf {
fun intfImplMethod_ShouldBeFinal() {}
fun intfImplMethod_ShouldBeOpen() {}
}
class IntfImpl2_ShouldBeFinalBecauseIntfIsAnInterface : IntfImpl() {
fun intfImpl2Method_ShouldBeFinal() {}
class IntfImpl2_ShouldBeOpen : IntfImpl() {
fun intfImpl2Method_ShouldBeOpen() {}
}
@@ -39,12 +39,12 @@ public interface Intf {
@kotlin.Metadata
public class IntfImpl {
public method <init>(): void
public final method intfImplMethod_ShouldBeFinal(): void
public method intfImplMethod_ShouldBeOpen(): void
public method intfMethod(): void
}
@kotlin.Metadata
public final class IntfImpl2_ShouldBeFinalBecauseIntfIsAnInterface {
public class IntfImpl2_ShouldBeOpen {
public method <init>(): void
public final method intfImpl2Method_ShouldBeFinal(): void
public method intfImpl2Method_ShouldBeOpen(): void
}
@@ -0,0 +1,23 @@
<?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" />
<orderEntry type="module" module-name="allopen-cli" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="library" name="gradle-and-groovy-plugin" level="project" />
<orderEntry type="module" module-name="idea" />
<orderEntry type="module" module-name="idea-maven" />
<orderEntry type="library" name="maven" level="project" />
<orderEntry type="module" module-name="cli-common" />
<orderEntry type="module" module-name="idea-jps-common" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="annotation-based-compiler-plugins-ide-support" />
</component>
</module>
@@ -0,0 +1,29 @@
/*
* 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.ide
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractGradleImportHandler
import org.jetbrains.kotlin.utils.PathUtil
class AllOpenGradleProjectImportHandler : AbstractGradleImportHandler() {
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
override val pluginName = "allopen"
override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name
override val dataStorageTaskName = "allOpenDataStorageTask"
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
}
@@ -0,0 +1,56 @@
/*
* 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.ide
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
private companion object {
val ANNOTATION_PARAMETER_PREFIX = "all-open:${AllOpenCommandLineProcessor.ANNOTATION_OPTION.name}="
private val SPRING_ALLOPEN_ANNOTATIONS = listOf(
"org.springframework.stereotype.Component",
"org.springframework.transaction.annotation.Transactional",
"org.springframework.scheduling.annotation.Async",
"org.springframework.cache.annotation.Cacheable"
)
}
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
override val pluginName = "allopen"
override val annotationOptionName = AllOpenCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-allopen"
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? {
if ("all-open" !in enabledCompilerPlugins && "spring" !in enabledCompilerPlugins) {
return null
}
val annotations = mutableListOf<String>()
if ("spring" in enabledCompilerPlugins) {
annotations.addAll(SPRING_ALLOPEN_ANNOTATIONS)
}
annotations.addAll(compilerPluginOptions.mapNotNull { text ->
if (!text.startsWith(ANNOTATION_PARAMETER_PREFIX)) return@mapNotNull null
text.substring(ANNOTATION_PARAMETER_PREFIX.length)
})
return annotations
}
}
@@ -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.ide
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import org.jetbrains.kotlin.allopen.AbstractAllOpenDeclarationAttributeAltererExtension
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.PLUGIN_ID
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.ANNOTATION_OPTION
import java.util.*
class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : AbstractAllOpenDeclarationAttributeAltererExtension() {
private companion object {
val ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.name}="
}
private val cache: CachedValue<WeakHashMap<Module, List<String>>> = cachedValue(project) {
CachedValueProvider.Result.create(WeakHashMap<Module, List<String>>(), ProjectRootModificationTracker.getInstance(project))
}
override fun getAllOpenAnnotationFqNames(modifierListOwner: KtModifierListOwner): List<String> {
val project = modifierListOwner.project
val virtualFile = modifierListOwner.containingFile?.virtualFile ?: return emptyList()
val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(virtualFile) ?: return emptyList()
return cache.value.getOrPut(module) {
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
val commonArgs = kotlinFacet.configuration.settings.compilerInfo.commonCompilerArguments ?: return@getOrPut emptyList()
commonArgs.pluginOptions?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
?: emptyList()
}
}
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
return CachedValuesManager.getManager(project).createCachedValue(result, false)
}
}
@@ -1,13 +0,0 @@
annotation class AllOpen
@AllOpen
final class Test1
@AllOpen
class Test2 {
fun method1() {}
val prop1: String = ""
final fun method2() {}
val prop2: String = ""
}