KT-12195 Quickfix @JvmStatic on main() method in an object (#1192)

* Quickfix @JvmStatic on main() method in an object #KT-12195 Fixed

* Fixed #KT-12195
This commit is contained in:
Toshiaki Kameyama
2017-07-19 20:37:12 +09:00
committed by Dmitry Jemerov
parent a8da79a130
commit 366b9d1d79
20 changed files with 220 additions and 5 deletions
@@ -46,7 +46,8 @@ class MainFunctionDetector {
return findMainFunction(declarations) != null
}
fun isMain(function: KtNamedFunction): Boolean {
@JvmOverloads
fun isMain(function: KtNamedFunction, checkJvmStaticAnnotation: Boolean = true): Boolean {
if (function.isLocal) {
return false
}
@@ -61,11 +62,11 @@ class MainFunctionDetector {
}
/* Psi only check for kotlin.jvm.jvmStatic annotation */
if (!function.isTopLevel && !hasAnnotationWithExactNumberOfArguments(function, 0)) {
if (checkJvmStaticAnnotation && !function.isTopLevel && !hasAnnotationWithExactNumberOfArguments(function, 0)) {
return false
}
return isMain(getFunctionDescriptor(function))
return isMain(getFunctionDescriptor(function), checkJvmStaticAnnotation)
}
fun getMainFunction(module: ModuleDescriptor): FunctionDescriptor? = getMainFunction(module, module.getPackage(FqName.ROOT))
@@ -90,7 +91,7 @@ class MainFunctionDetector {
companion object {
fun isMain(descriptor: DeclarationDescriptor): Boolean {
fun isMain(descriptor: DeclarationDescriptor, checkJvmStaticAnnotation: Boolean = true): Boolean {
if (descriptor !is FunctionDescriptor) return false
if (getJVMFunctionName(descriptor) != "main") {
@@ -123,7 +124,7 @@ class MainFunctionDetector {
val containingDeclaration = descriptor.containingDeclaration
return containingDeclaration is ClassDescriptor
&& containingDeclaration.kind.isSingleton
&& descriptor.hasJvmStaticAnnotation()
&& (descriptor.hasJvmStaticAnnotation() || !checkJvmStaticAnnotation)
}
private fun getJVMFunctionName(functionDescriptor: FunctionDescriptor): String {
@@ -0,0 +1,5 @@
object Test {
<spot>@JvmStatic</spot>
fun main(args: Array<String>) {
}
}
@@ -0,0 +1,4 @@
object Test {
fun main(args: Array<String>) {
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds @JvmStatic annotation to main() method in an object.
</body>
</html>
+5
View File
@@ -1288,6 +1288,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddJvmStaticIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveArgumentNameIntention</className>
<category>Kotlin</category>
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2017 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.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.MainFunctionDetector
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.util.addAnnotation
import org.jetbrains.kotlin.idea.util.findAnnotation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtNamedFunction
class AddJvmStaticIntention : SelfTargetingIntention<KtNamedFunction>(
KtNamedFunction::class.java,
"Add '@JvmStatic' annotation"
), LowPriorityAction {
private val annotationFqName = FqName("kotlin.jvm.JvmStatic")
override fun isApplicableTo(element: KtNamedFunction, caretOffset: Int): Boolean {
if (element.findAnnotation(annotationFqName) != null) return false
if (element.isTopLevel) return false
val detector = MainFunctionDetector { function ->
function.resolveToDescriptor() as FunctionDescriptor
}
return detector.isMain(element, false)
}
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
element.addAnnotation(annotationFqName)
}
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddJvmStaticIntention
+8
View File
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
object Test {
@JvmStatic
fun <caret>main(args: Array<String>) {
}
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
private fun test() = object {
fun <caret>main(args: Array<String>) {
}
}
+6
View File
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Test {
fun <caret>main(args: Array<String>) {
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class Test {
companion object {
fun <caret>main(args: Array<String>) {
}
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
class Test {
companion object {
@JvmStatic
fun main(args: Array<String>) {
}
}
}
+6
View File
@@ -0,0 +1,6 @@
// WITH_RUNTIME
object Test {
fun <caret>main(args: Array<String>) {
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
object Test {
@JvmStatic
fun main(args: Array<String>) {
}
}
+4
View File
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun <caret>main(args: Array<String>) {
}
+7
View File
@@ -0,0 +1,7 @@
// WITH_RUNTIME
object Test {
@JvmName("main")
fun <caret>test(args: Array<String>) {
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
object Test {
@JvmStatic
@JvmName("main")
fun <caret>test(args: Array<String>) {
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
object Test {
@JvmName("test")
fun <caret>main(args: Array<String>) {
}
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
object Test {
fun <caret>test(args: Array<String>) {
}
}
@@ -288,6 +288,69 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/addJvmStatic")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddJvmStatic extends AbstractIntentionTest {
public void testAllFilesPresentInAddJvmStatic() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addJvmStatic"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("hasJvmStatic.kt")
public void testHasJvmStatic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/hasJvmStatic.kt");
doTest(fileName);
}
@TestMetadata("inAnonymousObject.kt")
public void testInAnonymousObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/inAnonymousObject.kt");
doTest(fileName);
}
@TestMetadata("inClass.kt")
public void testInClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/inClass.kt");
doTest(fileName);
}
@TestMetadata("inCompanionObject.kt")
public void testInCompanionObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/inCompanionObject.kt");
doTest(fileName);
}
@TestMetadata("inObjedct.kt")
public void testInObjedct() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/inObjedct.kt");
doTest(fileName);
}
@TestMetadata("inTopLevel.kt")
public void testInTopLevel() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/inTopLevel.kt");
doTest(fileName);
}
@TestMetadata("mainJvmName.kt")
public void testMainJvmName() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/mainJvmName.kt");
doTest(fileName);
}
@TestMetadata("notMainJvmName.kt")
public void testNotMainJvmName() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/notMainJvmName.kt");
doTest(fileName);
}
@TestMetadata("notMainMethod.kt")
public void testNotMainMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addJvmStatic/notMainMethod.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addMissingDestructuring")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)