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.
This commit is contained in:
Mark Paluch
2017-10-24 16:31:06 +02:00
parent d0d0d72c98
commit 14a56dee00
2 changed files with 24 additions and 3 deletions

View File

@@ -110,7 +110,7 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert
return false;
}
if (entity.getType().getClassLoader() == null || entity.getType().getPackage().getName().startsWith("java")) {
if (!isTypeInjectable(entity)) {
return false;
}
@@ -143,6 +143,13 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert
return hashCodes.size() == propertyCount.get();
}
private static boolean isTypeInjectable(PersistentEntity<?, ?> entity) {
Class<?> type = entity.getType();
return type.getClassLoader() != null
&& (type.getPackage() == null || !type.getPackage().getName().startsWith("java"));
}
/**
* @param entity must not be {@literal null}.
* @return

View File

@@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mapping.model;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
@@ -38,6 +38,7 @@ import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.data.mapping.model.subpackage.TypeInOtherPackage;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
/**
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory}
@@ -63,7 +64,8 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
}
@Parameters(name = "{3}")
public static List<Object[]> parameters() {
@SuppressWarnings("unchecked")
public static List<Object[]> parameters() throws ReflectiveOperationException {
List<Object[]> parameters = new ArrayList<Object[]>();
List<String> propertyNames = Arrays.asList("privateField", "packageDefaultField", "protectedField", "publicField",
@@ -80,6 +82,11 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
ClassGeneratingPropertyAccessorPublicType.class));
parameters.addAll(parameters(new SubtypeOfTypeInOtherPackage(), propertyNames, SubtypeOfTypeInOtherPackage.class));
Class<Object> defaultPackageClass = (Class) Class.forName("TypeInDefaultPackage");
parameters
.add(new Object[] { defaultPackageClass.newInstance(), "", defaultPackageClass, "Class in default package" });
return parameters;
}
@@ -95,9 +102,16 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
return parameters;
}
@Test // DATACMNS-1201
public void shouldSupportGeneratedPropertyAccessors() {
assertThat(factory.isSupported(mappingContext.getPersistentEntity(bean.getClass())), is(true));
}
@Test // DATACMNS-809
public void shouldSetAndGetProperty() throws Exception {
assumeTrue(StringUtils.hasText(propertyName));
PersistentProperty<?> property = getProperty(bean, propertyName);
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);