create from usage: fix target class when created from super expression (IDEA-145227)

include all classes in tests in target and choose the first available
This commit is contained in:
Anna Kozlova
2015-09-30 20:41:12 +02:00
parent 7f2fe8c5a2
commit 9dabc8ed14
3 changed files with 46 additions and 8 deletions
@@ -94,7 +94,7 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction {
List<PsiClass> targetClasses = getTargetClasses(element);
if (targetClasses.isEmpty()) return;
if (targetClasses.size() == 1) {
if (targetClasses.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
doInvoke(project, targetClasses.get(0));
} else {
chooseTargetClass(targetClasses, editor);
@@ -379,10 +379,6 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction {
return Collections.emptyList();
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
return Collections.singletonList(psiClass);
}
if (!allowOuterClasses || !isAllowOuterTargetClass()) {
final ArrayList<PsiClass> classes = new ArrayList<PsiClass>();
collectSupers(psiClass, classes);
@@ -120,7 +120,10 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
PsiMethodCallExpression call = getMethodCall();
if (call == null) return Collections.emptyList();
for (PsiClass target : targets) {
if (target.isInterface() && shouldCreateStaticMember(call.getMethodExpression(), target) && !PsiUtil.isLanguageLevel8OrHigher(target)) continue;
if (shouldCreateStaticMember(call.getMethodExpression(), target)){
if (target.isInterface() && !PsiUtil.isLanguageLevel8OrHigher(target)) continue;
if (target.getContainingClass() != null && !target.hasModifierProperty(PsiModifier.STATIC)) continue;
}
if (!isMethodSignatureExists(call, target)) {
result.add(target);
}
@@ -20,13 +20,11 @@ import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.actionSystem.EditorActionManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiModifier
import com.intellij.psi.util.PsiTreeUtil
/**
* @author ven
*/
@@ -132,6 +130,47 @@ class Usage {
state.gotoEnd()
}
public void "test prefer outer class when static is not applicable for inner"() {
configureFromFileText "a.java", """
class A {
int x;
A(int x) { this.x = x; }
class B extends A{
B(int x) { super(f<caret>oo(x)); }
}
}
"""
TemplateManagerImpl.setTemplateTesting(project, testRootDisposable);
doAction("Create method 'foo'")
def state = TemplateManagerImpl.getTemplateState(getEditor())
def document = getEditor().getDocument()
def offset = getEditor().getCaretModel().getOffset()
ApplicationManager.application.runWriteAction {
def method = PsiTreeUtil.getParentOfType(getFile().findElementAt(offset), PsiMethod.class)
method.getModifierList().setModifierProperty(PsiModifier.STATIC, false)
PsiDocumentManager.getInstance(getFile().project).commitDocument(document)
}
state.gotoEnd()
checkResultByText """
class A {
int x;
A(int x) { this.x = x; }
class B extends A{
B(int x) { super(foo(x)); }
}
private int foo(int x) {
return 0;
}
}
"""
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/createMethodFromUsage";