Skip to content

Cleanup nullability issues #3142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.transport.Version;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.jspecify.annotations.NonNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
Expand Down Expand Up @@ -162,7 +163,7 @@ protected Mono<Boolean> doExists(String id, IndexCoordinates index) {
ExistsRequest existsRequest = requestConverter.documentExistsRequest(id, routingResolver.getRouting(), index);

return Mono.from(execute(
((ClientCallback<Publisher<BooleanResponse>>) client -> client.exists(existsRequest))))
((ClientCallback<@NonNull Publisher<BooleanResponse>>) client -> client.exists(existsRequest))))
.map(BooleanResponse::value) //
.onErrorReturn(NoSuchIndexException.class, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
Expand Down Expand Up @@ -369,7 +370,7 @@ public T populateIdIfNecessary(@Nullable Object id) {
* @see org.springframework.data.elasticsearch.core.EntityOperations.AdaptableEntity#initializeVersionProperty()
*/
@Override
public T initializeVersionProperty() {
public @NonNull T initializeVersionProperty() {
return map;
}

Expand All @@ -389,7 +390,7 @@ public boolean hasSeqNoPrimaryTerm() {
}

@Override
public SeqNoPrimaryTerm getSeqNoPrimaryTerm() {
public @Nullable SeqNoPrimaryTerm getSeqNoPrimaryTerm() {
return null;
}

Expand All @@ -398,7 +399,7 @@ public SeqNoPrimaryTerm getSeqNoPrimaryTerm() {
* @see org.springframework.data.elasticsearch.core.EntityOperations.AdaptableEntity#incrementVersion()
*/
@Override
public T incrementVersion() {
public @NonNull T incrementVersion() {
return map;
}

Expand All @@ -407,7 +408,7 @@ public T incrementVersion() {
* @see org.springframework.data.elasticsearch.core.EntityOperations.Entity#getBean()
*/
@Override
public T getBean() {
public @NonNull T getBean() {
return map;
}

Expand All @@ -425,12 +426,12 @@ public boolean isNew() {
* @see org.springframework.data.elasticsearch.core.EntityOperations.Entity#getPersistentEntity()
*/
@Override
public ElasticsearchPersistentEntity<?> getPersistentEntity() {
public @Nullable ElasticsearchPersistentEntity<?> getPersistentEntity() {
return null;
}

@Override
public String getRouting() {
public @Nullable String getRouting() {
return null;
}
}
Expand Down Expand Up @@ -650,7 +651,7 @@ public T incrementVersion() {
}

@Override
public String getRouting() {
public @Nullable String getRouting() {

String routing = routingResolver.getRouting(propertyAccessor.getBean());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ class ElasticsearchPropertyValueProvider implements PropertyValueProvider<Elasti
}

@Override
public <T> T getPropertyValue(ElasticsearchPersistentProperty property) {
public <T> @Nullable T getPropertyValue(ElasticsearchPersistentProperty property) {

String expression = property.getSpelExpression();
Object value = expression != null ? evaluator.evaluate(expression) : accessor.get(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public String[] getIndices() {
return indices;
}

@Nullable
public String[] getAliases() {
public String@Nullable[] getAliases() {
return aliases;
}

Expand Down Expand Up @@ -107,8 +106,8 @@ public Class<?> getFilterQueryClass() {
}

public static final class Builder {
@Nullable private String[] indices;
@Nullable private String[] aliases;
private String @Nullable [] indices;
private String @Nullable [] aliases;
@Nullable private Query filterQuery;
@Nullable private Class<?> filterQueryClass;
@Nullable private Boolean isHidden;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public List<AliasAction> getActions() {
public AliasActions add(@Nullable AliasAction... actions) {

if (actions != null) {
// noinspection NullableProblems
this.actions.addAll(Arrays.asList(actions));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Transient;
Expand Down Expand Up @@ -273,7 +274,7 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE
writeTypeHintMapping(propertiesNode);

if (entity != null) {
entity.doWithProperties((PropertyHandler<ElasticsearchPersistentProperty>) property -> {
entity.doWithProperties((PropertyHandler<@NonNull ElasticsearchPersistentProperty>) property -> {
try {
if (property.isAnnotationPresent(Transient.class) || isInIgnoreFields(property, parentFieldAnnotation)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static Builder builder() {
public static class Builder {

@Nullable private String name;
@Nullable private String[] indexPatterns;
private String @Nullable [] indexPatterns;
@Nullable private Settings settings;
@Nullable private Document mapping;
@Nullable AliasActions aliasActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static final class TemplateDataBuilder {
@Nullable Document mapping;
int order;
@Nullable Integer version;
@Nullable private String[] indexPatterns;
@Nullable private String@Nullable [] indexPatterns;
@Nullable private Map<String, AliasData> aliases;

private TemplateDataBuilder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexStoreType();

@Override
ElasticsearchPersistentProperty getVersionProperty();
@Nullable ElasticsearchPersistentProperty getVersionProperty();

@Nullable
String settingPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.InvalidDataAccessApiUsageException;
Expand Down Expand Up @@ -298,7 +299,7 @@ public ElasticsearchPersistentProperty getPersistentPropertyWithFieldName(String

return fieldNamePropertyCache.computeIfAbsent(fieldName, key -> {
AtomicReference<ElasticsearchPersistentProperty> propertyRef = new AtomicReference<>();
doWithProperties((PropertyHandler<ElasticsearchPersistentProperty>) property -> {
doWithProperties((PropertyHandler<@NonNull ElasticsearchPersistentProperty>) property -> {
if (key.equals(property.getFieldName())) {
propertyRef.set(property);
}
Expand Down Expand Up @@ -552,10 +553,10 @@ private static class SettingsParameter {
short replicas;
@Nullable String refreshIntervall;
@Nullable String indexStoreType;
@Nullable private String[] sortFields;
private Setting.@Nullable SortOrder[] sortOrders;
private Setting.@Nullable SortMode[] sortModes;
private Setting.@Nullable SortMissing[] sortMissingValues;
private String @Nullable [] sortFields;
private Setting.@Nullable SortOrder @Nullable [] sortOrders;
private Setting.@Nullable SortMode @Nullable [] sortModes;
private Setting.@Nullable SortMissing @Nullable [] sortMissingValues;

Settings toSettings() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public List<RescorerQuery> getRescorerQueries() {
/**
* @since 5.1
*/
public Integer getReactiveBatchSize() {
public @Nullable Integer getReactiveBatchSize() {
return reactiveBatchSize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ public String getNodeId() {
*
* @return a new {@link SearchFailureBuilder} to build {@link SearchFailure}
*/
public static SearchFailureBuilder builder() {
return new SearchFailureBuilder();
public static SearchFailureBuilder builder(Throwable reason) {
return new SearchFailureBuilder(reason);
}

/**
Expand All @@ -358,7 +358,9 @@ public static final class SearchFailureBuilder {
@Nullable private Integer shardId;
@Nullable private String nodeId;

private SearchFailureBuilder() {}
private SearchFailureBuilder(Throwable reason) {
this.reason = reason;
}

public SearchFailureBuilder withReason(Throwable reason) {
this.reason = reason;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public Boolean fetchSource() {
}

@Override
public String[] getIncludes() {
public @Nullable String[] getIncludes() {
return includes;
}

@Override
public String[] getExcludes() {
public @Nullable String[] getExcludes() {
return excludes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
public class FetchSourceFilterBuilder {

@Nullable private Boolean fetchSource;
@Nullable private String[] includes;
@Nullable private String[] excludes;
private String @Nullable [] includes;
private String @Nullable [] excludes;

public FetchSourceFilterBuilder withIncludes(String... includes) {
this.includes = includes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core.query;

import org.jspecify.annotations.Nullable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

Expand Down Expand Up @@ -92,7 +93,7 @@ public GeoDistanceOrder with(DistanceType distanceType) {
getIgnoreUnmapped());
}

public GeoDistanceOrder with(Mode mode) {
public GeoDistanceOrder with(@Nullable Mode mode) {
return new GeoDistanceOrder(getProperty(), getGeoPoint(), getDirection(), getDistanceType(), mode, getUnit(),
getIgnoreUnmapped());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public enum ScoreMode {

public static final class Builder {
private final String type;
private Query query;
@Nullable private Query query;

@Nullable private Boolean ignoreUnmapped;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public InnerHitsQuery getInnerHitsQuery() {

public static class Builder {
private final String parentType;
private Query query;
@Nullable private Query query;

@Nullable private Boolean score;
@Nullable private Boolean ignoreUnmapped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public interface SourceFilter {
/**
* @return the name of the fields to include in a response.
*/
@Nullable
String[] getIncludes();

String@Nullable[] getIncludes();

/**
* @return the names of the fields to exclude from a response.
*/
@Nullable
String[] getExcludes();

String@Nullable[] getExcludes();

/**
* Flag to set the _source parameter in a query to true or false. If this is not null, the values returned from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public ScriptData getScriptData() {
}

public static final class Builder {
private String id;
private String id = "";
@Nullable private String script = null;
@Nullable private Map<String, Object> params;
@Nullable private Document document = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.jspecify.annotations.Nullable;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.query.BaseQuery;
import org.springframework.data.elasticsearch.core.query.SearchTemplateQuery;
Expand All @@ -33,7 +34,7 @@
public class ReactiveRepositorySearchTemplateQuery extends AbstractReactiveElasticsearchRepositoryQuery {

private String id;
private Map<String, Object> params;
private Map<String, Object> params = Map.of();

public ReactiveRepositorySearchTemplateQuery(ReactiveElasticsearchQueryMethod queryMethod,
ReactiveElasticsearchOperations elasticsearchOperations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class RepositorySearchTemplateQuery extends AbstractElasticsearchRepositoryQuery {

private String id;
private Map<String, Object> params;
private Map<String, Object> params = Map.of();

public RepositorySearchTemplateQuery(ElasticsearchQueryMethod queryMethod,
ElasticsearchOperations elasticsearchOperations, ValueExpressionDelegate valueExpressionDelegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private Criteria applyPropertySpecs(Criteria criteria, String path, @Nullable Ob

Object propertyValue = propertyAccessor.getProperty(property);
if (property.isMap() && propertyValue != null) {
for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) {
for (Map.Entry<String, @Nullable Object> entry : ((Map<String, @Nullable Object>) propertyValue).entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
criteria = applyPropertySpec(propertyPath + "." + key, value, exampleSpecAccessor, property, matchMode,
Expand All @@ -96,7 +96,7 @@ private Criteria applyPropertySpecs(Criteria criteria, String path, @Nullable Ob
return criteria;
}

private Criteria applyPropertySpec(String path, Object propertyValue, ExampleMatcherAccessor exampleSpecAccessor,
private Criteria applyPropertySpec(String path, @Nullable Object propertyValue, ExampleMatcherAccessor exampleSpecAccessor,
ElasticsearchPersistentProperty property, ExampleMatcher.MatchMode matchMode, Criteria criteria) {

if (exampleSpecAccessor.isIgnoreCaseForPath(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public QueryByExampleElasticsearchExecutor(ElasticsearchOperations operations) {

@Override
public <S extends T> Optional<S> findOne(Example<S> example) {
CriteriaQuery criteriaQuery = CriteriaQuery.builder(exampleCriteriaMapper.criteria(example)).withMaxResults(2).build();
CriteriaQuery criteriaQuery = CriteriaQuery.builder(exampleCriteriaMapper.criteria(example)).withMaxResults(2)
.build();
SearchHits<S> searchHits = operations.search(criteriaQuery, example.getProbeType(),
operations.getIndexCoordinatesFor(example.getProbeType()));
if (searchHits.getTotalHits() > 1) {
throw new org.springframework.dao.IncorrectResultSizeDataAccessException(1);
}
return Optional.ofNullable(searchHits).filter(SearchHits::hasSearchHits)
return Optional.of(searchHits).filter(SearchHits::hasSearchHits)
.map(result -> (List<S>) SearchHitSupport.unwrapSearchHits(result)).map(s -> s.get(0));
}

Expand Down
Loading