mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
inline constant: decode with this access from initial expr (IDEA-174568)
This commit is contained in:
@@ -145,8 +145,6 @@ public class InlineConstantFieldProcessor extends BaseRefactoringProcessor {
|
||||
protected void performRefactoring(@NotNull UsageInfo[] usages) {
|
||||
PsiExpression initializer = InlineConstantFieldHandler.getInitializer(myField);
|
||||
LOG.assertTrue(initializer != null);
|
||||
initializer = normalize (initializer);
|
||||
|
||||
final Set<PsiAssignmentExpression> assignments = new HashSet<>();
|
||||
for (UsageInfo info : usages) {
|
||||
if (info instanceof UsageFromJavaDoc) continue;
|
||||
@@ -215,47 +213,9 @@ public class InlineConstantFieldProcessor extends BaseRefactoringProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
if (expr instanceof PsiReferenceExpression) {
|
||||
PsiExpression qExpression = ((PsiReferenceExpression)expr).getQualifierExpression();
|
||||
if (qExpression != null) {
|
||||
initializer1 = (PsiExpression)initializer1.copy();
|
||||
PsiReferenceExpression referenceExpression = null;
|
||||
if (initializer1 instanceof PsiReferenceExpression) {
|
||||
referenceExpression = (PsiReferenceExpression)initializer1;
|
||||
}
|
||||
else if (initializer1 instanceof PsiMethodCallExpression) {
|
||||
referenceExpression = ((PsiMethodCallExpression)initializer1).getMethodExpression();
|
||||
}
|
||||
if (referenceExpression != null &&
|
||||
referenceExpression.getQualifierExpression() == null &&
|
||||
!(referenceExpression.advancedResolve(false).getCurrentFileResolveScope() instanceof PsiImportStaticStatement)) {
|
||||
referenceExpression.setQualifierExpression(qExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InlineUtil.inlineVariable(myField, initializer1, (PsiJavaCodeReferenceElement)expr);
|
||||
}
|
||||
|
||||
private static PsiExpression normalize(PsiExpression expression) {
|
||||
if (expression instanceof PsiArrayInitializerExpression) {
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
|
||||
try {
|
||||
final PsiType type = expression.getType();
|
||||
if (type != null) {
|
||||
String typeString = type.getCanonicalText();
|
||||
PsiNewExpression result = (PsiNewExpression)factory.createExpressionFromText("new " + typeString + "{}", expression);
|
||||
result.getArrayInitializer().replace(expression);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
|
||||
return expression;
|
||||
PsiExpression thisAccessExpr = expr instanceof PsiReferenceExpression ? ((PsiReferenceExpression)expr).getQualifierExpression() : null;
|
||||
PsiExpression invalidationCopy = thisAccessExpr != null ? (PsiExpression)thisAccessExpr.copy() : null;
|
||||
InlineUtil.inlineVariable(myField, initializer1, (PsiJavaCodeReferenceElement)expr, invalidationCopy);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -43,7 +43,15 @@ public class InlineUtil {
|
||||
private InlineUtil() {}
|
||||
|
||||
@NotNull
|
||||
public static PsiExpression inlineVariable(PsiVariable variable, PsiExpression initializer, PsiJavaCodeReferenceElement ref)
|
||||
public static PsiExpression inlineVariable(PsiVariable variable, PsiExpression initializer, PsiJavaCodeReferenceElement ref) throws IncorrectOperationException {
|
||||
return inlineVariable(variable, initializer, ref, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PsiExpression inlineVariable(PsiVariable variable,
|
||||
PsiExpression initializer,
|
||||
PsiJavaCodeReferenceElement ref,
|
||||
PsiExpression thisAccessExpr)
|
||||
throws IncorrectOperationException {
|
||||
PsiManager manager = initializer.getManager();
|
||||
|
||||
@@ -68,7 +76,9 @@ public class InlineUtil {
|
||||
ChangeContextUtil.encodeContextInfo(initializer, false);
|
||||
PsiExpression expr = (PsiExpression)replaceDiamondWithInferredTypesIfNeeded(initializer, ref);
|
||||
|
||||
PsiThisExpression thisAccessExpr = createThisExpression(manager, thisClass, refParent);
|
||||
if (thisAccessExpr == null) {
|
||||
thisAccessExpr = createThisExpression(manager, thisClass, refParent);
|
||||
}
|
||||
|
||||
expr = (PsiExpression)ChangeContextUtil.decodeContextInfo(expr, thisClass, thisAccessExpr);
|
||||
PsiType exprType = RefactoringUtil.getTypeByExpression(expr);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
class Foobar {
|
||||
void m(Foobar _local) {
|
||||
class Local {
|
||||
protected Foobar _field = _local;
|
||||
{
|
||||
System.out.println(this.<caret>_field);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
class Foobar {
|
||||
void m(Foobar _local) {
|
||||
class Local {
|
||||
{
|
||||
System.out.println(_local);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,10 @@ public class InlineConstantFieldTest extends LightRefactoringTestCase {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testLocalClassDecoding() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testQualifiedConstantExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user