mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
introduce parameter: replace field access with getter: correct anonymous class access; check qualifier
This commit is contained in:
@@ -150,13 +150,13 @@ public class OldReferenceResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if (subj instanceof PsiField && PsiTreeUtil.isAncestor(clss, scope, false)) {
|
||||
if (subj instanceof PsiField && PsiTreeUtil.isAncestor(scope, clss, false)) {
|
||||
// probably replacing field with a getter
|
||||
if (myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) {
|
||||
if (myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL ||
|
||||
myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE &&
|
||||
!JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible((PsiMember)subj, newExpr, null)) {
|
||||
newExpr = replaceFieldWithGetter(newExpr, (PsiField)subj);
|
||||
newExpr = replaceFieldWithGetter(newExpr, (PsiField)subj, oldRef.getQualifierExpression() == null && !((PsiField)subj).hasModifierProperty(PsiModifier.STATIC));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,7 +210,7 @@ public class OldReferenceResolver {
|
||||
}
|
||||
}
|
||||
|
||||
private PsiElement replaceFieldWithGetter(PsiElement expr, PsiField psiField) throws IncorrectOperationException {
|
||||
private PsiElement replaceFieldWithGetter(PsiElement expr, PsiField psiField, boolean qualify) throws IncorrectOperationException {
|
||||
if (RefactoringUtil.isAssignmentLHS(expr)) {
|
||||
// todo: warning
|
||||
return expr;
|
||||
@@ -228,9 +228,13 @@ public class OldReferenceResolver {
|
||||
String id = getter.getName();
|
||||
String qualifier = null;
|
||||
if (newExpr instanceof PsiReferenceExpression) {
|
||||
final PsiExpression qualifierExpression = ((PsiReferenceExpression)newExpr).getQualifierExpression();
|
||||
if (qualifierExpression != null) {
|
||||
qualifier = qualifierExpression.getText();
|
||||
if (qualify) {
|
||||
qualifier = getInstanceRef(factory).getText();
|
||||
} else {
|
||||
final PsiExpression qualifierExpression = ((PsiReferenceExpression)newExpr).getQualifierExpression();
|
||||
if (qualifierExpression != null) {
|
||||
qualifier = qualifierExpression.getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
PsiMethodCallExpression getterCall =
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
public class Test {
|
||||
private String myStr;
|
||||
|
||||
public String getMyStr() {
|
||||
return myStr;
|
||||
}
|
||||
|
||||
void foo(String anObject) {
|
||||
new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(anObject);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class X {
|
||||
public void n() {
|
||||
final Test test = new Test();
|
||||
test.foo(test.getMyStr());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
public class Test {
|
||||
private String myStr;
|
||||
|
||||
public String getMyStr() {
|
||||
return myStr;
|
||||
}
|
||||
|
||||
void foo() {
|
||||
new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(<selection>myStr</selection>);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class X {
|
||||
public void n() {
|
||||
new Test().foo();
|
||||
}
|
||||
}
|
||||
@@ -244,6 +244,10 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false);
|
||||
}
|
||||
|
||||
public void testGetterQualifier() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE, false, false, false, false);
|
||||
}
|
||||
|
||||
public void testIncompleteEnumDefinition() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, false, false, "Incomplete call(Root()): 2 parameters expected but only 0 found\n" +
|
||||
"Incomplete call(Root()): expected to delete the 1 parameter but only 0 parameters found");
|
||||
|
||||
Reference in New Issue
Block a user