spring-projects / spring-projects/spring-data-redis

Simplify afterPropertiesSet() Method in `RedisTemplate` Using Objects.requireNonNullElse

Open
#3,003 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: declined
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

The afterPropertiesSet() method currently contains multiple null checks and assignments that can be simplified. By utilizing Java 9’s Objects.requireNonNullElse and Objects.requireNonNullElseGet methods, we can make the code more concise, readable, and maintainable.

AS-IS

public void afterPropertiesSet() {
    super.afterPropertiesSet();

    if (defaultSerializer == null) {
        defaultSerializer = new JdkSerializationRedisSerializer(
            classLoader != null ? classLoader : this.getClass().getClassLoader());
    }

    if (enableDefaultSerializer) {
        if (keySerializer == null) {
            keySerializer = defaultSerializer;
        }
        if (valueSerializer == null) {
            valueSerializer = defaultSerializer;
        }
        if (hashKeySerializer == null) {
            hashKeySerializer = defaultSerializer;
        }
        if (hashValueSerializer == null) {
            hashValueSerializer = defaultSerializer;
        }
    }

    if (scriptExecutor == null) {
        this.scriptExecutor = new DefaultScriptExecutor<>(this);
    }

    initialized = true;
}

TO-BE

public void afterPropertiesSet() {
    super.afterPropertiesSet();

    defaultSerializer = Objects.requireNonNullElseGet(
        defaultSerializer,
        () -> new JdkSerializationRedisSerializer(
            Objects.requireNonNullElse(classLoader, getClass().getClassLoader()))
    );

    if (enableDefaultSerializer) {
        keySerializer = Objects.requireNonNullElse(keySerializer, defaultSerializer);
        valueSerializer = Objects.requireNonNullElse(valueSerializer, defaultSerializer);
        hashKeySerializer = Objects.requireNonNullElse(hashKeySerializer, defaultSerializer);
        hashValueSerializer = Objects.requireNonNullElse(hashValueSerializer, defaultSerializer);
    }

    scriptExecutor = Objects.requireNonNullElseGet(
        scriptExecutor,
        () -> new DefaultScriptExecutor<>(this)
    );

    initialized = true;
}
}

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 at RedisTemplate.afterPropertiesSet() and compare its current null-handling branches with the proposed Objects-based form. Check the surrounding imports and relevant existing tests, then verify that initialization and serializer defaults remain unchanged while the method becomes more concise.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis
Domain
databases
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.