Repository navigation
Runtime initialize non-constant static finals - #474
Conversation
|
@bjkail to confirm my understanding of
you are doing something like this condition is always false since |
|
We have a client library that uses JaCoCo, but clients need to be able to choose their own JaCoCo version. We store execution data on a per-format folder basis, and the client library uses |
|
Okay, so very similar to my example: due to inlining client library can claim that uses older format, while runtime dependency changed and it actually uses new format. |
|
|
||
| static { | ||
| FORMAT_VERSION = 0x1007; | ||
| } |
|
|
||
| /** ASM API version */ | ||
| public static final int ASM_API_VERSION = Opcodes.ASM5; | ||
| public static final int ASM_API_VERSION; |
There was a problem hiding this comment.
I think we should add a comment to prevent accidental change back.
|
|
||
| /** File format version, will be incremented for each incompatible change. */ | ||
| public static final char FORMAT_VERSION = 0x1007; | ||
| public static final char FORMAT_VERSION; |
There was a problem hiding this comment.
I added comment here and in JaCoCo. I wrote a test that uses TargetLoader to load JaCoCo and a test class that accesses JaCoCo.ASM_API_VERSION, and it uses ASM to rewrite JaCoCo to assign a different value. It seemed like overkill, but I can do it if you prefer.
There was a problem hiding this comment.
Yes - this seems like an overkill. Just comment looks fine and enough for me.
| buffer.write(0xC0); | ||
| buffer.write(0xC0); | ||
| final char version = ExecutionDataWriter.FORMAT_VERSION - 1; | ||
| final char version = (char) (ExecutionDataWriter.FORMAT_VERSION - 1); |
There was a problem hiding this comment.
Interesting that explicit cast not needed in case of inlined value. Learned something new today 😆
There was a problem hiding this comment.
I also learned this today :-). I guess constant fields are treated like literal constants in all regards.
There was a problem hiding this comment.
There are implicit casts: char -> int -> subtraction , in first case value after subtraction is known at compile time and so one more safe implicit cast int -> char. In second case value not known and so javac says "possible lossy conversion from int to char".
2f9a1b6 to
b8da5e6
Compare
|
I believe I fixed the issues, thanks. Sorry for the sloppiness with the author/committer email and file modes. |
|
To make it more explicit that this is not a constant value I would prefer a static method instead: This breaks the API but people using it currently ended up with broken code anyways. |
| public static final String RUNTIMEPACKAGE; | ||
|
|
||
| /** ASM API version */ | ||
| public static final int ASM_API_VERSION = Opcodes.ASM5; |
There was a problem hiding this comment.
This should only get compiled into JaCoCo classes and not be used externally. For internal classes inlining is perfectly ok because the compiled version of JaCoCo has specifically been tested for this ASM version.
Instead of changing the initialization scheme here I would rather remove it from the API and move it to some internal classes (e.g. InstrSupport).
There was a problem hiding this comment.
I've added a separate commit to move ASM_API_VERSION to InstrSupport.
b8da5e6 to
c4eb7e3
Compare
|
@marchof My code uses |
|
@bjkail Many thanks for moving I see your point about compatibility: Another option would be to leave But I can also live with your PR as is. |
|
@marchof As soon as a JaCoCo is released with non-constant |
c4eb7e3 to
be6ba14
Compare
If a constant expression is assigned to a static final variable, javac will inline the constant value in the caller, which means constants should only be used for values that will never change. Otherwise, calling code must be recompiled when upgrading to a new version of the library even if the caller would otherwise not require any changes.
be6ba14 to
ba80c4e
Compare
|
@bjkail Not sure I fully understand why "more work". But in any case let's keep this as a field. @marchof other options seems overkill to me. I updated changelog, will merge after pass of builds. @bjkail A kind of off topic: since you support multiple versions, may I ask out of curiosity - down to which one? Especially that we talk about version of format that last time was changed in 0.7.5 (2015/05/24)? |
|
@Godin By "more work" I meant that I would have to update the current reflection to look for both I know 0.7.4 is still heavily used (the most recent for 0x1006), but I've seen older versions in use too. I have not encountered 0.4.x (0x1005) or older. Older versions are used both due to risk aversion and because upgrading creates a "flag day" for components that want to merge/intersect their execution data (if one component moves then they all need to move). |


In my case,
ExecutionDataWriter.FORMAT_VERSIONis the problematic constant. We workaround by using reflection to access the field so that javac won't inline the value from the version of the library we use to compile.I reviewed the
org.jacoco.doc/target/apidocs/constant-values.html, andJaCoCo.ASM_API_VERSIONseemed like the only other constant that was likely to cause similar problems for anyone.