How to use RequiresNonNull with method

47 views Asked by At

Using the Checker Framework and given this getter

    private @MonotonicNonNull OffsetDateTime expiration;

    @Pure
    @SuppressWarnings("monotonic")
    public @MonotonicNonNull OffsetDateTime getExpiration() {
        return expiration;
    }

I want to get this to compile without errors

    @RequiresNonNull("getExpiration()")
    public long getExpiresIn() {
        return OffsetDateTime.now().until(getExpiration(), ChronoUnit.SECONDS);
    }

But I get

error: [argument] incompatible argument for parameter obj of requireNonNull.
found : @Initialized @MonotonicNonNull OffsetDateTime

The weird thing is that the same works when I use the field directly, but I dont like it because it's not really object-oriented to expose the field in the contract:

    @RequiresNonNull("expiration")
    public long getExpiresIn() {
        return OffsetDateTime.now().until(expiration, ChronoUnit.SECONDS);
    }

Even throwing in an explicit assert doesnt work.

    @RequiresNonNull("getExpiration()")
    public long getExpiresIn() {
        assert getExpiration() != null : "expiration required";
        return OffsetDateTime.now().until(getExpiration(), ChronoUnit.SECONDS);
    }

What is the best way to annotate the methods?

0

There are 0 answers