Suppose I have a base class with a field refCount
. I accidentally create a derived class which ALSO declares a field with the same name. (Actually, the name is not important: what matters is the type, which is ReferenceCount; but the names are pretty consistent.) This is a waste of memory, so it would be nice to be able to find such things automatically. Can this be done with Structural Search (or some other way for that matter)?

- 1,489
- 16
- 31
2 Answers
It looks like the Java | Visibility | Field name hides field in superclass
inspection does something similar to what you need.
If you want to do this with Structural Search, you could do something like this. Search template:
class $X$ {
RefCount $f$;
}
And add the following "script" filter on the "complete match":
import com.intellij.psi.*;
for (PsiClass aClass : X.getSupers()) { // X refers to the template var $X$
for (PsiField field : aClass.getAllFields()) {
// compares the type of the super field with the type of field $a$
if (field.getType().equals(a.getType())) {
return true;
}
}
}
return false;

- 23,709
- 4
- 70
- 68
-
I don't think so, because the field is private in the base class – Mark VY Nov 16 '18 at 17:43
-
1The inspection has an option to also warn when the superclass’ field is `private` – Bas Leijdekkers Nov 19 '18 at 17:30
-
Well then, thank you, this solves 90% of my problem. Ideally I would want to match my the TYPE of the field, rather than its name, but since the names are pretty consistent, this will do. Thanks! EDIT: wait no it doesn't, too many false positives unrelated to the field I was interested in, though arguably those are also worth looking at. – Mark VY Nov 19 '18 at 19:57
-
1Well, if it would match the type there would be way more false positives. – Bas Leijdekkers Nov 20 '18 at 15:54
-
Normally, yes. I wanted to say: find all cases where the base class has a field of type X and the child class does too, where X is under my control. In particular, I want X=RefCount. This should have no false positives at all. – Mark VY Nov 20 '18 at 23:52
-
Thanks!! I am going to try this today! – Mark VY Dec 28 '18 at 16:08
It looks like the new version of IntelliJ (2018.3) has made this fairly easy, albeit slightly awkward. Had I known this was coming I would have been tempted to use the preview release.
The trick is to first create (and save!) a search template for the base class; for instance like this:
class $Class$ {RefCount $count$;}
and then do something like this:
class $Child$ extends $Base$ {RefCount $count$;}
And then add a "reference" filter for $Base$
which refers to the template you saved.
This trick doesn't work if Child extends Parent which in turn extends GrandParent, where the field is declared in Child and in GrandParent but not in Parent. I think this can be fixed to work for that case without much trouble but I don't actually know how to do it.

- 1,489
- 16
- 31
-
1It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at https://youtrack.jetbrains.com – Bas Leijdekkers Dec 27 '18 at 19:01