Skip to content

Commit 899a98c

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-809 - Add Class-generating property accessor factory.
We now support generated PersistentPropertyAccessors when using Java 7 and if property/association name hashCodes are unique within a PersistentEntity. Generated PersistentPropertyAccessors provide optimized access to properties. They use either MethodHandles or direct property/field access, depending on the visibility/final modifiers of the entity type and its members. A generated PersistentPropertyAccessor is injected into the originating class loader of the entity class to enable optimizations for package-default/protected member access. Original pull request: #159.
1 parent 0515b13 commit 899a98c

10 files changed

+2509
-28
lines changed

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545

4646
/**
4747
* Simple value object to capture information of {@link PersistentEntity}s.
48-
*
48+
*
4949
* @author Oliver Gierke
5050
* @author Jon Brisbin
5151
* @author Patryk Wasik
5252
* @author Thomas Darimont
5353
* @author Christoph Strobl
54+
* @author Mark Paluch
5455
*/
5556
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {
5657

@@ -66,9 +67,11 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
6667
private P idProperty;
6768
private P versionProperty;
6869

70+
private final PersistentPropertyAccessorFactory propertyAccessorFactory;
71+
6972
/**
7073
* Creates a new {@link BasicPersistentEntity} from the given {@link TypeInformation}.
71-
*
74+
*
7275
* @param information must not be {@literal null}.
7376
*/
7477
public BasicPersistentEntity(TypeInformation<T> information) {
@@ -79,7 +82,7 @@ public BasicPersistentEntity(TypeInformation<T> information) {
7982
* Creates a new {@link BasicPersistentEntity} for the given {@link TypeInformation} and {@link Comparator}. The given
8083
* {@link Comparator} will be used to define the order of the {@link PersistentProperty} instances added to the
8184
* entity.
82-
*
85+
*
8386
* @param information must not be {@literal null}.
8487
* @param comparator can be {@literal null}.
8588
*/
@@ -91,14 +94,15 @@ public BasicPersistentEntity(TypeInformation<T> information, Comparator<P> compa
9194
this.properties = new ArrayList<P>();
9295
this.comparator = comparator;
9396
this.constructor = new PreferredConstructorDiscoverer<T, P>(information, this).getConstructor();
94-
this.associations = comparator == null ? new HashSet<Association<P>>() : new TreeSet<Association<P>>(
95-
new AssociationComparator<P>(comparator));
97+
this.associations = comparator == null ? new HashSet<Association<P>>()
98+
: new TreeSet<Association<P>>(new AssociationComparator<P>(comparator));
9699

97100
this.propertyCache = new HashMap<String, P>();
98101
this.annotationCache = new HashMap<Class<? extends Annotation>, Annotation>();
102+
this.propertyAccessorFactory = new DefaultPersistentPropertyAccessorFactory();
99103
}
100104

101-
/*
105+
/*
102106
* (non-Javadoc)
103107
* @see org.springframework.data.mapping.PersistentEntity#getPersistenceConstructor()
104108
*/
@@ -122,7 +126,7 @@ public boolean isIdProperty(PersistentProperty<?> property) {
122126
return this.idProperty == null ? false : this.idProperty.equals(property);
123127
}
124128

125-
/*
129+
/*
126130
* (non-Javadoc)
127131
* @see org.springframework.data.mapping.PersistentEntity#isVersionProperty(org.springframework.data.mapping.PersistentProperty)
128132
*/
@@ -146,31 +150,31 @@ public P getIdProperty() {
146150
return idProperty;
147151
}
148152

149-
/*
153+
/*
150154
* (non-Javadoc)
151155
* @see org.springframework.data.mapping.PersistentEntity#getVersionProperty()
152156
*/
153157
public P getVersionProperty() {
154158
return versionProperty;
155159
}
156160

157-
/*
161+
/*
158162
* (non-Javadoc)
159163
* @see org.springframework.data.mapping.PersistentEntity#hasIdProperty()
160164
*/
161165
public boolean hasIdProperty() {
162166
return idProperty != null;
163167
}
164168

165-
/*
169+
/*
166170
* (non-Javadoc)
167171
* @see org.springframework.data.mapping.PersistentEntity#hasVersionProperty()
168172
*/
169173
public boolean hasVersionProperty() {
170174
return versionProperty != null;
171175
}
172176

173-
/*
177+
/*
174178
* (non-Javadoc)
175179
* @see org.springframework.data.mapping.MutablePersistentEntity#addPersistentProperty(P)
176180
*/
@@ -197,9 +201,11 @@ public void addPersistentProperty(P property) {
197201
if (property.isVersionProperty()) {
198202

199203
if (this.versionProperty != null) {
200-
throw new MappingException(String.format(
201-
"Attempt to add version property %s but already have property %s registered "
202-
+ "as version. Check your mapping configuration!", property.getField(), versionProperty.getField()));
204+
throw new MappingException(
205+
String.format(
206+
"Attempt to add version property %s but already have property %s registered "
207+
+ "as version. Check your mapping configuration!",
208+
property.getField(), versionProperty.getField()));
203209
}
204210

205211
this.versionProperty = property;
@@ -208,7 +214,7 @@ public void addPersistentProperty(P property) {
208214

209215
/**
210216
* Returns the given property if it is a better candidate for the id property than the current id property.
211-
*
217+
*
212218
* @param property the new id property candidate, will never be {@literal null}.
213219
* @return the given id property or {@literal null} if the given property is not an id property.
214220
*/
@@ -244,7 +250,7 @@ public P getPersistentProperty(String name) {
244250
return propertyCache.get(name);
245251
}
246252

247-
/*
253+
/*
248254
* (non-Javadoc)
249255
* @see org.springframework.data.mapping.PersistentEntity#getPersistentProperty(java.lang.Class)
250256
*/
@@ -279,7 +285,7 @@ public Class<T> getType() {
279285
return information.getType();
280286
}
281287

282-
/*
288+
/*
283289
* (non-Javadoc)
284290
* @see org.springframework.data.mapping.PersistentEntity#getTypeAlias()
285291
*/
@@ -312,7 +318,7 @@ public void doWithProperties(PropertyHandler<P> handler) {
312318
}
313319
}
314320

315-
/*
321+
/*
316322
* (non-Javadoc)
317323
* @see org.springframework.data.mapping.PersistentEntity#doWithProperties(org.springframework.data.mapping.PropertyHandler.Simple)
318324
*/
@@ -354,7 +360,7 @@ public void doWithAssociations(SimpleAssociationHandler handler) {
354360
}
355361
}
356362

357-
/*
363+
/*
358364
* (non-Javadoc)
359365
* @see org.springframework.data.mapping.PersistentEntity#findAnnotation(java.lang.Class)
360366
*/
@@ -372,7 +378,7 @@ public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
372378
return annotation;
373379
}
374380

375-
/*
381+
/*
376382
* (non-Javadoc)
377383
* @see org.springframework.data.mapping.MutablePersistentEntity#verify()
378384
*/
@@ -383,7 +389,7 @@ public void verify() {
383389
}
384390
}
385391

386-
/*
392+
/*
387393
* (non-Javadoc)
388394
* @see org.springframework.data.mapping.PersistentEntity#getPropertyAccessor(java.lang.Object)
389395
*/
@@ -393,10 +399,10 @@ public PersistentPropertyAccessor getPropertyAccessor(Object bean) {
393399
Assert.notNull(bean, "Target bean must not be null!");
394400
Assert.isTrue(getType().isInstance(bean), "Target bean is not of type of the persistent entity!");
395401

396-
return new BeanWrapper<Object>(bean);
402+
return propertyAccessorFactory.getPropertyAccessor(this, bean);
397403
}
398404

399-
/*
405+
/*
400406
* (non-Javadoc)
401407
* @see org.springframework.data.mapping.PersistentEntity#getIdentifierAccessor(java.lang.Object)
402408
*/
@@ -419,7 +425,7 @@ private static enum NullReturningIdentifierAccessor implements IdentifierAccesso
419425

420426
INSTANCE;
421427

422-
/*
428+
/*
423429
* (non-Javadoc)
424430
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
425431
*/
@@ -431,11 +437,11 @@ public Object getIdentifier() {
431437

432438
/**
433439
* Simple {@link Comparator} adaptor to delegate ordering to the inverse properties of the association.
434-
*
440+
*
435441
* @author Oliver Gierke
436442
*/
437-
private static final class AssociationComparator<P extends PersistentProperty<P>> implements
438-
Comparator<Association<P>>, Serializable {
443+
private static final class AssociationComparator<P extends PersistentProperty<P>>
444+
implements Comparator<Association<P>>, Serializable {
439445

440446
private static final long serialVersionUID = 4508054194886854513L;
441447
private final Comparator<P> delegate;

0 commit comments

Comments
 (0)