Has that method been ever used? If nothing else its name seems strange. To me, a union has OR semantics, not AND.
> On 27 Aug 2021, at 15:37, Andrey Turbanov <turban...@gmail.com> wrote: > > Hello. > I found suspicious code in the method > "java.util.regex.Pattern.BmpCharPredicate#union(java.util.regex.Pattern.CharPredicate...)" > https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/regex/Pattern.java#L5639 > > static CharPredicate union(CharPredicate... predicates) { > CharPredicate cp = ch -> { > for (CharPredicate p : predicates) { > if (!p.is(ch)) > return false; > } > return true; > }; > for (CharPredicate p : predicates) { > if (! (p instanceof BmpCharPredicate)) > return cp; > } > return (BmpCharPredicate)cp; > } > > Variable `cp` has type CharPredicate initially. And then it's casted > to BmpCharPredicate. This cast always fails with ClassCastException > when reached. > > Can reproduced in small sample class: > > public static void main(String[] args) { > CharPredicate result = BmpCharPredicate.union(); > System.out.println(result); > } > > interface CharPredicate { > boolean is(int ch); > } > > interface BmpCharPredicate extends CharPredicate { > static CharPredicate union(CharPredicate... predicates) { > CharPredicate cp = ch -> true; > for (CharPredicate p : predicates) { > if (! (p instanceof BmpCharPredicate)) > return cp; > } > return (BmpCharPredicate)cp; > } > } > > > Exception in thread "main" java.lang.ClassCastException: class > org.RegexpBug$BmpCharPredicate$$Lambda$14/0x0000000800c028f0 cannot be > cast to class org.RegexpBug$BmpCharPredicate > (org.RegexpBug$BmpCharPredicate$$Lambda$14/0x0000000800c028f0 and > org.RegexpBug$BmpCharPredicate are in unnamed module of loader 'app') > at org.RegexpBug$BmpCharPredicate.union(RegexpBug.java:20) > at org.RegexpBug.main(RegexpBug.java:5) > > As I can see this method is never used. Perhaps it should be removed? > > > Andrey Turbanov