From 70d48263177138591bae4e1cd019b74d86fc5390 Mon Sep 17 00:00:00 2001 From: svtk Date: Wed, 28 Sep 2011 11:58:55 +0400 Subject: [PATCH] Tests for import class action added --- .../jet/lang/resolve/BodyResolver.java | 9 +++++- .../jetbrains/jet/lang/types/ErrorUtils.java | 2 +- .../jetbrains/jet/plugin/JetPluginUtil.java | 3 +- .../quickfix/ChangeAccessorTypeFix.java | 3 +- .../plugin/quickfix/ImportClassHelper.java | 3 +- .../quickfix/RemovePartsFromPropertyFix.java | 8 +---- .../quickfix/ReplaceSafeCallToDotCall.java | 1 - .../classImport/afterHasThisImport.kt | 15 +++++++++ .../classImport/afterNoImportJavaLang.kt | 8 +++++ .../classImport/afterNoImportJetStandard.kt | 8 +++++ .../quickfix/classImport/afterToImport1.kt | 11 +++++++ .../quickfix/classImport/afterToImport2.kt | 15 +++++++++ .../classImport/beforeHasThisImport.kt | 15 +++++++++ .../classImport/beforeNoImportJavaLang.kt | 8 +++++ .../classImport/beforeNoImportJetStandard.kt | 8 +++++ .../quickfix/classImport/beforeToImport1.kt | 10 ++++++ .../quickfix/classImport/beforeToImport2.kt | 13 ++++++++ .../jet/plugin/quickfix/ClassImportTests.java | 32 +++++++++++++++++++ 18 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 idea/testData/quickfix/classImport/afterHasThisImport.kt create mode 100644 idea/testData/quickfix/classImport/afterNoImportJavaLang.kt create mode 100644 idea/testData/quickfix/classImport/afterNoImportJetStandard.kt create mode 100644 idea/testData/quickfix/classImport/afterToImport1.kt create mode 100644 idea/testData/quickfix/classImport/afterToImport2.kt create mode 100644 idea/testData/quickfix/classImport/beforeHasThisImport.kt create mode 100644 idea/testData/quickfix/classImport/beforeNoImportJavaLang.kt create mode 100644 idea/testData/quickfix/classImport/beforeNoImportJetStandard.kt create mode 100644 idea/testData/quickfix/classImport/beforeToImport1.kt create mode 100644 idea/testData/quickfix/classImport/beforeToImport2.kt create mode 100644 idea/tests/org/jetbrains/jet/plugin/quickfix/ClassImportTests.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/BodyResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/BodyResolver.java index 24d8ea9f5dd..1d9d9265e94 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/BodyResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/BodyResolver.java @@ -526,6 +526,9 @@ public class BodyResolver { if (propertyDescriptor.getModality() == Modality.ABSTRACT) { JetType returnType = propertyDescriptor.getReturnType(); + if (returnType instanceof DeferredType) { + returnType = ((DeferredType) returnType).getActualType(); + } JetExpression initializer = property.getInitializer(); if (initializer != null) { @@ -575,7 +578,11 @@ public class BodyResolver { } if (inTrait) { // context.getTrace().getErrorHandler().genericError(initializer.getNode(), "Property initializers are not allowed in traits"); - context.getTrace().report(PROPERTY_INITIALIZER_IN_TRAIT.on(property, initializer, propertyDescriptor.getReturnType())); + JetType returnType = propertyDescriptor.getReturnType(); + if (returnType instanceof DeferredType) { + returnType = ((DeferredType) returnType).getActualType(); + } + context.getTrace().report(PROPERTY_INITIALIZER_IN_TRAIT.on(property, initializer, returnType)); } else if (!backingFieldRequired) { // context.getTrace().getErrorHandler().genericError(initializer.getNode(), "Initializer is not allowed here because this property has no backing field"); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java index b30f40e2aa2..6e5761683d0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java @@ -177,7 +177,7 @@ public class ErrorUtils { } public static boolean isError(@NotNull DeclarationDescriptor candidate) { - return candidate.getContainingDeclaration() == getErrorClass(); + return candidate.getContainingDeclaration() == getErrorClass() || candidate == ERROR_MODULE; } private static class ErrorTypeImpl implements JetType { diff --git a/idea/src/org/jetbrains/jet/plugin/JetPluginUtil.java b/idea/src/org/jetbrains/jet/plugin/JetPluginUtil.java index 2bbac86b500..37cc3071f59 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetPluginUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/JetPluginUtil.java @@ -9,6 +9,7 @@ import org.jetbrains.jet.lang.resolve.java.JavaClassDescriptor; import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.types.DeferredType; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.jetbrains.jet.lang.types.JetType; @@ -61,7 +62,7 @@ public class JetPluginUtil { JetScope libraryScope = standardLibrary.getLibraryScope(); DeclarationDescriptor declaration = type.getMemberScope().getContainingDeclaration(); - if (declaration instanceof JavaClassDescriptor) { + if (declaration instanceof JavaClassDescriptor || ErrorUtils.isError(declaration)) { return false; } while (!(declaration instanceof NamespaceDescriptor)) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java index dfab9fc0a0e..17590360691 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java @@ -48,7 +48,6 @@ public class ChangeAccessorTypeFix extends JetIntentionAction createFactory() { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java index 09c5891de8f..a8471ab2e96 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java @@ -3,6 +3,7 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetPluginUtil; @@ -21,7 +22,7 @@ public class ImportClassHelper { JetNamespace namespace = (JetNamespace) parent; List importDirectives = namespace.getImportDirectives(); - if (JetPluginUtil.checkTypeIsStandard(type, element.getProject())) { + if (JetPluginUtil.checkTypeIsStandard(type, element.getProject()) || ErrorUtils.isError(type.getMemberScope().getContainingDeclaration())) { element.replace(newElement); return; } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java index 2b4675c931c..55429205dff 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java @@ -10,7 +10,6 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticParameters; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithPsiElement; import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.types.DeferredType; import org.jetbrains.jet.lang.types.JetType; /** @@ -22,12 +21,7 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction private RemovePartsFromPropertyFix(@NotNull JetProperty element, JetType type) { super(element); - if (type instanceof DeferredType) { - this.type = ((DeferredType) type).getActualType(); - } - else { - this.type = type; - } + this.type = type; partsToRemove = partsToRemove(element.getGetter() != null && element.getGetter().getBodyExpression() != null, element.getSetter() != null && element.getSetter().getBodyExpression() != null, element.getInitializer() != null); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallToDotCall.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallToDotCall.java index 9f6f3016f8e..61a8cc6e0ad 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallToDotCall.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallToDotCall.java @@ -36,7 +36,6 @@ public class ReplaceSafeCallToDotCall extends JetIntentionAction { JetSafeQualifiedExpression safeQualifiedExpression = (JetSafeQualifiedExpression) element; JetDotQualifiedExpression newElement = (JetDotQualifiedExpression) JetPsiFactory.createExpression(project, "x.foo"); - //TODO check for null CodeEditUtil.replaceChild(newElement.getNode(), newElement.getSelectorExpression().getNode(), safeQualifiedExpression.getSelectorExpression().getNode()); CodeEditUtil.replaceChild(newElement.getNode(), newElement.getReceiverExpression().getNode(), safeQualifiedExpression.getReceiverExpression().getNode()); diff --git a/idea/testData/quickfix/classImport/afterHasThisImport.kt b/idea/testData/quickfix/classImport/afterHasThisImport.kt new file mode 100644 index 00000000000..097dd252598 --- /dev/null +++ b/idea/testData/quickfix/classImport/afterHasThisImport.kt @@ -0,0 +1,15 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections + +namespace b { + +import java.util.List + +class M { + trait A { + val l : List? + } +} +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/afterNoImportJavaLang.kt b/idea/testData/quickfix/classImport/afterNoImportJavaLang.kt new file mode 100644 index 00000000000..1ffb2769998 --- /dev/null +++ b/idea/testData/quickfix/classImport/afterNoImportJavaLang.kt @@ -0,0 +1,8 @@ +// "Remove initializer from property" "true" +namespace a + +class M { + trait A { + abstract val e : Exception + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/afterNoImportJetStandard.kt b/idea/testData/quickfix/classImport/afterNoImportJetStandard.kt new file mode 100644 index 00000000000..dc3f621c67e --- /dev/null +++ b/idea/testData/quickfix/classImport/afterNoImportJetStandard.kt @@ -0,0 +1,8 @@ +// "Remove initializer from property" "true" +namespace a + +class M { + trait A { + abstract val i : Int + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/afterToImport1.kt b/idea/testData/quickfix/classImport/afterToImport1.kt new file mode 100644 index 00000000000..60d9ffb0c1b --- /dev/null +++ b/idea/testData/quickfix/classImport/afterToImport1.kt @@ -0,0 +1,11 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections +import java.util.List + +class M { + trait A { + abstract val l : List? + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/afterToImport2.kt b/idea/testData/quickfix/classImport/afterToImport2.kt new file mode 100644 index 00000000000..60556ccd964 --- /dev/null +++ b/idea/testData/quickfix/classImport/afterToImport2.kt @@ -0,0 +1,15 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections + +namespace b { + +import java.util.List + +class M { + trait A { + abstract val l : List? + } +} +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/beforeHasThisImport.kt b/idea/testData/quickfix/classImport/beforeHasThisImport.kt new file mode 100644 index 00000000000..bfcc05e9b9c --- /dev/null +++ b/idea/testData/quickfix/classImport/beforeHasThisImport.kt @@ -0,0 +1,15 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections + +namespace b { + +import java.util.List + +class M { + trait A { + val l = Collections.emptyList() + } +} +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/beforeNoImportJavaLang.kt b/idea/testData/quickfix/classImport/beforeNoImportJavaLang.kt new file mode 100644 index 00000000000..d81efa4eaa8 --- /dev/null +++ b/idea/testData/quickfix/classImport/beforeNoImportJavaLang.kt @@ -0,0 +1,8 @@ +// "Remove initializer from property" "true" +namespace a + +class M { + trait A { + abstract val e = Exception("") + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/beforeNoImportJetStandard.kt b/idea/testData/quickfix/classImport/beforeNoImportJetStandard.kt new file mode 100644 index 00000000000..320776e668a --- /dev/null +++ b/idea/testData/quickfix/classImport/beforeNoImportJetStandard.kt @@ -0,0 +1,8 @@ +// "Remove initializer from property" "true" +namespace a + +class M { + trait A { + abstract val i = 10 + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/beforeToImport1.kt b/idea/testData/quickfix/classImport/beforeToImport1.kt new file mode 100644 index 00000000000..075adebf27d --- /dev/null +++ b/idea/testData/quickfix/classImport/beforeToImport1.kt @@ -0,0 +1,10 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections + +class M { + trait A { + abstract val l = Collections.emptyList() + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/classImport/beforeToImport2.kt b/idea/testData/quickfix/classImport/beforeToImport2.kt new file mode 100644 index 00000000000..c99cd4ebe4d --- /dev/null +++ b/idea/testData/quickfix/classImport/beforeToImport2.kt @@ -0,0 +1,13 @@ +// "Remove initializer from property" "true" +namespace a + +import java.util.Collections + +namespace b { + +class M { + trait A { + abstract val l = Collections.emptyList() + } +} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/ClassImportTests.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/ClassImportTests.java new file mode 100644 index 00000000000..2b29637e9fa --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/ClassImportTests.java @@ -0,0 +1,32 @@ +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase; +import com.intellij.openapi.projectRoots.Sdk; +import org.jetbrains.jet.JetTestCaseBase; + +/** + * @author svtk + */ +public class ClassImportTests extends LightQuickFixTestCase { + + public void test() throws Exception { + doAllTests(); + } + + @Override + protected String getBasePath() { + return "/quickfix/classImport"; + } + + @Override + protected String getTestDataPath() { + return JetTestCaseBase.getTestDataPathBase(); + } + + @Override + protected Sdk getProjectJDK() { + return JetTestCaseBase.jdkFromIdeaHome(); + } + +} +