Skip to content

Commit a13fc23

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1201 - Support generated property accessors for types in default packages.
We now support generated property accessors for types that reside in the default package. Original pull request: #256.
1 parent 4bd8a3c commit a13fc23

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ private static boolean isClassLoaderDefineClassAvailable(PersistentEntity<?, ?>
133133
}
134134

135135
private static boolean isTypeInjectable(PersistentEntity<?, ?> entity) {
136-
return entity.getType().getClassLoader() != null && !entity.getType().getPackage().getName().startsWith("java");
136+
137+
Class<?> type = entity.getType();
138+
return type.getClassLoader() != null
139+
&& (type.getPackage() == null || !type.getPackage().getName().startsWith("java"));
137140
}
138141

139142
private boolean hasUniquePropertyHashCodes(PersistentEntity<?, ?> entity) {

src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mapping.model;
1817

1918
import static org.assertj.core.api.Assertions.*;
19+
import static org.junit.Assume.*;
2020

2121
import java.lang.reflect.Constructor;
2222
import java.util.ArrayList;
@@ -36,6 +36,7 @@
3636
import org.springframework.data.mapping.context.SamplePersistentProperty;
3737
import org.springframework.data.mapping.model.subpackage.TypeInOtherPackage;
3838
import org.springframework.test.util.ReflectionTestUtils;
39+
import org.springframework.util.StringUtils;
3940

4041
/**
4142
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory}
@@ -61,7 +62,8 @@ public ClassGeneratingPropertyAccessorFactoryTests(Object bean, String propertyN
6162
}
6263

6364
@Parameters(name = "{3}")
64-
public static List<Object[]> parameters() {
65+
@SuppressWarnings("unchecked")
66+
public static List<Object[]> parameters() throws ReflectiveOperationException {
6567

6668
List<Object[]> parameters = new ArrayList<>();
6769
List<String> propertyNames = Arrays.asList("privateField", "packageDefaultField", "protectedField", "publicField",
@@ -79,6 +81,11 @@ public static List<Object[]> parameters() {
7981
ClassGeneratingPropertyAccessorPublicType.class));
8082
parameters.addAll(parameters(new SubtypeOfTypeInOtherPackage(), propertyNames, SubtypeOfTypeInOtherPackage.class));
8183

84+
Class<Object> defaultPackageClass = (Class) Class.forName("TypeInDefaultPackage");
85+
86+
parameters
87+
.add(new Object[] { defaultPackageClass.newInstance(), "", defaultPackageClass, "Class in default package" });
88+
8289
return parameters;
8390
}
8491

@@ -94,9 +101,16 @@ private static List<Object[]> parameters(Object bean, List<String> propertyNames
94101
return parameters;
95102
}
96103

104+
@Test // DATACMNS-1201
105+
public void shouldSupportGeneratedPropertyAccessors() {
106+
assertThat(factory.isSupported(mappingContext.getRequiredPersistentEntity(bean.getClass()))).isTrue();
107+
}
108+
97109
@Test // DATACMNS-809
98110
public void shouldSetAndGetProperty() throws Exception {
99111

112+
assumeTrue(StringUtils.hasText(propertyName));
113+
100114
assertThat(getProperty(bean, propertyName)).satisfies(property -> {
101115

102116
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);

0 commit comments

Comments
 (0)