0

The class A declares a constant C as follows:

public class A {
    public static final int C = 10;
}

This constant is used in method M of class B as follows:

public class B  {
    public int M() {
        return A.C;
    }
}

In the bytecodes of class B, the method M looks like this:

public other()I
 L0
  LINENUMBER 29 L0
  BIPUSH 10
  IRETURN
 L1
  LOCALVARIABLE this Lcalc/B; L0 L1 0
  MAXSTACK = 1
  MAXLOCALS = 1

So, there is no reference to class A anywhere in the bytecode because the Java compiler optimizes constant expressions at compile time (look this related question).

How to avoid this phenomenon in order to collect the usage of class A by class B?

cesarsotovalero
  • 1,116
  • 6
  • 15
  • 1
    Does this answer your question? [is it possible to disable javac's inlining of static final variables?](https://stackoverflow.com/q/3524150/5221149) – Andreas Apr 16 '20 at 22:07
  • No, it doesn’t. I need to do the usage analysis at the bytecode level. I already implemented the trick of changing the declaration of `C` in the class `A` and put it inside a static construct ([here’s how](https://stackoverflow.com/questions/61167940/how-to-transform-bytecodes-to-initialize-primitive-constants-in-static-block-wit)), as suggested by rveach [here](https://stackoverflow.com/questions/3524150/is-it-possible-to-disable-javacs-inlining-of-static-final-variables). The problem is that it doesn’t change the call in class `B` since the value is “optimized” at compile time. – cesarsotovalero Apr 16 '20 at 22:22
  • 5
    There is no way to recreate absent information. Since only the source code contains the information, there is no way around getting it from the source code, like recompiling the sources. – Holger Apr 17 '20 at 16:24

0 Answers0