4

I'm facing an odd issue

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * Created by mklueh on 13/09/2022
 */
public class Reproducer {

    private static final String COMMA_SEPARATED_STRING = "2020-05-09,hello ,10     ,11.345 ,true    ,1000000000000";

    /**
     * Not working
     */
    @Nested
    public class WithReturn {

        public String withReturn(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
            return stream.map(s -> {
                             return Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
                                          .map(String::strip) //Comment out to make it compile
                                          .collect(Collectors.toList());
                         })
                         .flatMap(Collection::stream)
                         .collect(Collectors.joining(","));
        }

        @Test
        void usingDelimiter() {
            assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                    withReturn(Stream.of(COMMA_SEPARATED_STRING), ",", null));
        }

        @Test
        void usingTokenizer() {
            assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                    withReturn(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
        }
    }

    /**
     * Working
     */
    @Nested
    public class WithReturnAndStripLambda {

        public String withReturnAndStripLambda(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
            return stream.map(s -> {
                             return Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
                                          .map(s1 -> s1.strip())
                                          .collect(Collectors.toList());
                         })
                         .flatMap(Collection::stream)
                         .collect(Collectors.joining(","));
        }

        @Test
        void usingDelimiter() {
            assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                    withReturnAndStripLambda(Stream.of(COMMA_SEPARATED_STRING), ",", null));
        }

        @Test
        void usingTokenizer() {
            assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                    withReturnAndStripLambda(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
        }
    }


    /**
     * Working
     */
    @Nested
    public class WithMethodExpression {

        public String withMethodExpression(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
            return stream.map(s -> Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
                                         .map(String::strip)
                                         .collect(Collectors.toList())
                         )
                         .flatMap(Collection::stream)
                         .collect(Collectors.joining(","));

    }

    @Test
    void usingDelimiter() {
        assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                withMethodExpression(Stream.of(COMMA_SEPARATED_STRING), ",", null));
    }

    @Test
    void usingTokenizer() {
        assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
                withMethodExpression(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
    }

  }

}

invalid method reference

error: incompatible types: invalid method reference
                            .map(String::strip)
                                 ^
    method strip in class String cannot be applied to given types
      required: no arguments
      found: long
      reason: actual and formal argument lists differ in length

And the IntelliJ IDEA even recommends to perform the auto-refactoring to get rid of the lambda function in favor of the method reference

enter image description here

as well as replacing the return with expression lambda

enter image description here

What is wrong here and why does IntelliJ recommend to break code / not recognize that the recommendation is causing compiler errors?

13
  • 2
    Please provide a minimal reproducible example. I cannot reproduce the issue. Commented Sep 13, 2022 at 13:58
  • 1
    Your comment creates even more confusion. The line .stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter)) is entirely unrelated to the subsequent map call. As already said, you should post a minimal reproducible example. Commented Sep 13, 2022 at 18:58
  • 1
    But using if/else shouldn’t be necessary. Even imaginable scenarios like tokenizer.apply(s) returning a different type could not explain an error message about a long argument found when using this method reference. Having a workaround is not the same as solving (and hence understanding) the problem. Otherwise, you could have stayed with the lambda expression. Commented Sep 14, 2022 at 6:39
  • 1
    No, this linked question is about a numeric condition. This does not apply to expressions of type String[]. If you are able to reproduce the problem, it shouldn’t be so hard to provide a minimal reproducible example. I don’t understand why you don’t do this. Commented Sep 14, 2022 at 7:02
  • 1
    I believe there's no point in collecting into a List (with .collect(Collectors.toList())) and then again streaming over it (using .flatMap(Collection::stream)). Remove the toList() part and then use flatMap(Function.identity()), as you are already returning a Stream inside the previous step. Commented Sep 14, 2022 at 9:03

1 Answer 1

5

This seems very likely to be a bug in javac. What IntelliJ is suggesting should be a safe and functionally equivalent replacement.

Looks to be accounted for by JDK-8268312, which is marked as fixed in Java 20.


In case it's not, here's some other interesting behaviour I found when fiddling with your specific example:

In addition to failing for method references, it also fails when you explicitly specify the type in the lambda's parameter list as String, i.e. String::strip becomes (String sa) -> sa.trim()

Here's a more minimal example than yours. Note that I intentionally made both paths of the ternary within Arrays.stream do the exact same thing. I also used trim so this can be run on Java 8, as well as later JDKs.

Stream.of("A").flatMap(s -> {
    return Arrays.stream(true ? s.split(",") : s.split(","))
        .map((String sa) -> sa.trim());
})
.collect(Collectors.joining(","));

For some reason, it compiles fine if the flatMap argument is an expression lambda, rather than a statement lambda, which should not make a difference to the type system.

Stream.of("A").flatMap(s ->
    Arrays.stream(true ? s.split(",") : s.split(","))
        .map((String sa) -> sa.trim())
)
.collect(Collectors.joining(","));

It also works if you cast the result of the ternary to String[], even though that's already the type of that expression, meaning the cast should be redundant.

Stream.of("A").flatMap(s -> {
        return Arrays.stream((String[]) (true ? s.split(",") : s.split(",")))
            .map((String sa) -> sa.trim());
    })
    .collect(Collectors.joining(","));
Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly, this example compiles fine with JDK 8, whereas the bug report’s example stops compiling around update 60 of JDK 8. Even more interesting is that following the error message’s advice to compile again with -Xdiags:verbose to get more information, makes the error disappear. But it seems, you found the right bug report, though the rabbit hole might go far deeper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.