Apologies if this is painfully obvious to everyone in this thread, but
I want to explicitly draw attention to what Anthony is saying. Among
other things, he is saying that Path.endsWith(String, String...) is
NOT equivalent to Path.endsWith(Path.of(String, String...)), which
people will be tempted to use without reading the javadoc or IDE
hand-holding.

The reason it's not equivalent is that two paths -- the receiver and
the argument -- might be incompatible. That is, they might be produced
by two different file systems. If paths in Path.endsWith(Path) are
from different file systems, it's extremely likely to be a bug. And
the way to maximise the odds of this bug is to construct the argument
as Path.of(String, String...).

Knowing the main author of this API a bit, this String-taking method
wasn't introduced willy-nilly. It was likely introduced specifically
to eliminate the described hazard. However, it looks like the method
backfired in a really unexpected way. I guess no amount of thinking,
even by a very smart person, can foresee all real-life issues.

-Pavel

On Wed, Jan 14, 2026 at 10:01 AM Anthony Vanelverdinghe
<[email protected]> wrote:
>
> On 1/14/2026 8:15 AM, Alan Bateman wrote:
> > On 13/01/2026 20:13, Anthony Vanelverdinghe wrote:
> >> There are 3 questions:
> >>
> >> (1) should we deprecate `Path::startsWith(String)`?
> >> (2) should we deprecate `Path::endsWith(String)`?
> >> (3) should we add a file extension API?
> >>
> > The "plan" is to deprecate startsWith(String) and endsWith(String),
> > and to reboot the effort to add the file extension API.
> >
> > -Alan
>
>
> Just for the record, emptying my bag of arguments:
>
> Google's Error Prone has a whole catalog of bug patterns, including
> confusing Java SE APIs. Shall we deprecate all of those as well then?
> And note that it does *not* have a bug pattern for
> `Path::{starts,ends}With`. So nobody at Google ever bothered to add it.
> And nobody outside Google ever bothered to file an issue to add it.
> (Same for SonarQube, SpotBugs, or FindBugs: no pattern for this.)
>
> Virtually every Java developer has made the mistake of using
> `Thread.sleep(5)` to mean "sleep 5 seconds" (which is the behavior in
> pretty much every shell). So why are we not deprecating that method then?
>
> Even `String::endsWith(String)` itself is confusing, since some people
> assume its argument is a regex (as with some other String methods like
> `matches`) [1]. So why not e.g., replace `String::matches(String)` with
> `String::matches(Pattern)`?
>
> [1]
> https://stackoverflow.com/questions/9943609/checking-if-string-ends-with-something-with-java-regex
>
> Anthony
>

Reply via email to