Polishing.

Reduce method visibility. Improve method naming. Avoid using var in production code.

See #2857
Original pull request #2859
This commit is contained in:
Mark Paluch
2023-03-15 15:23:28 +01:00
parent b3943a6e50
commit 03b3516f33
4 changed files with 19 additions and 24 deletions

View File

@@ -17,22 +17,20 @@ package org.springframework.data.jpa.provider;
import jakarta.persistence.EntityManager;
import java.util.Date;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.TypedParameterValue;
import org.hibernate.type.BasicType;
import org.hibernate.type.BasicTypeRegistry;
import org.springframework.data.jpa.repository.query.JpaParametersParameterAccessor;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* {@link org.springframework.data.repository.query.ParameterAccessor} based on an {@link Parameters} instance. In
* addition to the {@link JpaParametersParameterAccessor} functions, the bindable value is provided by fetching the
* method type when there is null.
* addition to the {@link JpaParametersParameterAccessor} functions, the bindable parameterValue is provided by fetching
* the method type when there is null.
*
* @author Wonchul Heo
* @author Jens Schauder
@@ -68,13 +66,13 @@ class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAcce
@SuppressWarnings("unchecked")
public Object getValue(Parameter parameter) {
var value = super.getValue(parameter.getIndex());
Object value = super.getValue(parameter.getIndex());
if (value != null) {
return value;
}
var type = typeHelper.getRegisteredType(parameter.getType());
BasicType<?> type = typeHelper.getRegisteredType(parameter.getType());
if (type == null) {
return null;
@@ -84,19 +82,18 @@ class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAcce
}
/**
* For Hibernate, check if the incoming value is wrapped inside a {@link TypedParameterValue} before extracting and
* casting the {@link Date}.
* For Hibernate, check if the incoming parameterValue can be wrapped inside a {@link TypedParameterValue} before
* extracting.
*
* @param value a value that is either a {@link Date} or a {@link TypedParameterValue} containing a {@literal Date}.
* @param parameterValue a parameterValue that is either a plain value or a {@link TypedParameterValue} containing a
* {@literal Date}.
* @since 3.0.4
*/
@Override
public Date unwrapDate(Object value) {
protected Object potentiallyUnwrap(Object parameterValue) {
Object extracted = (value instanceof TypedParameterValue<?> typedParameterValue) //
return (parameterValue instanceof TypedParameterValue<?> typedParameterValue) //
? typedParameterValue.getValue() //
: value;
return (Date) extracted;
: parameterValue;
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.data.jpa.repository.query;
import java.util.Date;
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
@@ -54,13 +52,13 @@ public class JpaParametersParameterAccessor extends ParametersParameterAccessor
}
/**
* For general JPA providers, simply pass through the extracted value, casting it as a {@link Date}.
* Apply potential unwrapping to {@code parameterValue}.
*
* @param extractedValue
* @since 3.1
* @param parameterValue
* @since 3.0.4
*/
public Date unwrapDate(Object extractedValue) {
return (Date) extractedValue;
protected Object potentiallyUnwrap(Object parameterValue) {
return parameterValue;
}
}

View File

@@ -83,7 +83,7 @@ interface QueryParameterSetter {
Object extractedValue = valueExtractor.apply(accessor);
final Date value = accessor.unwrapDate(extractedValue);
Date value = (Date) accessor.potentiallyUnwrap(extractedValue);
// One would think we can simply use parameter to identify the parameter we want to set.
// But that does not work with list valued parameters. At least Hibernate tries to bind them by name.

View File

@@ -68,7 +68,7 @@ class NamedOrIndexedQueryParameterSetterUnitTests {
Date testDate = new Date();
when(accessor.getValues()).thenReturn(new Object[] { testDate });
when(accessor.unwrapDate(testDate)).thenReturn(testDate);
when(accessor.potentiallyUnwrap(testDate)).thenReturn(testDate);
this.methodArguments = accessor;
}