Skip to content

Commit f75d692

Browse files
committed
Consider @column and @element annotated constructor parameters.
We now allow using Column and Element annotations for constructor arguments when using constructor creation from columns/UDT fields respective tuple value elements. The column name/element ordinal can be overwritten for persistent properties. Non-persistent properties can be easier obtained than through the Value("…") annotation that uses SpEL. Closes #1104.
1 parent 024214f commit f75d692

File tree

8 files changed

+725
-72
lines changed

8 files changed

+725
-72
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.core.convert;
17+
18+
import java.lang.annotation.Annotation;
19+
import java.lang.reflect.AnnotatedType;
20+
import java.lang.reflect.Field;
21+
import java.lang.reflect.Method;
22+
23+
import org.springframework.beans.BeansException;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.core.annotation.MergedAnnotations;
26+
import org.springframework.data.cassandra.core.cql.Ordering;
27+
import org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty;
28+
import org.springframework.data.cassandra.core.mapping.Column;
29+
import org.springframework.data.cassandra.core.mapping.Element;
30+
import org.springframework.data.mapping.Association;
31+
import org.springframework.data.mapping.PersistentEntity;
32+
import org.springframework.data.util.TypeInformation;
33+
import org.springframework.lang.Nullable;
34+
35+
import com.datastax.oss.driver.api.core.CqlIdentifier;
36+
37+
/**
38+
* {@link CassandraPersistentProperty} wrapper for a delegate {@link CassandraPersistentProperty} considering
39+
* annotations from a constructor parameter.
40+
*
41+
* @author Mark Paluch
42+
* @since 3.2
43+
*/
44+
class AnnotatedCassandraConstructorProperty implements CassandraPersistentProperty {
45+
46+
private final CassandraPersistentProperty delegate;
47+
48+
private final MergedAnnotations annotations;
49+
50+
public AnnotatedCassandraConstructorProperty(CassandraPersistentProperty delegate, MergedAnnotations annotations) {
51+
this.delegate = delegate;
52+
this.annotations = annotations;
53+
}
54+
55+
@Override
56+
@Nullable
57+
public CqlIdentifier getColumnName() {
58+
59+
if (annotations.isPresent(Column.class)) {
60+
return CqlIdentifier.fromCql(annotations.get(Column.class).getString("value"));
61+
}
62+
63+
return delegate.getColumnName();
64+
}
65+
66+
@Override
67+
@Nullable
68+
public Integer getOrdinal() {
69+
70+
if (annotations.isPresent(Element.class)) {
71+
return annotations.get(Element.class).getInt("value");
72+
}
73+
74+
return delegate.getOrdinal();
75+
}
76+
77+
@Override
78+
@Nullable
79+
public Ordering getPrimaryKeyOrdering() {
80+
return delegate.getPrimaryKeyOrdering();
81+
}
82+
83+
@Override
84+
public boolean isClusterKeyColumn() {
85+
return delegate.isClusterKeyColumn();
86+
}
87+
88+
@Override
89+
public boolean isCompositePrimaryKey() {
90+
return delegate.isCompositePrimaryKey();
91+
}
92+
93+
@Override
94+
public boolean isMapLike() {
95+
return delegate.isMapLike();
96+
}
97+
98+
@Override
99+
public boolean isPartitionKeyColumn() {
100+
return delegate.isPartitionKeyColumn();
101+
}
102+
103+
@Override
104+
public boolean isPrimaryKeyColumn() {
105+
return delegate.isPrimaryKeyColumn();
106+
}
107+
108+
@Override
109+
@Nullable
110+
public AnnotatedType findAnnotatedType(Class<? extends Annotation> annotationType) {
111+
return delegate.findAnnotatedType(annotationType);
112+
}
113+
114+
@Override
115+
public PersistentEntity<?, CassandraPersistentProperty> getOwner() {
116+
return delegate.getOwner();
117+
}
118+
119+
@Override
120+
public String getName() {
121+
return delegate.getName();
122+
}
123+
124+
@Override
125+
public Class<?> getType() {
126+
return delegate.getType();
127+
}
128+
129+
@Override
130+
public TypeInformation<?> getTypeInformation() {
131+
return delegate.getTypeInformation();
132+
}
133+
134+
@Override
135+
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
136+
return delegate.getPersistentEntityTypes();
137+
}
138+
139+
@Override
140+
@Nullable
141+
public Method getGetter() {
142+
return delegate.getGetter();
143+
}
144+
145+
@Override
146+
@Nullable
147+
public Method getSetter() {
148+
return delegate.getSetter();
149+
}
150+
151+
@Override
152+
@Nullable
153+
public Method getWither() {
154+
return delegate.getWither();
155+
}
156+
157+
@Override
158+
@Nullable
159+
public Field getField() {
160+
return delegate.getField();
161+
}
162+
163+
@Override
164+
@Nullable
165+
public String getSpelExpression() {
166+
return delegate.getSpelExpression();
167+
}
168+
169+
@Override
170+
@Nullable
171+
public Association<CassandraPersistentProperty> getAssociation() {
172+
return delegate.getAssociation();
173+
}
174+
175+
@Override
176+
public boolean isEntity() {
177+
return delegate.isEntity();
178+
}
179+
180+
@Override
181+
public boolean isIdProperty() {
182+
return delegate.isIdProperty();
183+
}
184+
185+
@Override
186+
public boolean isVersionProperty() {
187+
return delegate.isVersionProperty();
188+
}
189+
190+
@Override
191+
public boolean isCollectionLike() {
192+
return delegate.isCollectionLike();
193+
}
194+
195+
@Override
196+
public boolean isMap() {
197+
return delegate.isMap();
198+
}
199+
200+
@Override
201+
public boolean isArray() {
202+
return delegate.isArray();
203+
}
204+
205+
@Override
206+
public boolean isTransient() {
207+
return delegate.isTransient();
208+
}
209+
210+
@Override
211+
public boolean isWritable() {
212+
return delegate.isWritable();
213+
}
214+
215+
@Override
216+
public boolean isImmutable() {
217+
return delegate.isImmutable();
218+
}
219+
220+
@Override
221+
public boolean isAssociation() {
222+
return delegate.isAssociation();
223+
}
224+
225+
@Override
226+
@Nullable
227+
public Class<?> getComponentType() {
228+
return delegate.getComponentType();
229+
}
230+
231+
@Override
232+
public Class<?> getRawType() {
233+
return delegate.getRawType();
234+
}
235+
236+
@Override
237+
@Nullable
238+
public Class<?> getMapValueType() {
239+
return delegate.getMapValueType();
240+
}
241+
242+
@Override
243+
public Class<?> getActualType() {
244+
return delegate.getActualType();
245+
}
246+
247+
@Override
248+
@Nullable
249+
public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
250+
return delegate.findAnnotation(annotationType);
251+
}
252+
253+
@Override
254+
@Nullable
255+
public <A extends Annotation> A findPropertyOrOwnerAnnotation(Class<A> annotationType) {
256+
return delegate.findPropertyOrOwnerAnnotation(annotationType);
257+
}
258+
259+
@Override
260+
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
261+
return delegate.isAnnotationPresent(annotationType);
262+
}
263+
264+
@Override
265+
public boolean usePropertyAccess() {
266+
return delegate.usePropertyAccess();
267+
}
268+
269+
@Override
270+
@Nullable
271+
public Class<?> getAssociationTargetType() {
272+
return delegate.getAssociationTargetType();
273+
}
274+
275+
@Override
276+
public void setColumnName(CqlIdentifier columnName) {
277+
throw new UnsupportedOperationException();
278+
}
279+
280+
@Override
281+
@Deprecated
282+
public void setForceQuote(boolean forceQuote) {
283+
throw new UnsupportedOperationException();
284+
}
285+
286+
@Override
287+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
288+
throw new UnsupportedOperationException();
289+
}
290+
291+
}

0 commit comments

Comments
 (0)