I am trying to do a source transformation on some Java code that results in code where for every expression, a method is called if the expression is evaluated.
(The use case is a simplistic line coverage measure. I've done this sort of thing before in JavaScript: <my-expression>
becomes (covered("path/to/file.js", 12), <my-expression>)
or something, where 12 is the line number of the expression).
Java doesn't have a comma operator. I thought about wrapping expressions in a method call, e.g. my covered
function would be declared public static <T> T covered(String file, int line, T expr)
and return its third argument, so I could write covered("path/to/file.java", 12, myExpression())
but it doesn't work for expressions that have type void
.
Is there an easy way to accomplish this? Evil code is okay; this is generated code.
(I see the problem, now.)
The only context in Java where a void expression can legally occur is when it is a statement expression, or the 1st or 3rd part in a classic for
statement. So:
If the expression is used as an expression statement:
covered(...); <my-expression>;
If the expression is used as the 1st or 3rd part of a for
,
covered(...), <my-expression>
Otherwise
covered(..., <my-expression>)
or some such. (This requires an overload of covered
for each primitive type, and also an overload with signature <T> overload(..., <T>)
.)
I think this can be determined purely based on the syntax. No type analysis or method overload resolution is required to figure out whether a void method is actually being called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With