Home » Java » java – Sonal lint : change this condition so that it does not always evaluate to true-Exceptionshub

java – Sonal lint : change this condition so that it does not always evaluate to true-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to consume Data JPA Repository from From Java Service which is something like below:

@Service
public class APIKeyServiceImpl implements APIKeyService {

    private APIKeyRepo apiKeyRepo;

    @Autowired
    public APIKeyServiceImpl(APIKeyRepo apiKeyRepo) {
        this.apiKeyRepo = apiKeyRepo;
    }


    @Override
    public String save(String apiKeyInput) {

        APIKEY apikey = new APIKEY();
        apikey.setApikey(apiKeyInput);

        APIKEY savedKey = apiKeyRepo.save(apikey);

        return null != savedKey ? savedKey.getApikey() : null;
    }

}

Now Short Repo Code is below:

public interface APIKeyRepo extends JpaRepository<APIKEY, String> {

}

Problem is whenever I run Sonarlint Report then I am getting below major issue:

change this condition so that it does not always evaluate to true

Which is occurring at ternary operator code which is :

return null != savedKey ? savedKey.getApikey() : null;

I havn’t much understand which part of this will always evaluate true.

How to&Answers:

Documentation

As you save an entity save method will return you the saved entity, which can never be null.

Source: Method Doc.