AllOpen: Add IDE integration
This commit is contained in:
committed by
Yan Zhulanow
parent
4d638c2cfd
commit
6abde4223b
+20
-16
@@ -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
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -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
|
||||
}
|
||||
+4
-1
@@ -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
|
||||
}
|
||||
+3
-3
@@ -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() {}
|
||||
}
|
||||
+3
-3
@@ -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 = ""
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?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="idea" />
|
||||
<orderEntry type="module" module-name="idea-maven" />
|
||||
<orderEntry type="module" module-name="cli-common" />
|
||||
<orderEntry type="library" name="idea-full" level="project" />
|
||||
<orderEntry type="library" name="maven" level="project" />
|
||||
<orderEntry type="library" name="gradle-and-groovy-plugin" level="project" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="idea-jps-common" />
|
||||
</component>
|
||||
</module>
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.annotation.plugin.ide
|
||||
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.project.ModuleData
|
||||
import com.intellij.openapi.externalSystem.model.task.TaskData
|
||||
import org.jetbrains.kotlin.idea.configuration.GradleProjectImportHandler
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractGradleImportHandler : GradleProjectImportHandler {
|
||||
private companion object {
|
||||
private val TASK_DESCRIPTION_REGEX = "Supported annotations: (.*?); Compiler plugin classpath: (.*)".toRegex()
|
||||
}
|
||||
|
||||
abstract val compilerPluginId: String
|
||||
abstract val pluginName: String
|
||||
abstract val annotationOptionName: String
|
||||
abstract val dataStorageTaskName: String
|
||||
abstract val pluginJarFileFromIdea: File
|
||||
|
||||
override fun invoke(facet: KotlinFacet, sourceSetNode: DataNode<GradleSourceSetData>) {
|
||||
modifyCompilerArgumentsForPlugin(facet, getPluginSetup(sourceSetNode),
|
||||
compilerPluginId = compilerPluginId,
|
||||
pluginName = pluginName,
|
||||
annotationOptionName = annotationOptionName)
|
||||
}
|
||||
|
||||
private fun getPluginSetup(
|
||||
sourceSetNode: DataNode<GradleSourceSetData>
|
||||
): AnnotationBasedCompilerPluginSetup? {
|
||||
val moduleNode = sourceSetNode.parent ?: return null
|
||||
if (moduleNode.data !is ModuleData) return null
|
||||
val dataStorageTaskData = moduleNode.children.firstOrNull {
|
||||
val data = it.data as? TaskData ?: return@firstOrNull false
|
||||
data.name == dataStorageTaskName
|
||||
}?.data as? TaskData ?: return null
|
||||
|
||||
val dataStorageTaskDescription = dataStorageTaskData.description ?: return null
|
||||
val (annotationFqNamesList, classpathList) = TASK_DESCRIPTION_REGEX.matchEntire(
|
||||
dataStorageTaskDescription)?.groupValues?.drop(1) ?: return null
|
||||
|
||||
val annotationFqNames = annotationFqNamesList.split(',')
|
||||
|
||||
// For now we can't use plugins from Gradle cause they're shaded. So we use ones from the IDEA plugin
|
||||
val classpath = listOf(pluginJarFileFromIdea.absolutePath)
|
||||
|
||||
return AnnotationBasedCompilerPluginSetup(annotationFqNames, classpath)
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.annotation.plugin.ide
|
||||
|
||||
import org.jdom.Element
|
||||
import org.jdom.Text
|
||||
import org.jetbrains.idea.maven.project.MavenProject
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.kotlin.idea.maven.MavenProjectImportHandler
|
||||
import org.jetbrains.kotlin.idea.maven.KotlinMavenImporter.Companion.KOTLIN_PLUGIN_GROUP_ID
|
||||
import org.jetbrains.kotlin.idea.maven.KotlinMavenImporter.Companion.KOTLIN_PLUGIN_ARTIFACT_ID
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractMavenImportHandler : MavenProjectImportHandler {
|
||||
abstract val compilerPluginId: String
|
||||
abstract val pluginName: String
|
||||
abstract val annotationOptionName: String
|
||||
abstract val mavenPluginArtifactName: String
|
||||
|
||||
override fun invoke(facet: KotlinFacet, mavenProject: MavenProject) {
|
||||
modifyCompilerArgumentsForPlugin(facet, getPluginSetup(mavenProject),
|
||||
compilerPluginId = compilerPluginId,
|
||||
pluginName = pluginName,
|
||||
annotationOptionName = annotationOptionName)
|
||||
}
|
||||
|
||||
abstract fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>?
|
||||
|
||||
private fun getPluginSetup(mavenProject: MavenProject): AnnotationBasedCompilerPluginSetup? {
|
||||
val kotlinPlugin = mavenProject.plugins.firstOrNull {
|
||||
it.groupId == KOTLIN_PLUGIN_GROUP_ID && it.artifactId == KOTLIN_PLUGIN_ARTIFACT_ID
|
||||
} ?: return null
|
||||
|
||||
val runtimeJarFile = mavenProject.dependencies
|
||||
.firstOrNull { it.groupId == KOTLIN_PLUGIN_GROUP_ID &&
|
||||
(it.artifactId == "kotlin-runtime" || it.artifactId == "kotlin-stdlib") }
|
||||
?.file ?: return null
|
||||
val runtimeVersion = runtimeJarFile.parentFile.name
|
||||
|
||||
val mavenCompilerPluginJar = File(runtimeJarFile.parentFile.parentFile.parentFile,
|
||||
"$mavenPluginArtifactName/$runtimeVersion/$mavenPluginArtifactName-$runtimeVersion.jar")
|
||||
|
||||
val configuration = kotlinPlugin.configurationElement ?: return null
|
||||
|
||||
val enabledCompilerPlugins = configuration.getElement("compilerPlugins")
|
||||
?.getElements()
|
||||
?.flatMap { plugin -> plugin.content.mapNotNull { (it as? Text)?.text } }
|
||||
?: emptyList()
|
||||
|
||||
val compilerPluginOptions = configuration.getElement("pluginOptions")
|
||||
?.getElements()
|
||||
?.flatMap { it.content }
|
||||
?.mapTo(mutableListOf()) { (it as Text).text }
|
||||
?: mutableListOf<String>()
|
||||
|
||||
val annotationFqNames = getAnnotations(enabledCompilerPlugins, compilerPluginOptions) ?: return null
|
||||
return AnnotationBasedCompilerPluginSetup(annotationFqNames, listOf(mavenCompilerPluginJar.absolutePath))
|
||||
}
|
||||
|
||||
private fun Element.getElement(name: String) = content.firstOrNull { it is Element && it.name == name } as? Element
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun Element.getElements() = content.filterIsInstance<Element>()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.annotation.plugin.ide
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
|
||||
internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List<String>, val classpath: List<String>)
|
||||
|
||||
internal fun modifyCompilerArgumentsForPlugin(
|
||||
facet: KotlinFacet,
|
||||
setup: AnnotationBasedCompilerPluginSetup?,
|
||||
compilerPluginId: String,
|
||||
pluginName: String,
|
||||
annotationOptionName: String
|
||||
) {
|
||||
val compileInfo = facet.configuration.settings.compilerInfo
|
||||
|
||||
// investigate why copyBean() sometimes throws exceptions
|
||||
val commonArguments = compileInfo.commonCompilerArguments ?: CommonCompilerArguments.DummyImpl()
|
||||
|
||||
/** See [CommonCompilerArguments.PLUGIN_OPTION_FORMAT] **/
|
||||
val annotationOptions = setup?.annotationFqNames?.map { "plugin:$compilerPluginId:$annotationOptionName=$it" } ?: emptyList()
|
||||
|
||||
val oldPluginOptions = (commonArguments.pluginOptions ?: emptyArray()).filterTo(mutableListOf()) { !it.startsWith("plugin:$compilerPluginId:") }
|
||||
val newPluginOptions = oldPluginOptions + annotationOptions
|
||||
|
||||
val oldPluginClasspaths = (commonArguments.pluginClasspaths ?: emptyArray()).filterTo(mutableListOf()) {
|
||||
!it.substringAfterLast('/', missingDelimiterValue = "").matches("(kotlin-)?$pluginName-.*\\.jar".toRegex())
|
||||
}
|
||||
|
||||
val newPluginClasspaths = oldPluginClasspaths + (setup?.classpath ?: emptyList())
|
||||
|
||||
commonArguments.pluginOptions = newPluginOptions.toTypedArray()
|
||||
commonArguments.pluginClasspaths = newPluginClasspaths.toTypedArray()
|
||||
|
||||
compileInfo.commonCompilerArguments = commonArguments
|
||||
}
|
||||
@@ -37,6 +37,6 @@
|
||||
<orderEntry type="module" module-name="build-common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="jps-tests" scope="TEST" />
|
||||
<orderEntry type="module" module-name="kapt3" scope="TEST" />
|
||||
<orderEntry type="module" module-name="allopen" scope="TEST" />
|
||||
<orderEntry type="module" module-name="allopen-cli" scope="TEST" />
|
||||
</component>
|
||||
</module>
|
||||
+2
-2
@@ -25,8 +25,8 @@ abstract class AbstractBytecodeListingTestForAllOpen : AbstractBytecodeListingTe
|
||||
val ALLOPEN_ANNOTATIONS = listOf("AllOpen", "AllOpen2", "test.AllOpen")
|
||||
}
|
||||
|
||||
override fun setUpEnvironment(environment: KotlinCoreEnvironment) {
|
||||
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
|
||||
DeclarationAttributeAltererExtension.registerExtension(
|
||||
environment.project, AllOpenDeclarationAttributeAltererExtension(ALLOPEN_ANNOTATIONS))
|
||||
environment.project, CliAllOpenDeclarationAttributeAltererExtension(ALLOPEN_ANNOTATIONS))
|
||||
}
|
||||
}
|
||||
+13
-12
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.allopen;
|
||||
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;
|
||||
|
||||
@@ -27,71 +28,71 @@ 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/allopen/testData/bytecodeListing")
|
||||
@TestMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class BytecodeListingTestForAllOpenGenerated extends AbstractBytecodeListingTestForAllOpen {
|
||||
public void testAllFilesPresentInBytecodeListing() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/allopen/testData/bytecodeListing"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/allopen/allopen-cli/testData/bytecodeListing"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("allOpenOnNotClasses.kt")
|
||||
public void testAllOpenOnNotClasses() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/allOpenOnNotClasses.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/allOpenOnNotClasses.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("alreadyOpen.kt")
|
||||
public void testAlreadyOpen() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/alreadyOpen.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/alreadyOpen.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("explicitFinal.kt")
|
||||
public void testExplicitFinal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/explicitFinal.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/explicitFinal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("metaAnnotation.kt")
|
||||
public void testMetaAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/metaAnnotation.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/metaAnnotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nestedInner.kt")
|
||||
public void testNestedInner() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/nestedInner.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/nestedInner.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noAllOpen.kt")
|
||||
public void testNoAllOpen() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/noAllOpen.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/noAllOpen.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("sealed.kt")
|
||||
public void testSealed() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/sealed.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/sealed.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("severalAllOpen.kt")
|
||||
public void testSeveralAllOpen() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/severalAllOpen.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/severalAllOpen.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/simple.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("superClassAnnotation.kt")
|
||||
public void testSuperClassAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/testData/bytecodeListing/superClassAnnotation.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/allopen/allopen-cli/testData/bytecodeListing/superClassAnnotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user