From 0f7231ec01627acf4fd04f34732456f32beac443 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 7 May 2012 12:53:54 +0200 Subject: [PATCH] DATACMNS-170 - Fixed potential NullPointerException in ReflectionEntityInstantiator. Moved null-check before the attempt to make the discovered id field accessible. --- .../support/ReflectionEntityInformation.java | 3 +- .../ReflectionEntityInformationUnitTests.java | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java index 533d622f6..3d1935a3d 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/ReflectionEntityInformation.java @@ -69,9 +69,8 @@ public class ReflectionEntityInformation extends Abs } }); - ReflectionUtils.makeAccessible(field); - Assert.notNull(this.field, String.format("No field annotated with %s found!", annotation.toString())); + ReflectionUtils.makeAccessible(field); } /* diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java new file mode 100644 index 000000000..5566fbda1 --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/ReflectionEntityInformationUnitTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may +import java.io.Serializable; + not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.repository.core.support; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.io.Serializable; + +import org.junit.Test; +import org.springframework.data.annotation.Id; +import org.springframework.data.repository.core.EntityInformation; + +/** + * Unit tests for {@link ReflectionEntityInformation}. + * + * @author Oliver Gierke + */ +public class ReflectionEntityInformationUnitTests { + + @Test + public void discoversAnnotationOnField() { + + EntityInformation information = getEntityInformation(Sample.class); + assertThat(information.getIdType(), is(typeCompatibleWith(String.class))); + } + + /** + * @see DATACMNS-170 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsTypeWithoutAnnotatedField() { + getEntityInformation(Unannotated.class); + } + + private static EntityInformation getEntityInformation(Class type) { + return new ReflectionEntityInformation(type, Id.class); + } + + static class Sample { + + @Id + String id; + } + + static class Unannotated { + + String id; + } +}