Skip to content

Fix Java unchecked warnings in stream testkit fluent API (#2625)#2725

Merged
He-Pin merged 1 commit intomainfrom
testkitWarning
Mar 15, 2026
Merged

Fix Java unchecked warnings in stream testkit fluent API (#2625)#2725
He-Pin merged 1 commit intomainfrom
testkitWarning

Conversation

@He-Pin
Copy link
Copy Markdown
Member

@He-Pin He-Pin commented Mar 13, 2026

Motivation

When Java code uses the stream testkit fluent API (e.g., TestSink.create(system), probe.expectNext()), the compiler emits unchecked warnings because Scala's abstract type member type Self <: ManualProbe[I] erases to a raw ManualProbe type in bytecode — no generic Signature attribute is emitted. Java reads these bytecode signatures and sees raw types, triggering -Xlint:unchecked warnings on every chained call.

See: #2625

Modification

Root-cause fix using this.type — Replace the abstract type member pattern with Scala's singleton this.type return type:

  1. Remove type Self abstract type members from TestPublisher.ManualProbe, TestPublisher.Probe, TestSubscriber.ManualProbe, and TestSubscriber.Probe
  2. Change all fluent method return types from Self to this.type — this makes the Scala compiler emit parameterized generic signatures in bytecode
  3. Replace self references (this.asInstanceOf[Self]) with plain this
  4. Add MiMa exclusion filters for IncompatibleSignatureProblem entries in both ManualProbe and Probe classes (only generic Signature metadata changes — erased JVM types are unchanged)
  5. Fix ScalaDoc links: TestSubscriberTestSubscriber.Probe, TestPublisherTestPublisher.Probe
  6. Fix typo: "JAVA PAI" → "JAVA API"

Why this.type works

  • JVM erased return type: Still ManualProbe/Probe (unchanged — binary compatible)
  • Generic Signature attribute: Changes to properly parameterized types
  • MiMa safe: Added exclusion filters for IncompatibleSignatureProblem — these are Signature-only changes, erased types are identical
  • Java compiler: Reads Signature attributes for generic info → sees proper types → zero warnings

Why this is better than method overrides

The previous approach required 27+ boilerplate override methods in Probe classes to narrow return types. This fix addresses the root cause in the base class itself — zero overrides needed, and any future fluent methods added to ManualProbe will automatically have correct generic signatures.

Result

  • Zero unchecked warnings when compiling Java test code with -Xlint:unchecked
  • Binary compatible (erased types unchanged, MiMa filters added for both Scala 2.13 and Scala 3)
  • ✅ All 25 stream-testkit tests pass
  • ✅ All downstream Java test modules compile cleanly
  • ✅ MiMa binary compatibility check passes

References

@He-Pin He-Pin changed the title Fix Java compiler warnings for unchecked calls in TestSink/TestSource Fix Java unchecked warnings in stream testkit fluent API Mar 13, 2026
@He-Pin He-Pin changed the title Fix Java unchecked warnings in stream testkit fluent API Fix Java unchecked warnings in stream testkit fluent API (#2625) Mar 13, 2026
Use Scala's `this.type` return type instead of abstract type member `Self`
to fix raw type erasure in bytecode signatures. This is a root-cause fix
that eliminates unchecked warnings in Java without any boilerplate overrides.

Root cause: `type Self <: ManualProbe[I]` erased to raw `ManualProbe` in
bytecode (no generic Signature attribute), causing Java to see raw types.
`this.type` makes the Scala compiler emit `ManualProbe<TI;>` (parameterized)
in the bytecode Signature attribute, so Java sees proper generic types.

Changes:
- Remove `type Self` abstract type members from ManualProbe/Probe classes
- Change all fluent method return types from `Self` to `this.type`
- Replace `self` references with `this`
- Fix ScalaDoc links: TestSubscriber → TestSubscriber.Probe, TestPublisher → TestPublisher.Probe
- Fix typo: "JAVA PAI" → "JAVA API"

Binary compatible: erased JVM return types unchanged (MiMa safe).
Only generic Signature attribute metadata changes.
@He-Pin He-Pin marked this pull request as ready for review March 13, 2026 18:40
Copy link
Copy Markdown
Member

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@He-Pin He-Pin merged commit 722b195 into main Mar 15, 2026
9 checks passed
@He-Pin He-Pin deleted the testkitWarning branch March 15, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants