Skip to content

Use enclosing class constructor parameter only for non-static inner classes #3039

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 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3038-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**
* Value object to represent constructor parameters.
*
* @param <T> the type of the parameter
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class Parameter<T, P extends PersistentProperty<P>> {

Expand Down Expand Up @@ -72,7 +74,7 @@ public Parameter(@Nullable String name, TypeInformation<T> type, Annotation[] an
}

Class<T> owningType = entity.getType();
return owningType.isMemberClass() && type.getType().equals(owningType.getEnclosingClass());
return ClassUtils.isInnerClass(owningType) && type.getType().equals(owningType.getEnclosingClass());
});

this.hasSpelExpression = Lazy.of(() -> StringUtils.hasText(getSpelExpression()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@
package org.springframework.data.mapping;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.lang.annotation.Annotation;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.mapping.ParameterUnitTests.IFace.ClassMember;
import org.springframework.data.mapping.ParameterUnitTests.IFace.RecordMember;
import org.springframework.data.mapping.ParameterUnitTests.StaticType.NonStaticInner;
import org.springframework.data.mapping.ParameterUnitTests.StaticType.RecordInner;
import org.springframework.data.mapping.ParameterUnitTests.StaticType.StaticInner;
import org.springframework.data.util.TypeInformation;

/**
* Unit tests for {@link Parameter}.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
class ParameterUnitTests<P extends PersistentProperty<P>> {
Expand Down Expand Up @@ -87,4 +95,81 @@ void twoParametersWithDifferenTypeAreNotEqual() {

assertThat(left).isNotEqualTo(right);
}

@Test // GH-3038
void shouldNotConsiderRecordTypeOfInterfaceEnclosingClassParameter() {

PersistentEntity pe = Mockito.mock(PersistentEntity.class);
when(pe.getType()).thenReturn(RecordMember.class);

Parameter<IFace, P> iFace = new Parameter<IFace, P>("iFace", TypeInformation.of(IFace.class), annotations, pe);
assertThat(iFace.isEnclosingClassParameter()).isFalse();
}

@Test // GH-3038
void shouldNotConsiderMemberTypeOfInterfaceEnclosingClassParameter() {

PersistentEntity pe = Mockito.mock(PersistentEntity.class);
when(pe.getType()).thenReturn(ClassMember.class);

Parameter<IFace, P> iFace = new Parameter<IFace, P>("iFace", TypeInformation.of(IFace.class), annotations, pe);
assertThat(iFace.isEnclosingClassParameter()).isFalse();
}

@Test // GH-3038
void shouldConsiderMemberTypeOfClassEnclosingClassParameter() {

PersistentEntity pe = Mockito.mock(PersistentEntity.class);
when(pe.getType()).thenReturn(NonStaticInner.class);

Parameter<StaticType, P> iFace = new Parameter<StaticType, P>("outer", TypeInformation.of(StaticType.class),
annotations, pe);
assertThat(iFace.isEnclosingClassParameter()).isTrue();
}

@Test // GH-3038
void shouldNotConsiderStaticMemberTypeOfClassEnclosingClassParameter() {

PersistentEntity pe = Mockito.mock(PersistentEntity.class);
when(pe.getType()).thenReturn(StaticInner.class);

Parameter<StaticType, P> iFace = new Parameter<StaticType, P>("outer", TypeInformation.of(StaticType.class),
annotations, pe);
assertThat(iFace.isEnclosingClassParameter()).isFalse();
}

@Test // GH-3038
void shouldNotConsiderRecordMemberTypeOfClassEnclosingClassParameter() {

PersistentEntity pe = Mockito.mock(PersistentEntity.class);
when(pe.getType()).thenReturn(RecordInner.class);

Parameter<StaticType, P> iFace = new Parameter<StaticType, P>("outer", TypeInformation.of(StaticType.class),
annotations, pe);
assertThat(iFace.isEnclosingClassParameter()).isFalse();
}

interface IFace {

record RecordMember(IFace iFace) {
}

class ClassMember {
ClassMember(IFace iface) {}
}
}

static class StaticType {

class NonStaticInner {
NonStaticInner(StaticType outer) {}
}

static class StaticInner {
StaticInner(StaticType outer) {}
}

record RecordInner(StaticType outer) {
}
}
}