mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
create static inner class if new expression is inside this/super constructor call (IDEA-45530)
This commit is contained in:
+25
-1
@@ -17,8 +17,10 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
|
||||
/**
|
||||
@@ -50,11 +52,33 @@ public class CreateInnerClassFromNewFix extends CreateClassFromNewFix {
|
||||
final PsiModifierList modifierList = created.getModifierList();
|
||||
LOG.assertTrue(modifierList != null);
|
||||
modifierList.setModifierProperty(PsiModifier.PRIVATE, true);
|
||||
if (PsiUtil.getEnclosingStaticElement(newExpression, targetClass) != null) {
|
||||
if (PsiUtil.getEnclosingStaticElement(newExpression, targetClass) != null || isInThisOrSuperCall(newExpression)) {
|
||||
modifierList.setModifierProperty(PsiModifier.STATIC, true);
|
||||
}
|
||||
created = (PsiClass)targetClass.add(created);
|
||||
|
||||
setupClassFromNewExpression(created, newExpression);
|
||||
}
|
||||
|
||||
private static boolean isInThisOrSuperCall(PsiNewExpression newExpression) {
|
||||
boolean inFirstConstructorLine = false;
|
||||
final PsiExpressionStatement expressionStatement = PsiTreeUtil.getParentOfType(newExpression, PsiExpressionStatement.class);
|
||||
if (expressionStatement != null) {
|
||||
final PsiExpression expression = expressionStatement.getExpression();
|
||||
if (expression instanceof PsiMethodCallExpression) {
|
||||
final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)expression).getMethodExpression();
|
||||
final PsiElement resolve = methodExpression.resolve();
|
||||
if (resolve instanceof PsiMethod && ((PsiMethod)resolve).isConstructor()) {
|
||||
final PsiElement referenceNameElement = methodExpression.getReferenceNameElement();
|
||||
if (referenceNameElement != null) {
|
||||
if (Comparing.strEqual(referenceNameElement.getText(), PsiKeyword.THIS) ||
|
||||
Comparing.strEqual(referenceNameElement.getText(), PsiKeyword.SUPER)) {
|
||||
inFirstConstructorLine = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return inFirstConstructorLine;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Create Inner Class 'Generic'" "true"
|
||||
class Base {
|
||||
void foo(){}
|
||||
}
|
||||
class Test extends Base {
|
||||
Test() {
|
||||
super.foo(new Generic<String> ());
|
||||
}
|
||||
|
||||
private class Generic<T> {
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Create Inner Class 'Generic'" "true"
|
||||
class Test {
|
||||
Test() {
|
||||
this (new Generic<String> ());
|
||||
}
|
||||
|
||||
Test(String s){}
|
||||
|
||||
private static class Generic<T> {
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create Inner Class 'Generic'" "true"
|
||||
class Base {
|
||||
void foo(){}
|
||||
}
|
||||
class Test extends Base {
|
||||
Test() {
|
||||
super.foo(new <caret>Generic<String> ());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Create Inner Class 'Generic'" "true"
|
||||
class Test {
|
||||
Test() {
|
||||
this (new <caret>Generic<String> ());
|
||||
}
|
||||
|
||||
Test(String s){}
|
||||
}
|
||||
Reference in New Issue
Block a user