Skip to content

Compute ReturnType#isDto eagerly #3160

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -29,6 +29,7 @@
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.ProjectionInformation;
import org.springframework.data.util.Lazy;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -198,12 +199,14 @@ public List<String> getInputProperties() {
* A {@link ReturnedType} that's backed by an actual class.
*
* @author Oliver Gierke
* @author Mikhail Polivakha
* @since 1.12
*/
private static final class ReturnedClass extends ReturnedType {

private static final Set<Class<?>> VOID_TYPES = new HashSet<>(Arrays.asList(Void.class, void.class));

private final Lazy<Boolean> isDto;
private final Class<?> type;
private final List<String> inputProperties;

Expand All @@ -222,6 +225,15 @@ public ReturnedClass(Class<?> returnedType, Class<?> domainType) {
Assert.isTrue(!returnedType.isInterface(), "Returned type must not be an interface");

this.type = returnedType;
this.isDto = Lazy.of(() ->
!Object.class.equals(type) && //
!type.isEnum() && //
!isDomainSubtype() && //
!isPrimitiveOrWrapper() && //
!Number.class.isAssignableFrom(type) && //
!VOID_TYPES.contains(type) && //
!type.getPackage().getName().startsWith("java.")
);
this.inputProperties = detectConstructorParameterNames(returnedType);
}

Expand Down Expand Up @@ -271,13 +283,7 @@ private List<String> detectConstructorParameterNames(Class<?> type) {
}

private boolean isDto() {
return !Object.class.equals(type) && //
!type.isEnum() && //
!isDomainSubtype() && //
!isPrimitiveOrWrapper() && //
!Number.class.isAssignableFrom(type) && //
!VOID_TYPES.contains(type) && //
!type.getPackage().getName().startsWith("java.");
return isDto.get();
}

private boolean isDomainSubtype() {
Expand Down