[Solved] Find out double arithmetic operations in my java code


Operations on doubles have their own bytecode instructions: dadd, ddiv, dmul, etc. So you can search for those operations in the class files for your project.

I’m sure there are tools to do that, so search for those.

But if you can’t find any suitable, it’s not too hard to build your own. On a compiled class, you can use javap -c ClassName to get a fairly human-readable version of the bytecode for the class. For instance, this code:

import java.util.Random;

public class Example {
    public static void main(String[] args) {
        Random r = new Random();
        double d1 = r.nextDouble();
        double d2 = r.nextDouble();
        double d3 = d1 + d2;
        System.out.println(d3);
    }
}

…results in this output from javap -c:

Compiled from "Example.java"
public class Example {
  public Example();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/util/Random
       3: dup
       4: invokespecial #3                  // Method java/util/Random."":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #4                  // Method java/util/Random.nextDouble:()D
      12: dstore_2
      13: aload_1
      14: invokevirtual #4                  // Method java/util/Random.nextDouble:()D
      17: dstore        4
      19: dload_2
      20: dload         4
      22: dadd
      23: dstore        6
      25: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
      28: dload         6
      30: invokevirtual #6                  // Method java/io/PrintStream.println:(D)V
      33: return
}

So you could write a program to:

  1. Get the javap -c output for each class

  2. Look in that output for double-related bytecode instructions (fairly simple regular expression work)

1

solved Find out double arithmetic operations in my java code