QuickFix for UNCHECKED_CAST

This commit is contained in:
Wojciech Lopata
2013-02-19 22:46:06 +01:00
committed by Evgeny Gerashchenko
parent db99492b9c
commit 1f3739415a
9 changed files with 116 additions and 1 deletions
@@ -62,6 +62,13 @@ public class JetPsiFactory {
return property.getTypeRef();
}
@NotNull
public static PsiElement createStar(Project project) {
PsiElement star = createType(project, "List<*>").findElementAt(5);
assert star != null;
return star;
}
//the pair contains the first and the last elements of a range
public static Pair<PsiElement, PsiElement> createColonAndWhiteSpaces(Project project) {
JetProperty property = createProperty(project, "val x : Int");
@@ -15,6 +15,8 @@ add.modifier.family=Add Modifier
make.class.annotation.class=Make ''{0}'' an annotation class
make.class.annotation.class.family=Make Class an Annotation Class
add.star.projections=Add ''{0}''
change.to.star.projection=Change type argument list to {0}
change.to.star.projection.family=Change to Star Projection
make.element.not.modifier=Make {0} not {1}
remove.modifier=Remove ''{0}'' modifier
remove.modifier.family=Remove Modifier
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2013 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.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.plugin.JetBundle;
public class ChangeToStarProjectionFix extends JetIntentionAction<JetTypeElement> {
public ChangeToStarProjectionFix(@NotNull JetTypeElement element) {
super(element);
}
@NotNull
@Override
public String getText() {
String stars = TypeUtils.getTypeNameAndStarProjectionsString("", element.getTypeArgumentsAsTypes().size());
return JetBundle.message("change.to.star.projection", stars);
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.to.star.projection.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
for (JetTypeReference typeReference : element.getTypeArgumentsAsTypes()) {
if (typeReference != null) {
typeReference.replace(JetPsiFactory.createStar(project));
}
}
}
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
JetBinaryExpressionWithTypeRHS expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpressionWithTypeRHS.class);
if (expression == null) return null;
JetTypeReference typeReference = expression.getRight();
if (typeReference == null) return null;
JetTypeElement typeElement = typeReference.getTypeElement();
return typeElement == null ? null : new ChangeToStarProjectionFix(typeElement);
}
};
}
}
@@ -180,6 +180,7 @@ public class QuickFixes {
factories.put(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION, AddStarProjectionsFix.createFactoryForIsExpression());
factories.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, AddStarProjectionsFix.createFactoryForJavaClass());
factories.put(UNCHECKED_CAST, ChangeToStarProjectionFix.createFactory());
factories.put(INACCESSIBLE_OUTER_CLASS_EXPRESSION, AddModifierFix.createFactory(INNER_KEYWORD, JetClass.class));
@@ -0,0 +1,4 @@
// "Change type argument list to <*>" "true"
public fun foo(a: Any) {
a as List<*>
}
@@ -0,0 +1,4 @@
// "Change type argument list to <*, *>" "true"
public fun foo(a: Any?) {
a as Map<*, *>?
}
@@ -0,0 +1,4 @@
// "Change type argument list to <*>" "true"
public fun foo(a: Any) {
a as List<Int><caret>
}
@@ -0,0 +1,4 @@
// "Change type argument list to <*, *>" "true"
public fun foo(a: Any?) {
a as Map<*, Int>?<caret>
}
@@ -146,7 +146,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
@TestMetadata("idea/testData/quickfix/addStarProjections")
@InnerTestClasses({AddStarProjections.JavaClass.class, AddStarProjections.When.class})
@InnerTestClasses({AddStarProjections.Cast.class, AddStarProjections.JavaClass.class, AddStarProjections.When.class})
public static class AddStarProjections extends AbstractQuickFixTest {
public void testAllFilesPresentInAddStarProjections() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/addStarProjections"), Pattern.compile("^before(\\w+)\\.kt$"), true);
@@ -182,6 +182,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/addStarProjections/beforeUnqualifiedMapOneArg.kt");
}
@TestMetadata("idea/testData/quickfix/addStarProjections/cast")
public static class Cast extends AbstractQuickFixTest {
public void testAllFilesPresentInCast() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/addStarProjections/cast"), Pattern.compile("^before(\\w+)\\.kt$"), true);
}
@TestMetadata("beforeChangeToStarProjection.kt")
public void testChangeToStarProjection() throws Exception {
doTest("idea/testData/quickfix/addStarProjections/cast/beforeChangeToStarProjection.kt");
}
@TestMetadata("beforeChangeToStarProjectionNullable.kt")
public void testChangeToStarProjectionNullable() throws Exception {
doTest("idea/testData/quickfix/addStarProjections/cast/beforeChangeToStarProjectionNullable.kt");
}
}
@TestMetadata("idea/testData/quickfix/addStarProjections/javaClass")
public static class JavaClass extends AbstractQuickFixTest {
public void testAllFilesPresentInJavaClass() throws Exception {
@@ -251,6 +269,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
public static Test innerSuite() {
TestSuite suite = new TestSuite("AddStarProjections");
suite.addTestSuite(AddStarProjections.class);
suite.addTestSuite(Cast.class);
suite.addTestSuite(JavaClass.class);
suite.addTestSuite(When.class);
return suite;