Skip to content

Commit 14a56de

Browse files
committed
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 d0d0d72 commit 14a56de

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public boolean isSupported(PersistentEntity<?, ?> entity) {
110110
return false;
111111
}
112112

113-
if (entity.getType().getClassLoader() == null || entity.getType().getPackage().getName().startsWith("java")) {
113+
if (!isTypeInjectable(entity)) {
114114
return false;
115115
}
116116

@@ -143,6 +143,13 @@ public void doWithAssociation(Association<? extends PersistentProperty<?>> assoc
143143
return hashCodes.size() == propertyCount.get();
144144
}
145145

146+
private static boolean isTypeInjectable(PersistentEntity<?, ?> entity) {
147+
148+
Class<?> type = entity.getType();
149+
return type.getClassLoader() != null
150+
&& (type.getPackage() == null || !type.getPackage().getName().startsWith("java"));
151+
}
152+
146153
/**
147154
* @param entity must not be {@literal null}.
148155
* @return

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,13 +13,13 @@
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.hamcrest.CoreMatchers.instanceOf;
2019
import static org.hamcrest.CoreMatchers.is;
2120
import static org.hamcrest.Matchers.equalTo;
2221
import static org.junit.Assert.*;
22+
import static org.junit.Assume.assumeTrue;
2323

2424
import java.lang.reflect.Constructor;
2525
import java.util.ArrayList;
@@ -38,6 +38,7 @@
3838
import org.springframework.data.mapping.context.SamplePersistentProperty;
3939
import org.springframework.data.mapping.model.subpackage.TypeInOtherPackage;
4040
import org.springframework.test.util.ReflectionTestUtils;
41+
import org.springframework.util.StringUtils;
4142

4243
/**
4344
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory}
@@ -63,7 +64,8 @@ public ClassGeneratingPropertyAccessorFactoryTests(Object bean, String propertyN
6364
}
6465

6566
@Parameters(name = "{3}")
66-
public static List<Object[]> parameters() {
67+
@SuppressWarnings("unchecked")
68+
public static List<Object[]> parameters() throws ReflectiveOperationException {
6769

6870
List<Object[]> parameters = new ArrayList<Object[]>();
6971
List<String> propertyNames = Arrays.asList("privateField", "packageDefaultField", "protectedField", "publicField",
@@ -80,6 +82,11 @@ public static List<Object[]> parameters() {
8082
ClassGeneratingPropertyAccessorPublicType.class));
8183
parameters.addAll(parameters(new SubtypeOfTypeInOtherPackage(), propertyNames, SubtypeOfTypeInOtherPackage.class));
8284

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

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

105+
@Test // DATACMNS-1201
106+
public void shouldSupportGeneratedPropertyAccessors() {
107+
assertThat(factory.isSupported(mappingContext.getPersistentEntity(bean.getClass())), is(true));
108+
}
109+
98110
@Test // DATACMNS-809
99111
public void shouldSetAndGetProperty() throws Exception {
100112

113+
assumeTrue(StringUtils.hasText(propertyName));
114+
101115
PersistentProperty<?> property = getProperty(bean, propertyName);
102116
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
103117

0 commit comments

Comments
 (0)