Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I check whether a class uses a particular other class?  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In our application, we have a class doing debug-related stuff. Every call to it is supposed to be protected by a check of the value of a particular constant, set by generated code which is different, depending on whether the build is for debug or release purposes. The idea is that a release build will not use, or even load, the debug class.

This works fine, except when people forget to protect their code appropriately. Therefore, I would like to make a little tool, to run during the build process, to check whether we have successfully excluded the debug class.

The approach that currently seems most attractive is to look in each class file to see if there are any bytecodes that refer to the debug class. There shouldn't be any, for a release build.

Is this feasible? What would be the best way to examine the class file?

Any other ideas? I did a very quick program that did a rather dumb examination of the source. It was pretty good at spotting the mistakes, but created quite a lot of false positives (where it thought there was a mistake, but there wasn't really).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, if I had to do something like that, I would look at whether a byte code manipulation library like ASM or BCEL could help me.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose something like BCEL could be used, but it seems rather heavyweight, for my one, relatively simple requirement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!