diff --git a/plugins/groovy/resources/inspectionDescriptions/UnnecessaryQualifiedReference.html b/plugins/groovy/resources/inspectionDescriptions/UnnecessaryQualifiedReference.html
new file mode 100644
index 000000000000..6989333c538b
--- /dev/null
+++ b/plugins/groovy/resources/inspectionDescriptions/UnnecessaryQualifiedReference.html
@@ -0,0 +1,7 @@
+
+
+
+This inspection reports fully qualified references in Groovy files which can be replaced with import.
+
+
+
diff --git a/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/after.groovy.template b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/after.groovy.template
new file mode 100644
index 000000000000..18b12414bcae
--- /dev/null
+++ b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/after.groovy.template
@@ -0,0 +1,5 @@
+import java.util.concurrent.Semaphore
+
+def createSemaphore() {
+ new Semaphore();
+}
\ No newline at end of file
diff --git a/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/before.groovy.template b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/before.groovy.template
new file mode 100644
index 000000000000..6cc3b676181d
--- /dev/null
+++ b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/before.groovy.template
@@ -0,0 +1,3 @@
+def createSemaphore() {
+ new java.util.concurrent.Semaphore();
+}
diff --git a/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/description.html b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/description.html
new file mode 100644
index 000000000000..837a80e37929
--- /dev/null
+++ b/plugins/groovy/resources/intentionDescriptions/ReplaceQualifiedReferenceWithImportIntention/description.html
@@ -0,0 +1,6 @@
+
+
+This intention converts selected qualified reference into simple reference
+and adds corresponding import for that reference.
+
+
diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml
index 18082ea1fa19..61297584d87b 100644
--- a/plugins/groovy/src/META-INF/plugin.xml
+++ b/plugins/groovy/src/META-INF/plugin.xml
@@ -562,6 +562,11 @@
intention.category.groovy/intention.category.groovy.style
org.jetbrains.plugins.groovy.intentions.style.ImportOnDemandIntention
+
+ org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle
+ intention.category.groovy/intention.category.groovy.style
+ org.jetbrains.plugins.groovy.intentions.style.ReplaceQualifiedReferenceWithImportIntention
+
org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle
intention.category.groovy/intention.category.groovy.style
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties
index a36c7d09f13e..457002f13b2e 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties
@@ -70,4 +70,5 @@ type.doesnt.implemnt.comparable=Type ''{0}'' doesnt implement Comparable
add.method=Add method ''{0}()'' to class ''{1}''
implement.class=Implement {0}
fix.class=Fix class {0}
-check.labeled.statement=Labeled statement inspection
\ No newline at end of file
+check.labeled.statement=Labeled statement inspection
+unnecessary.qualified.reference=Unnecessary qualified reference
\ No newline at end of file
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionProvider.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionProvider.java
index 98a919a2f380..7be2e8527147 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionProvider.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionProvider.java
@@ -120,6 +120,7 @@ public class GroovyInspectionProvider implements InspectionToolProvider, Applica
GroovyNegatedConditionalInspection.class,
GroovyNegatedIfInspection.class,
GroovyResultOfIncrementOrDecrementUsedInspection.class,
+ UnnecessaryQualifiedReferenceInspection.class,
GroovyEmptyStatementBodyInspection.class,
GroovyPointlessBooleanInspection.class,
GroovyPointlessArithmeticInspection.class,
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/UnnecessaryQualifiedReferenceInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/UnnecessaryQualifiedReferenceInspection.java
new file mode 100644
index 000000000000..b4f9d4c7488d
--- /dev/null
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/UnnecessaryQualifiedReferenceInspection.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2000-2011 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.plugins.groovy.codeInspection.confusing;
+
+import com.intellij.codeInspection.ProblemDescriptor;
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiElement;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.annotations.Nls;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
+import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
+import org.jetbrains.plugins.groovy.codeInspection.GroovyFix;
+import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle;
+import org.jetbrains.plugins.groovy.intentions.style.ReplaceQualifiedReferenceWithImportIntention;
+import org.jetbrains.plugins.groovy.lang.GrReferenceAdjuster;
+import org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference;
+import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
+import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
+
+/**
+ * @author Max Medvedev
+ */
+public class UnnecessaryQualifiedReferenceInspection extends BaseInspection {
+ private static final Logger LOG = Logger.getInstance(UnnecessaryQualifiedReferenceInspection.class);
+
+ @Override
+ protected BaseInspectionVisitor buildVisitor() {
+ return new BaseInspectionVisitor() {
+ @Override
+ public void visitCodeReferenceElement(GrCodeReferenceElement refElement) {
+ super.visitCodeReferenceElement(refElement);
+
+ if (ReplaceQualifiedReferenceWithImportIntention.canBeReplacedWithImport(refElement)) {
+ registerError(refElement);
+ }
+ }
+
+ @Override
+ public void visitReferenceExpression(GrReferenceExpression referenceExpression) {
+ super.visitReferenceExpression(referenceExpression);
+
+ if (ReplaceQualifiedReferenceWithImportIntention.canBeReplacedWithImport(referenceExpression)) {
+ registerError(referenceExpression);
+ }
+ }
+ };
+ }
+
+ @Nls
+ @NotNull
+ @Override
+ public String getGroupDisplayName() {
+ return CONFUSING_CODE_CONSTRUCTS;
+ }
+
+ @Nls
+ @NotNull
+ @Override
+ public String getDisplayName() {
+ return GroovyInspectionBundle.message("unnecessary.qualified.reference");
+ }
+
+ @Override
+ protected String buildErrorString(Object... args) {
+ return GroovyInspectionBundle.message("unnecessary.qualified.reference");
+ }
+
+ @Override
+ protected GroovyFix buildFix(PsiElement location) {
+ return new GroovyFix() {
+ @Override
+ protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
+ final PsiElement startElement = descriptor.getStartElement();
+ LOG.assertTrue(startElement instanceof GrReferenceElement);
+ GrReferenceAdjuster.shortenReference((GrQualifiedReference)startElement);
+ }
+
+ @NotNull
+ @Override
+ public String getName() {
+ return GroovyInspectionBundle.message("unnecessary.qualified.reference");
+ }
+ };
+ }
+}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties
index d3979767ba0b..291b350f9114 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties
@@ -121,5 +121,7 @@ import.static.intention.name=Add static import
import.static.intention.family.name=Add Single-Member Static Import in Groovy
import.on.demand.intention.name=Add import on demand
import.on.demand.intention.family.name=Add On Demand Static Import in Groovy
+replace.qualified.reference.with.import.intention.name=Replace with import
+replace.qualified.reference.with.import.intention.family.name=Replace qualified reference with import
move.to.correct.dir.family.name=Move File to Correct Package
move.to.correct.dir=Move to ''{0}''
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/ReplaceQualifiedReferenceWithImportIntention.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/ReplaceQualifiedReferenceWithImportIntention.java
new file mode 100644
index 000000000000..7517d24cc422
--- /dev/null
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/ReplaceQualifiedReferenceWithImportIntention.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2000-2011 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.plugins.groovy.intentions.style;
+
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.util.PsiTreeUtil;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.intentions.base.Intention;
+import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
+import org.jetbrains.plugins.groovy.lang.GrReferenceAdjuster;
+import org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference;
+import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
+import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
+import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
+import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
+
+/**
+ * @author Max Medvedev
+ */
+public class ReplaceQualifiedReferenceWithImportIntention extends Intention {
+ private static final Logger LOG = Logger.getInstance(ReplaceQualifiedReferenceWithImportIntention.class);
+
+ @Override
+ protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException {
+ LOG.assertTrue(element instanceof GrReferenceElement, element.getClass().getCanonicalName() + " : " + element.getText());
+ GrReferenceAdjuster.shortenReference((GrQualifiedReference)element);
+ }
+
+ @NotNull
+ @Override
+ protected PsiElementPredicate getElementPredicate() {
+ return new MyPredicate();
+ }
+
+ public static boolean canBeReplacedWithImport(PsiElement element) {
+ if (element instanceof GrCodeReferenceElement) {
+ if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null) return false;
+ }
+ else if (element instanceof GrReferenceExpression) {
+ if (!GrReferenceAdjuster.seemsToBeQualifiedClassName((GrReferenceExpression)element)) return false;
+ }
+ else {
+ return false;
+ }
+
+ final GrReferenceElement ref = (GrReferenceElement)element;
+ if (ref.getQualifier() == null) return false;
+
+ final PsiElement resolved = ref.resolve();
+ if (!(resolved instanceof PsiClass)) return false;
+
+ final String name = ((PsiClass)resolved).getName();
+ if (name == null) return false;
+
+ final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
+ final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
+
+ final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
+ if (resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK()) {
+ return true;
+ }
+ if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private static class MyPredicate implements PsiElementPredicate {
+ @Override
+ public boolean satisfiedBy(PsiElement element) {
+ return canBeReplacedWithImport(element);
+ }
+ }
+}
+
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/GrReferenceAdjuster.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/GrReferenceAdjuster.java
index 08176f54098f..df87b6bbfc29 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/GrReferenceAdjuster.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/GrReferenceAdjuster.java
@@ -179,16 +179,16 @@ public class GrReferenceAdjuster {
PsiTreeUtil.getParentOfType(ref, GrImportStatement.class) == null;
}
- private static boolean seemsToBeQualifiedClassName(@Nullable GrExpression qualifier) {
- if (qualifier == null) return false;
- while (qualifier instanceof GrReferenceExpression) {
- final PsiElement nameElement = ((GrReferenceExpression)qualifier).getReferenceNameElement();
- if (((GrReferenceExpression)qualifier).getTypeArguments().length > 0) return false;
+ public static boolean seemsToBeQualifiedClassName(@Nullable GrExpression expr) {
+ if (expr == null) return false;
+ while (expr instanceof GrReferenceExpression) {
+ final PsiElement nameElement = ((GrReferenceExpression)expr).getReferenceNameElement();
+ if (((GrReferenceExpression)expr).getTypeArguments().length > 0) return false;
if (nameElement == null || nameElement.getNode().getElementType() != GroovyTokenTypes.mIDENT) return false;
- IElementType dotType = ((GrReferenceExpression)qualifier).getDotTokenType();
+ IElementType dotType = ((GrReferenceExpression)expr).getDotTokenType();
if (dotType != null && dotType != GroovyTokenTypes.mDOT) return false;
- qualifier = ((GrReferenceExpression)qualifier).getQualifierExpression();
+ expr = ((GrReferenceExpression)expr).getQualifierExpression();
}
- return qualifier == null;
+ return expr == null;
}
}
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrIntentionTestCase.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrIntentionTestCase.java
index 2c12cbc405dc..af21328f889e 100644
--- a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrIntentionTestCase.java
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrIntentionTestCase.java
@@ -35,7 +35,13 @@ public abstract class GrIntentionTestCase extends LightCodeInsightFixtureTestCas
myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
else {
- assertEquals("Count of enabled intentions: ", 0, list.size());
+ if (list.size() > 0) {
+ StringBuilder text = new StringBuilder("available intentions:");
+ for (IntentionAction intentionAction : list) {
+ text.append(intentionAction.getFamilyName()).append(", ");
+ }
+ fail(text.toString());
+ }
}
}
}
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ReplaceQualifiedReferenceWithImportTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ReplaceQualifiedReferenceWithImportTest.groovy
new file mode 100644
index 000000000000..84d8564448d6
--- /dev/null
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ReplaceQualifiedReferenceWithImportTest.groovy
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2011 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.plugins.groovy.intentions
+
+import org.jetbrains.plugins.groovy.util.TestUtils
+
+/**
+ * @author Max Medvedev
+ */
+public class ReplaceQualifiedReferenceWithImportTest extends GrIntentionTestCase {
+
+ @Override
+ protected String getBasePath() {
+ "${TestUtils.testDataPath}intentions/replaceQualifiedReferenceWithImport/"
+ }
+
+ private doTest(boolean intentionExists) {
+ myFixture.addClass('package p1; public class X{}')
+ myFixture.addClass('package p2; public class X{}')
+ doTest(GroovyIntentionsBundle.message('replace.qualified.reference.with.import.intention.name'), intentionExists)
+ }
+
+ void testSimple() {
+ doTest(true)
+ }
+
+ void testCan() {
+ doTest(true)
+ }
+
+ void testCannot1() {
+ doTest(false)
+ }
+
+ void testCannot2() {
+ doTest(false)
+ }
+}
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can.groovy
new file mode 100644
index 000000000000..57cfb7723a82
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can.groovy
@@ -0,0 +1,3 @@
+print p1.X
+
+print p2.X
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can_after.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can_after.groovy
new file mode 100644
index 000000000000..62f916c64cd9
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Can_after.groovy
@@ -0,0 +1,5 @@
+import p2.X
+
+print p1.X
+
+print X
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot1.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot1.groovy
new file mode 100644
index 000000000000..a94cfb3c8bdc
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot1.groovy
@@ -0,0 +1,3 @@
+import p1.X
+
+print p2.X
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot2.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot2.groovy
new file mode 100644
index 000000000000..264a53c46dd4
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Cannot2.groovy
@@ -0,0 +1,3 @@
+def X = 5
+
+print p2.X
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple.groovy
new file mode 100644
index 000000000000..d0ee048ffd7b
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple.groovy
@@ -0,0 +1 @@
+print p1.X
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple_after.groovy b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple_after.groovy
new file mode 100644
index 000000000000..4ebbe54094b9
--- /dev/null
+++ b/plugins/groovy/testdata/intentions/replaceQualifiedReferenceWithImport/Simple_after.groovy
@@ -0,0 +1,3 @@
+import p1.X
+
+print X
\ No newline at end of file