From 5257f8451a2162e337876c06fda4f4513f52bb51 Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Mon, 7 Feb 2011 18:11:15 +0300 Subject: [PATCH] IDEA-64816 Groovy: "Find Usages" doesn't find usages of class members accesses as Groovy properties --- .../GroovyFindUsagesHandlerFactory.java | 31 +++++++---- .../lang/psi/util/GroovyPropertyUtils.java | 54 +++++++++++++++---- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/findUsages/GroovyFindUsagesHandlerFactory.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/findUsages/GroovyFindUsagesHandlerFactory.java index d3cbc97a606c..d53d33a9874d 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/findUsages/GroovyFindUsagesHandlerFactory.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/findUsages/GroovyFindUsagesHandlerFactory.java @@ -57,33 +57,35 @@ public class GroovyFindUsagesHandlerFactory extends JavaFindUsagesHandlerFactory @Override public PsiElement[] getSecondaryElements() { PsiElement element = getPsiElement(); - if (ApplicationManager.getApplication().isUnitTestMode()) return PsiElement.EMPTY_ARRAY; final PsiField field = (PsiField)element; PsiClass containingClass = field.getContainingClass(); if (containingClass != null) { - PsiMethod getter = GroovyPropertyUtils.findGetterForField(field); - PsiMethod setter = GroovyPropertyUtils.findSetterForField(field); - if (getter != null || setter != null) { + PsiMethod[] getters = GroovyPropertyUtils.getAllGettersByField(field); + PsiMethod[] setters = GroovyPropertyUtils.getAllSettersByField(field); + if (getters.length + setters.length > 0) { final boolean doSearch; - if ((getter == null || !getter.isPhysical()) && (setter == null || !setter.isPhysical())) { - doSearch = true; - } - else { + if (arePhysical(getters) || arePhysical(setters)) { + if (ApplicationManager.getApplication().isUnitTestMode()) return PsiElement.EMPTY_ARRAY; doSearch = Messages.showDialog(FindBundle.message("find.field.accessors.prompt", field.getName()), FindBundle.message("find.field.accessors.title"), new String[]{CommonBundle.getYesButtonText(), CommonBundle.getNoButtonText()}, 0, Messages.getQuestionIcon()) == DialogWrapper.OK_EXIT_CODE; } + else { + doSearch = true; + } if (doSearch) { final List elements = new ArrayList(); - if (getter != null) { + for (PsiMethod getter : getters) { ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(getter, ACTION_STRING)); } - if (setter != null) { + + for (PsiMethod setter : setters) { ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(setter, ACTION_STRING)); } return elements.toArray(new PsiElement[elements.size()]); - } else { + } + else { return PsiElement.EMPTY_ARRAY; } } @@ -92,4 +94,11 @@ public class GroovyFindUsagesHandlerFactory extends JavaFindUsagesHandlerFactory } }; } + + private static boolean arePhysical(PsiMethod[] methods) { + for (PsiMethod method : methods) { + if (method.isPhysical()) return true; + } + return false; + } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GroovyPropertyUtils.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GroovyPropertyUtils.java index 7703bfdeee05..f7549ed0b22a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GroovyPropertyUtils.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GroovyPropertyUtils.java @@ -42,6 +42,15 @@ public class GroovyPropertyUtils { private GroovyPropertyUtils() { } + public static PsiMethod[] getAllSettersByField(PsiField field) { + return getAllSetters(field.getContainingClass(), field.getName(), field.hasModifierProperty(GrModifier.STATIC), false); + } + + @NotNull + public static PsiMethod[] getAllGettersByField(PsiField field) { + return getAllGetters(field.getContainingClass(), field.getName(), field.hasModifierProperty(GrModifier.STATIC), false); + } + @Nullable public static PsiMethod findSetterForField(PsiField field) { final PsiClass containingClass = field.getContainingClass(); @@ -84,11 +93,9 @@ public class GroovyPropertyUtils { return null; } - /** - * @deprecated use PropertyUtil.getAllProperties() instead. - */ - @Deprecated() - public static List getAllPropertyGetters(@NotNull PsiClass aClass, @Nullable Boolean isStatic, boolean checkSuperClasses) { + @NotNull + public static PsiMethod[] getAllGetters(PsiClass aClass, String propertyName, boolean isStatic, boolean checkSuperClasses) { + if (aClass == null) return PsiMethod.EMPTY_ARRAY; PsiMethod[] methods; if (checkSuperClasses) { methods = aClass.getAllMethods(); @@ -97,19 +104,46 @@ public class GroovyPropertyUtils { methods = aClass.getMethods(); } - List res = new ArrayList(methods.length); - + List result = new ArrayList(); for (PsiMethod method : methods) { - if (isStatic != null && method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue; + if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue; if (isSimplePropertyGetter(method)) { - res.add(method); + if (propertyName.equals(getPropertyNameByGetter(method))) { + result.add(method); + } } } - return res; + return result.toArray(new PsiMethod[result.size()]); } + @NotNull + public static PsiMethod[] getAllSetters(PsiClass aClass, String propertyName, boolean isStatic, boolean checkSuperClasses) { + if (aClass == null) return PsiMethod.EMPTY_ARRAY; + PsiMethod[] methods; + if (checkSuperClasses) { + methods = aClass.getAllMethods(); + } + else { + methods = aClass.getMethods(); + } + + List result = new ArrayList(); + for (PsiMethod method : methods) { + if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue; + + if (isSimplePropertySetter(method)) { + if (propertyName.equals(getPropertyNameBySetter(method))) { + result.add(method); + } + } + } + + return result.toArray(new PsiMethod[result.size()]); + } + + @Nullable public static PsiMethod findPropertyGetter(@Nullable PsiClass aClass, String propertyName,