typetools / typetools/checker-framework

Object::getClass method reference resulting in methodref.return.invalid

Open
#1,440 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

This may be down to my ignorance of how generics work. But at first sight I think this code isn't wrong:


import java.util.function.Function;

import org.checkerframework.checker.nullness.qual.Nullable;

public class Packet<T> {

    public static <T> Packet<T> get(T object) {
        return new Packet<>(object);
    }

    private final @Nullable T value;

    private Packet(@Nullable T value) {
        this.value = value;
    }

    public <U> @Nullable U map(Function<T, @Nullable U> mappingFunction) {
        if (value == null) {
            return (U) null;
        }
        return mappingFunction.apply(value);
    }

    public static void main(String[] args) {
        Object obj = 2.14;
        System.out.println("class: "
                + Packet.get(obj).map(Object::getClass)
        );
    }
}

Yet it produces the following:

Packet.java:28: warning: [methodref.return.invalid] Incompatible return type
                + Packet.get(obj).map(Object::getClass)
                                      ^
  Method
    @Initialized @NonNull Class<? extends @Initialized @Nullable Object> getClass(@UnknownInitialization @NonNull Object this) in java.lang.Object
  is not a valid method reference for
    @Initialized @Nullable Class<? extends @Initialized @NonNull Object> apply(@Initialized @NonNull Function<@Initialized @NonNull Object, @Initialized @Nullable Class<? extends @Initialized @NonNull Object>> this, @Initialized @NonNull Object p0) in java.util.function.Function
  found   : @Initialized @NonNull Class<? extends @Initialized @Nullable Object>
  required: @Initialized @Nullable Class<? extends @Initialized @NonNull Object>

What does this mean?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the warning from the Packet.java example with the nullness checker, then trace the handling of Object::getClass method references and the reported generic return types. Compare the inferred nullable annotations with the Function target type; done means explaining whether the diagnostic is valid or identifying the checker behavior that needs correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.