1
1
/*
2
- * Copyright 2019 the original author or authors.
2
+ * Copyright 2019-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .mapping .model ;
17
17
18
+ import java .util .function .Function ;
19
+
18
20
import org .springframework .data .annotation .PersistenceConstructor ;
19
21
import org .springframework .data .mapping .PersistentEntity ;
20
22
import org .springframework .data .mapping .PersistentProperty ;
31
33
* {@link PersistentProperty} is to be applied on a completely immutable entity type exposing a persistence constructor.
32
34
*
33
35
* @author Oliver Drotbohm
36
+ * @author Mark Paluch
37
+ * @since 2.3
34
38
*/
35
39
public class InstantiationAwarePropertyAccessor <T > implements PersistentPropertyAccessor <T > {
36
40
37
41
private static final String NO_SETTER_OR_CONSTRUCTOR = "Cannot set property %s because no setter, wither or copy constructor exists for %s!" ;
38
42
private static final String NO_CONSTRUCTOR_PARAMETER = "Cannot set property %s because no setter, no wither and it's not part of the persistence constructor %s!" ;
39
43
40
- private final PersistentPropertyAccessor <T > delegate ;
44
+ private final Function < T , PersistentPropertyAccessor <T >> delegateFunction ;
41
45
private final EntityInstantiators instantiators ;
42
46
43
47
private T bean ;
@@ -48,17 +52,41 @@ public class InstantiationAwarePropertyAccessor<T> implements PersistentProperty
48
52
*
49
53
* @param delegate must not be {@literal null}.
50
54
* @param instantiators must not be {@literal null}.
55
+ * @deprecated since 2.4. Using this constructor allows only setting a single property as
56
+ * {@link PersistentPropertyAccessor} holds a reference to the initial bean state.
51
57
*/
58
+ @ Deprecated
52
59
public InstantiationAwarePropertyAccessor (PersistentPropertyAccessor <T > delegate , EntityInstantiators instantiators ) {
53
60
54
- Assert .notNull (delegate , "Delegate PersistenPropertyAccessor must not be null!" );
61
+ Assert .notNull (delegate , "Delegate PersistentPropertyAccessor must not be null!" );
55
62
Assert .notNull (instantiators , "EntityInstantiators must not be null!" );
56
63
57
- this .delegate = delegate ;
58
64
this .instantiators = instantiators ;
65
+ this .delegateFunction = t -> delegate ;
59
66
this .bean = delegate .getBean ();
60
67
}
61
68
69
+ /**
70
+ * Creates an {@link InstantiationAwarePropertyAccessor} using the given delegate {@code accessorFunction} and
71
+ * {@link EntityInstantiators}. {@code accessorFunction} is used to obtain a new {@link PersistentPropertyAccessor}
72
+ * for each property to set.
73
+ *
74
+ * @param bean must not be {@literal null}.
75
+ * @param accessorFunction must not be {@literal null}.
76
+ * @param instantiators must not be {@literal null}.
77
+ */
78
+ public InstantiationAwarePropertyAccessor (T bean , Function <T , PersistentPropertyAccessor <T >> accessorFunction ,
79
+ EntityInstantiators instantiators ) {
80
+
81
+ Assert .notNull (bean , "Bean must not be null!" );
82
+ Assert .notNull (accessorFunction , "PersistentPropertyAccessor function must not be null!" );
83
+ Assert .notNull (instantiators , "EntityInstantiators must not be null!" );
84
+
85
+ this .delegateFunction = accessorFunction ;
86
+ this .instantiators = instantiators ;
87
+ this .bean = bean ;
88
+ }
89
+
62
90
/*
63
91
* (non-Javadoc)
64
92
* @see org.springframework.data.mapping.PersistentPropertyAccessor#setProperty(org.springframework.data.mapping.PersistentProperty, java.lang.Object)
@@ -68,6 +96,7 @@ public InstantiationAwarePropertyAccessor(PersistentPropertyAccessor<T> delegate
68
96
public void setProperty (PersistentProperty <?> property , @ Nullable Object value ) {
69
97
70
98
PersistentEntity <?, ?> owner = property .getOwner ();
99
+ PersistentPropertyAccessor <T > delegate = delegateFunction .apply (this .bean );
71
100
72
101
if (!property .isImmutable () || property .getWither () != null || ReflectionUtils .isKotlinClass (owner .getType ())) {
73
102
@@ -123,7 +152,7 @@ public Object getParameterValue(Parameter parameter) {
123
152
@ Nullable
124
153
@ Override
125
154
public Object getProperty (PersistentProperty <?> property ) {
126
- return delegate .getProperty (property );
155
+ return delegateFunction . apply ( bean ) .getProperty (property );
127
156
}
128
157
129
158
/*
0 commit comments