1

Here's my basic example setup:

private final RedisTemplate<String, Object> redisTemplate;
public void save(Object thing, Instant expiresAt) {
 redisTemplate.execute(new SessionCallback<Void>() {
 @Override
 public @Nullable Void execute(@NonNull RedisOperations ops) throws DataAccessException {
 ops.opsForValue("key", thing);
 ops.expireAt("key", expiresAt);
 return null;
 }
 });
}

That version compiles, but has multiple warnings for

Unchecked call to ... as a member of raw type ...

These attempts all fail to compile:

redisTemplate.execute((SessionCallback<Void>) ops -> {
 ops.opsForValue("key", thing);
 ops.expireAt("key", expiresAt);
 return null;
});

Target method is generic

redisTemplate.execute(new SessionCallback<Void>() {
 @Override
 public @Nullable Void execute(@NonNull RedisOperations<String, Object> ops) throws DataAccessException {
 ops.opsForValue("key", thing);
 ops.expireAt("key", expiresAt);
 return null;
 }
});

Class 'Anonymous class derived from SessionCallback' must implement abstract method 'execute(RedisOperations<K, V>)' in 'SessionCallback'

redisTemplate.execute(new SessionCallback<Void>() {
 @Override
 public <K, V> @Nullable Void execute(@NonNull RedisOperations<K, V> ops) throws DataAccessException {
 ops.opsForValue("key", thing);
 ops.expireAt("key", expiresAt);
 return null;
 }
});

Required type 'T', provided 'String'
Cannot resolve method 'expireAt(String, Instant)'

And finally, compiling with warnings:

redisTemplate.execute(new SessionCallback<Void>() {
 @Override
 public <K, V> @Nullable Void execute(@NonNull RedisOperations<K, V> ops) throws DataAccessException {
 ops.opsForValue((K) "key", (V) thing);
 ops.expireAt((K) "key", (V) expiresAt);
 return null;
 }
});

Unchecked cast: 'java.lang.String' to 'K'

Is it actually possible to use this API without type warnings or errors?

asked Apr 14 at 12:35
2

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.