What version of OpenRewrite are you using?
- rewrite-static-analysis 2.34.1
- rewrite-core 8.81.7
- rewrite-java 8.81.7
How are you running OpenRewrite?
Via a custom recipe list that includes org.openrewrite.staticanalysis.RemoveRedundantTypeCast.
What is the smallest, simplest way to reproduce the problem?
RemoveRedundantTypeCast removes a cast that is required to disambiguate between two
overloaded methods. When a method has both a varargs Object... overload and a
Throwable overload (the classic SLF4J Logger shape), an explicit (Object) cast on
a single argument is what selects the varargs overload. The recipe treats the cast as
redundant and removes it, which makes the call ambiguous and no longer compiles.
class Test {
void log(String format, Object... args) {}
void log(String format, Throwable t) {}
void run(Object value) {
log("value: {}", (Object) value);
}
}
What did you expect to see?
No change. The (Object) cast is not redundant: it disambiguates the call per
JLS §15.12.2. Without it the compiler cannot choose between log(String, Object...)
and log(String, Throwable) when value is a reference type. This is the exact pattern
used to force SLF4J to treat a Throwable argument as a regular format parameter:
LOGGER.error("msg: {}", (Object) throwable).
What did you see instead?
The cast was removed, producing:
which fails to compile:
error: reference to log is ambiguous
log("value: {}", value);
^
both method log(String,Object...) in Test
and method log(String,Throwable) in Test match
What is the full stack trace of any errors?
No stack trace from the recipe itself (it applies cleanly); the failure surfaces as a
downstream Java compilation error after the rewrite is applied. Real-world example:
GraphQLWiring.java:177: error: reference to error is ambiguous
(Object) env.getObject());
^
both method error(String,Object...) in Logger
and method error(String,Throwable) in Logger match
What version of OpenRewrite are you using?
How are you running OpenRewrite?
Via a custom recipe list that includes
org.openrewrite.staticanalysis.RemoveRedundantTypeCast.What is the smallest, simplest way to reproduce the problem?
RemoveRedundantTypeCastremoves a cast that is required to disambiguate between twooverloaded methods. When a method has both a varargs
Object...overload and aThrowableoverload (the classic SLF4JLoggershape), an explicit(Object)cast ona single argument is what selects the varargs overload. The recipe treats the cast as
redundant and removes it, which makes the call ambiguous and no longer compiles.
What did you expect to see?
No change. The
(Object)cast is not redundant: it disambiguates the call perJLS §15.12.2. Without it the compiler cannot choose between
log(String, Object...)and
log(String, Throwable)whenvalueis a reference type. This is the exact patternused to force SLF4J to treat a
Throwableargument as a regular format parameter:LOGGER.error("msg: {}", (Object) throwable).What did you see instead?
The cast was removed, producing:
which fails to compile:
What is the full stack trace of any errors?
No stack trace from the recipe itself (it applies cleanly); the failure surfaces as a
downstream Java compilation error after the rewrite is applied. Real-world example: