Finding Methods Calls Programmatically
Finding Methods Calls Programmatically
2013/10/01
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: 1BigDecimal.ONE.divide(BigDecimal.valueOf(3)); Every senior developer knows about this trap, but its 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 OW2s ASM for such tasks, so it wasnt too hard to create a JUnit assertion for it. Just include this in your unit tests: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3
// 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() { @Override 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);
1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1