replace implements with static imports (IDEA-37581): check for supers

This commit is contained in:
anna
2010-05-11 14:40:59 +04:00
parent 8be6b22b0a
commit e860849c8b
4 changed files with 67 additions and 4 deletions

View File

@@ -24,12 +24,17 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReplaceImplementsWithStaticImportAction implements IntentionAction {
private static final Logger LOG = Logger.getInstance("#" + ReplaceImplementsWithStaticImportAction.class.getName());
@@ -64,9 +69,19 @@ public class ReplaceImplementsWithStaticImportAction implements IntentionAction
return false;
}
if (targetClass.getMethods().length > 0) return false;
return targetClass.getFields().length > 0;
final PsiReferenceList extendsList = targetClass.getExtendsList();
LOG.assertTrue(extendsList != null);
if (extendsList.getReferencedTypes().length > 0) {
final List<PsiMethod> methods = new ArrayList<PsiMethod>(Arrays.asList(targetClass.getAllMethods()));
final PsiClass objectClass =
JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(project));
if (objectClass == null) return false;
methods.removeAll(Arrays.asList(objectClass.getMethods()));
if (methods.size() > 0) return false;
} else if (targetClass.getMethods().length > 0){
return false;
}
return targetClass.getAllFields().length > 0;
}
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
@@ -85,7 +100,7 @@ public class ReplaceImplementsWithStaticImportAction implements IntentionAction
final PsiClass targetClass = (PsiClass)target;
for (PsiField constField : targetClass.getFields()) {
for (PsiField constField : targetClass.getAllFields()) {
for (PsiReference ref : ReferencesSearch.search(constField, new LocalSearchScope(psiClass))) {
((PsiReferenceExpressionImpl)ref).bindToElementViaStaticImport(targetClass, constField.getName(), ((PsiJavaFile)file).getImportList());
}

View File

@@ -0,0 +1,19 @@
// "Replace Implements with Static Import" "true"
import static I.BAZZ;
import static I.FOO;
public class X {
void bar() {
System.out.println(FOO);
System.out.println(BAZZ);
}
}
interface I extends I1{
String FOO = "foo";
}
interface I1 {
String BAZZ = "bazz";
}

View File

@@ -0,0 +1,14 @@
// "Replace Implements with Static Import" "false"
public class X implements <caret>I {
void bar() {
System.out.println(FOO);
}
}
interface I extends I1{
String FOO = "foo";
}
interface I1 {
void foo(){}
}

View File

@@ -0,0 +1,15 @@
// "Replace Implements with Static Import" "true"
public class X implements <caret>I {
void bar() {
System.out.println(FOO);
System.out.println(BAZZ);
}
}
interface I extends I1{
String FOO = "foo";
}
interface I1 {
String BAZZ = "bazz";
}