Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reduce method visibility. Improve method naming. Avoid using var in production code.

See #2857
Original pull request #2859
  • Loading branch information
mp911de committed Mar 15, 2023
1 parent b3943a6 commit 03b3516
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@

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
Expand Down Expand Up @@ -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;
Expand All @@ -84,19 +82,18 @@ public Object getValue(Parameter parameter) {
}

/**
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,13 +52,13 @@ public Object[] getValues() {
}

/**
* 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void setParameter(BindableQuery query, JpaParametersParameterAccessor acc

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void before() {
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;
}
Expand Down

0 comments on commit 03b3516

Please sign in to comment.