Over a million developers have joined DZone.

Finding Methods Calls Programmatically

DZone's Guide to

Finding Methods Calls Programmatically

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

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.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}