Skip to content

RemoveRedundantTypeCast removes cast needed to disambiguate overloaded method (Object... vs Throwable) #899

@protocol7

Description

@protocol7

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:

log("value: {}", value);

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions