delegate methods: fix single field case (IDEA-154698)

This commit is contained in:
Anna.Kozlova
2016-04-14 13:55:10 +02:00
parent 000306c168
commit 24c2c0fa95
4 changed files with 46 additions and 17 deletions

View File

@@ -345,13 +345,14 @@ public class GenerateDelegateHandler implements LanguageCodeInsightActionHandler
int offset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(offset);
if (element == null) return null;
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
final PsiClass targetClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
PsiClass aClass = targetClass;
if (aClass == null) return null;
List<PsiElementClassMember> result = new ArrayList<PsiElementClassMember>();
while (aClass != null) {
collectTargetsInClass(element, aClass, result);
collectTargetsInClass(element, targetClass, aClass, result);
if (aClass.hasModifierProperty(PsiModifier.STATIC)) break;
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
}
@@ -359,12 +360,16 @@ public class GenerateDelegateHandler implements LanguageCodeInsightActionHandler
return result.toArray(new PsiElementClassMember[result.size()]);
}
private static void collectTargetsInClass(PsiElement element, final PsiClass aClass, List<PsiElementClassMember> result) {
private static void collectTargetsInClass(PsiElement element,
final PsiClass targetClass,
final PsiClass aClass,
List<PsiElementClassMember> result) {
final PsiField[] fields = aClass.getAllFields();
PsiResolveHelper helper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
for (PsiField field : fields) {
final PsiType type = field.getType();
if (helper.isAccessible(field, aClass, aClass) && type instanceof PsiClassType && !PsiTreeUtil.isAncestor(field, element, false)) {
if (helper.isAccessible(field, aClass, aClass) && type instanceof PsiClassType &&
!(PsiTreeUtil.isAncestor(field, element, false) && targetClass != aClass)) {
final PsiClass containingClass = field.getContainingClass();
if (containingClass != null) {
result.add(new PsiFieldMember(field, TypeConversionUtil.getSuperClassSubstitutor(containingClass, aClass, PsiSubstitutor.EMPTY)));
@@ -378,7 +383,7 @@ public class GenerateDelegateHandler implements LanguageCodeInsightActionHandler
if (containingClass == null || CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) continue;
final PsiType returnType = method.getReturnType();
if (returnType != null && PropertyUtil.isSimplePropertyGetter(method) && helper.isAccessible(method, aClass, aClass) &&
returnType instanceof PsiClassType && !PsiTreeUtil.isAncestor(method, element, false)) {
returnType instanceof PsiClassType && !(PsiTreeUtil.isAncestor(method, element, false) && targetClass != aClass)) {
result.add(new PsiMethodMember(method, TypeConversionUtil.getSuperClassSubstitutor( containingClass, aClass,PsiSubstitutor.EMPTY)));
}
}

View File

@@ -0,0 +1,10 @@
class A {
void foo(){}
}
class Test{
public void foo() {
a.foo();
}
private A a;
}

View File

@@ -0,0 +1,6 @@
class A {
void foo(){}
}
class Test{
private A <caret>a;
}

View File

@@ -23,23 +23,23 @@ public class DelegateMethodsTest extends LightCodeInsightTestCase {
}
public void testStaticMemberWithNonStaticField() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testTypeParam() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testExistingMethodWithAnnotation() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testDelegateToContainingClassField() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testDelegateFromStaticClassField() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testCopyJavadoc() throws Exception {
@@ -57,34 +57,42 @@ public class DelegateMethodsTest extends LightCodeInsightTestCase {
}
public void testSuperSubstitution() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testCopyAnnotationWithParams() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testMultipleOverrideAnnotations() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testStripSuppressWarningsAnnotation() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testDoNotOverrideFinal() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testAllowDelegateToFinal() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testDelegateWithSubstitutionOverrides() throws Exception {
doTest(getTestName(false));
doTest();
}
public void testDelegateWithSubstitutionNoOverrides() throws Exception {
doTest();
}
public void testSingleField() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(getTestName(false));
}