Michael Schnell is living in Hamburg (Germany) and works as a Project Manager, Architect and Senior Java Developer. Michael is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Finding Methods Calls Programmatically

  • submit to reddit

In the current project a defect was raised with the following exception attached:

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

After I dug into the code, I found the cause was simple. There was no rounding mode given for calculations that might produce big decimals, like in this example:


Every senior developer knows about this trap, but it’s still easy to forget. A default of BigDecimal.ROUND_HALF_UP, applied when no rounding mode is defined explicitly, would have made the method more safe to use.

How is it possible to prevent such failures in the future and avoid dividing a single big decimal parameter ad infinitum? One could use Checkstyle rules, use FindBugs rules or create a simple unit test.

I chose the unit test, so the question became “How can I find all the methods that call a given method in Java?” This is not possible with the standard Reflection API, but I had already used OW2′s ASM for such tasks, so it wasn’t too hard to create a JUnit assertion for it.

Just include this in your unit tests:

// Path to your project's '*.class' files
final File classesDir = new File("target/classes");
// Can be used to exclude some files/packages
final FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return !file.getPath().contains("my/pkg/to/exclude");
// Define methods to find
final MCAMethod divide = new MCAMethod("java.math.BigDecimal",
"java.math.BigDecimal divide(java.math.BigDecimal)");
final MCAMethod setScale = new MCAMethod("java.math.BigDecimal",
"java.math.BigDecimal setScale(int)");
// Fails if any class calls one of the two methods
AssertUsage.assertMethodsNotUsed(classesDir, fileFilter,
divide, setScale);

The full source code can be found at GitHub.

Published at DZone with permission of Michael Schnell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)