IntelliJ structural search find derived classes if base class matches criteria












0















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)?










share|improve this question



























    0















    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)?










    share|improve this question

























      0












      0








      0








      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)?










      share|improve this question














      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)?







      java intellij-idea ide editor structural-search






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 18:56









      Mark VYMark VY

      675517




      675517
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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;





          share|improve this answer


























          • I don't think so, because the field is private in the base class

            – Mark VY
            Nov 16 '18 at 17:43






          • 1





            The 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








          • 1





            Well, 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



















          1














          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.






          share|improve this answer



















          • 1





            It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

            – Bas Leijdekkers
            Dec 27 '18 at 19:01











          • So THAT's what's wrong. Thank you!

            – Mark VY
            Dec 27 '18 at 20:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287778%2fintellij-structural-search-find-derived-classes-if-base-class-matches-criteria%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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;





          share|improve this answer


























          • I don't think so, because the field is private in the base class

            – Mark VY
            Nov 16 '18 at 17:43






          • 1





            The 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








          • 1





            Well, 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
















          2














          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;





          share|improve this answer


























          • I don't think so, because the field is private in the base class

            – Mark VY
            Nov 16 '18 at 17:43






          • 1





            The 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








          • 1





            Well, 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














          2












          2








          2







          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;





          share|improve this answer















          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;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 27 '18 at 19:11

























          answered Nov 16 '18 at 14:10









          Bas LeijdekkersBas Leijdekkers

          12.3k13144




          12.3k13144













          • I don't think so, because the field is private in the base class

            – Mark VY
            Nov 16 '18 at 17:43






          • 1





            The 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








          • 1





            Well, 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



















          • I don't think so, because the field is private in the base class

            – Mark VY
            Nov 16 '18 at 17:43






          • 1





            The 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








          • 1





            Well, 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

















          I don't think so, because the field is private in the base class

          – Mark VY
          Nov 16 '18 at 17:43





          I don't think so, because the field is private in the base class

          – Mark VY
          Nov 16 '18 at 17:43




          1




          1





          The inspection has an option to also warn when the superclass’ field is private

          – Bas Leijdekkers
          Nov 19 '18 at 17:30







          The 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







          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






          1




          1





          Well, if it would match the type there would be way more false positives.

          – Bas Leijdekkers
          Nov 20 '18 at 15:54





          Well, 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





          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













          1














          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.






          share|improve this answer



















          • 1





            It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

            – Bas Leijdekkers
            Dec 27 '18 at 19:01











          • So THAT's what's wrong. Thank you!

            – Mark VY
            Dec 27 '18 at 20:15
















          1














          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.






          share|improve this answer



















          • 1





            It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

            – Bas Leijdekkers
            Dec 27 '18 at 19:01











          • So THAT's what's wrong. Thank you!

            – Mark VY
            Dec 27 '18 at 20:15














          1












          1








          1







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '18 at 3:01









          Mark VYMark VY

          675517




          675517








          • 1





            It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

            – Bas Leijdekkers
            Dec 27 '18 at 19:01











          • So THAT's what's wrong. Thank you!

            – Mark VY
            Dec 27 '18 at 20:15














          • 1





            It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

            – Bas Leijdekkers
            Dec 27 '18 at 19:01











          • So THAT's what's wrong. Thank you!

            – Mark VY
            Dec 27 '18 at 20:15








          1




          1





          It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

          – Bas Leijdekkers
          Dec 27 '18 at 19:01





          It appears the "reference" filter needs a "search in hierarchy" checkbox. You may want to file a request for that at youtrack.jetbrains.com

          – Bas Leijdekkers
          Dec 27 '18 at 19:01













          So THAT's what's wrong. Thank you!

          – Mark VY
          Dec 27 '18 at 20:15





          So THAT's what's wrong. Thank you!

          – Mark VY
          Dec 27 '18 at 20:15


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287778%2fintellij-structural-search-find-derived-classes-if-base-class-matches-criteria%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues