Skip to content

Commit 88ac1fc

Browse files
christophstroblodrotbohm
authored andcommitted
DATACMNS-1101 - Polishing.
Move MappingException from o.s.d.mapping.model to o.s.d.mapping package and use it for getRequired* methods in MappingContext instead of throwing IllegalArgumentException. Further on we’ve consolidated usage of IllegalArgumentException and IllegalStateException. Along that IllegalMappingException was removed in favor or MappingException. BasicPersistentEntity now looks up all properties by annotation instead of just returning the first one found. We’ve kept the original methods behavior and updated the comment as well as introduced getPersistentProperties returning all matching properties.
1 parent 09dd13c commit 88ac1fc

17 files changed

+139
-111
lines changed

src/main/java/org/springframework/data/mapping/model/MappingException.java renamed to src/main/java/org/springframework/data/mapping/MappingException.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright 2011-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package org.springframework.data.mapping.model;
16+
package org.springframework.data.mapping;
1817

1918
/**
2019
* @author Jon Brisbin <[email protected]>

src/main/java/org/springframework/data/mapping/PersistentEntity.java

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mapping;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.Iterator;
1920

2021
import org.springframework.data.convert.EntityInstantiator;
2122
import org.springframework.data.util.TypeInformation;
@@ -28,13 +29,14 @@
2829
* @author Jon Brisbin
2930
* @author Patryk Wasik
3031
* @author Mark Paluch
32+
* @author Christoph Strobl
3133
*/
3234
public interface PersistentEntity<T, P extends PersistentProperty<P>> {
3335

3436
/**
3537
* The entity name including any package prefix.
3638
*
37-
* @return must never return {@literal null}
39+
* @return must never return {@literal null}.
3840
*/
3941
String getName();
4042

@@ -51,7 +53,7 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
5153
* Returns whether the given {@link PersistentProperty} is referred to by a constructor argument of the
5254
* {@link PersistentEntity}.
5355
*
54-
* @param property
56+
* @param property can be {@literal null}.
5557
* @return true if the given {@link PersistentProperty} is referred to by a constructor argument or {@literal false}
5658
* if not or {@literal null}.
5759
*/
@@ -60,16 +62,16 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
6062
/**
6163
* Returns whether the given {@link PersistentProperty} is the id property of the entity.
6264
*
63-
* @param property
64-
* @return
65+
* @param property can be {@literal null}.
66+
* @return {@literal true} given property is the entities id.
6567
*/
6668
boolean isIdProperty(PersistentProperty<?> property);
6769

6870
/**
6971
* Returns whether the given {@link PersistentProperty} is the version property of the entity.
7072
*
71-
* @param property
72-
* @return
73+
* @param property can be {@literal null}.
74+
* @return {@literal true} given property is used as version.
7375
*/
7476
boolean isVersionProperty(PersistentProperty<?> property);
7577

@@ -81,6 +83,13 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
8183
*/
8284
P getIdProperty();
8385

86+
/**
87+
* Returns the id property of the {@link PersistentEntity}.
88+
*
89+
* @return the id property of the {@link PersistentEntity}.
90+
* @throws IllegalStateException if {@link PersistentEntity} does not define an {@literal id} property.
91+
* @since 2.0
92+
*/
8493
default P getRequiredIdProperty() {
8594

8695
P property = getIdProperty();
@@ -100,6 +109,14 @@ default P getRequiredIdProperty() {
100109
*/
101110
P getVersionProperty();
102111

112+
/**
113+
* Returns the version property of the {@link PersistentEntity}. Can be {@literal null} in case no version property is
114+
* available on the entity.
115+
*
116+
* @return the version property of the {@link PersistentEntity}.
117+
* @throws IllegalStateException if {@link PersistentEntity} does not define a {@literal version} property.
118+
* @since 2.0
119+
*/
103120
default P getRequiredVersionProperty() {
104121

105122
P property = getVersionProperty();
@@ -114,17 +131,17 @@ default P getRequiredVersionProperty() {
114131
/**
115132
* Obtains a {@link PersistentProperty} instance by name.
116133
*
117-
* @param name The name of the property
134+
* @param name The name of the property. Can be {@literal null}.
118135
* @return the {@link PersistentProperty} or {@literal null} if it doesn't exist.
119136
*/
120137
P getPersistentProperty(String name);
121138

122139
/**
123140
* Returns the {@link PersistentProperty} with the given name.
124141
*
125-
* @param name the name of the property, must not be {@literal null} or empty.
142+
* @param name the name of the property. Can be {@literal null} or empty.
126143
* @return the {@link PersistentProperty} with the given name.
127-
* @throws IllegalArgumentException in case no property with the given name exists.
144+
* @throws IllegalStateException in case no property with the given name exists.
128145
*/
129146
default P getRequiredPersistentProperty(String name) {
130147

@@ -138,34 +155,47 @@ default P getRequiredPersistentProperty(String name) {
138155
}
139156

140157
/**
141-
* Returns the property equipped with an annotation of the given type.
158+
* Returns the first property equipped with an {@link Annotation} of the given type.
142159
*
143160
* @param annotationType must not be {@literal null}.
144-
* @return
161+
* @return {@literal null} if no property found with given annotation type.
145162
* @since 1.8
146163
*/
147-
P getPersistentProperty(Class<? extends Annotation> annotationType);
164+
default P getPersistentProperty(Class<? extends Annotation> annotationType) {
165+
166+
Iterator<P> it = getPersistentProperties(annotationType).iterator();
167+
return it.hasNext() ? it.next() : null;
168+
}
169+
170+
/**
171+
* Returns all properties equipped with an {@link Annotation} of the given type.
172+
*
173+
* @param annotationType must not be {@literal null}.
174+
* @return {@literal empty} {@link Iterator} if no match found. Never {@literal null}.
175+
* @since 2.0
176+
*/
177+
Iterable<P> getPersistentProperties(Class<? extends Annotation> annotationType);
148178

149179
/**
150180
* Returns whether the {@link PersistentEntity} has an id property. If this call returns {@literal true},
151181
* {@link #getIdProperty()} will return a non-{@literal null} value.
152182
*
153-
* @return
183+
* @return {@literal true} if entity has an {@literal id} property.
154184
*/
155185
boolean hasIdProperty();
156186

157187
/**
158188
* Returns whether the {@link PersistentEntity} has a version property. If this call returns {@literal true},
159189
* {@link #getVersionProperty()} will return a non-{@literal null} value.
160190
*
161-
* @return
191+
* @return {@literal true} if entity has a {@literal version} property.
162192
*/
163193
boolean hasVersionProperty();
164194

165195
/**
166196
* Returns the resolved Java type of this entity.
167197
*
168-
* @return The underlying Java class for this entity
198+
* @return The underlying Java class for this entity. Never {@literal null}.
169199
*/
170200
Class<T> getType();
171201

@@ -194,6 +224,12 @@ default P getRequiredPersistentProperty(String name) {
194224

195225
void doWithProperties(SimplePropertyHandler handler);
196226

227+
/**
228+
* Get {@link Iterable}
229+
*
230+
* @return
231+
* @since 2.0
232+
*/
197233
Iterable<P> getPersistentProperties();
198234

199235
/**
@@ -211,7 +247,7 @@ default P getRequiredPersistentProperty(String name) {
211247
* Looks up the annotation of the given type on the {@link PersistentEntity}.
212248
*
213249
* @param annotationType must not be {@literal null}.
214-
* @return
250+
* @return {@literal null} if not found.
215251
* @since 1.8
216252
*/
217253
<A extends Annotation> A findAnnotation(Class<A> annotationType);
@@ -220,7 +256,7 @@ default P getRequiredPersistentProperty(String name) {
220256
* Checks whether the annotation of the given type is present on the {@link PersistentEntity}.
221257
*
222258
* @param annotationType must not be {@literal null}.
223-
* @return
259+
* @return {@literal true} if {@link Annotation} of given type is present.
224260
* @since 2.0
225261
*/
226262
<A extends Annotation> boolean isAnnotationPresent(Class<A> annotationType);
@@ -229,7 +265,7 @@ default P getRequiredPersistentProperty(String name) {
229265
* Returns a {@link PersistentPropertyAccessor} to access property values of the given bean.
230266
*
231267
* @param bean must not be {@literal null}.
232-
* @return
268+
* @return new {@link PersistentPropertyAccessor}.
233269
* @since 1.10
234270
*/
235271
PersistentPropertyAccessor getPropertyAccessor(Object bean);
@@ -238,7 +274,7 @@ default P getRequiredPersistentProperty(String name) {
238274
* Returns the {@link IdentifierAccessor} for the given bean.
239275
*
240276
* @param bean must not be {@literal null}.
241-
* @return
277+
* @return new {@link IdentifierAccessor}.
242278
* @since 1.10
243279
*/
244280
IdentifierAccessor getIdentifierAccessor(Object bean);

src/main/java/org/springframework/data/mapping/PersistentProperty.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
3535
/**
3636
* Returns the {@link PersistentEntity} owning the current {@link PersistentProperty}.
3737
*
38-
* @return
38+
* @return never {@literal null}.
3939
*/
4040
PersistentEntity<?, P> getOwner();
4141

@@ -87,10 +87,22 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
8787

8888
Field getField();
8989

90+
/**
91+
* @return {@literal null} if no expression defined.
92+
*/
9093
String getSpelExpression();
9194

95+
/**
96+
* @return {@literal null} if property is not part of an {@link Association}.
97+
*/
9298
Association<P> getAssociation();
9399

100+
/**
101+
* Get the {@link Association} of this property.
102+
*
103+
* @return never {@literal null}.
104+
* @throws IllegalStateException if not involved in an {@link Association}.
105+
*/
94106
default Association<P> getRequiredAssociation() {
95107

96108
Association<P> association = getAssociation();
@@ -106,7 +118,7 @@ default Association<P> getRequiredAssociation() {
106118
* Returns whether the type of the {@link PersistentProperty} is actually to be regarded as {@link PersistentEntity}
107119
* in turn.
108120
*
109-
* @return
121+
* @return {@literal true} a {@link PersistentEntity}.
110122
*/
111123
boolean isEntity();
112124

@@ -116,7 +128,7 @@ default Association<P> getRequiredAssociation() {
116128
* {@link PersistentEntity} creation you should rather call {@link PersistentEntity#isIdProperty(PersistentProperty)}
117129
* to determine whether the current property is the id property of that {@link PersistentEntity} under consideration.
118130
*
119-
* @return
131+
* @return {@literal true} if the {@literal id} property.
120132
*/
121133
boolean isIdProperty();
122134

@@ -210,7 +222,7 @@ default Association<P> getRequiredAssociation() {
210222
* potentially backing field and traverse accessor methods to potentially available super types.
211223
*
212224
* @param annotationType the annotation to look up, must not be {@literal null}.
213-
* @return the annotation of the given type.
225+
* @return the annotation of the given type. Can be {@literal null}.
214226
* @see AnnotationUtils#findAnnotation(Method, Class)
215227
*/
216228
<A extends Annotation> A findAnnotation(Class<A> annotationType);
@@ -220,7 +232,7 @@ default Association<P> getRequiredAssociation() {
220232
* Usefull to lookup annotations that can be configured on the type but overridden on an individual property.
221233
*
222234
* @param annotationType must not be {@literal null}.
223-
* @return
235+
* @return the annotation of the given type. Can be {@literal null}.
224236
*/
225237
<A extends Annotation> A findPropertyOrOwnerAnnotation(Class<A> annotationType);
226238

src/main/java/org/springframework/data/mapping/PersistentPropertyAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface PersistentPropertyAccessor {
3535
*
3636
* @param property must not be {@literal null}.
3737
* @param value can be {@literal null}.
38-
* @throws org.springframework.data.mapping.model.MappingException in case an exception occurred when setting the
38+
* @throws MappingException in case an exception occurred when setting the
3939
* property value.
4040
*/
4141
void setProperty(PersistentProperty<?> property, Object value);

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import org.springframework.data.mapping.PersistentProperty;
4545
import org.springframework.data.mapping.PropertyPath;
4646
import org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory;
47-
import org.springframework.data.mapping.model.MappingException;
47+
import org.springframework.data.mapping.MappingException;
4848
import org.springframework.data.mapping.model.MutablePersistentEntity;
4949
import org.springframework.data.mapping.model.PersistentPropertyAccessorFactory;
5050
import org.springframework.data.mapping.model.Property;

src/main/java/org/springframework/data/mapping/context/InvalidPersistentPropertyPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mapping.context;
1717

18-
import org.springframework.data.mapping.model.MappingException;
18+
import org.springframework.data.mapping.MappingException;
1919
import org.springframework.data.util.TypeInformation;
2020
import org.springframework.util.Assert;
2121

0 commit comments

Comments
 (0)