Elffuss BraidHub

kikocis/java-driver public

270ab72db05a · 6.3 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
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 * http://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 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.Metadata;
19import com.datastax.driver.core.TypeCodec;
20import com.datastax.driver.mapping.annotations.*;
21import com.google.common.reflect.TypeToken;
22
23import java.beans.PropertyDescriptor;
24import java.lang.annotation.Annotation;
25import java.lang.reflect.Field;
26import java.lang.reflect.Method;
27import java.lang.reflect.Type;
28import java.util.Collection;
29import java.util.Map;
30
31import static com.google.common.base.Preconditions.checkState;
32
33/**
34 * Maps a Java bean property to a table column or a UDT field.
35 * <p>
36 * Properties can be either accessed through getter and setter pairs,
37 * or by direct field access, depending on what is available in the
38 * entity/UDT class.
39 */
40class PropertyMapper {
41
42 private final String propertyName;
43 final String alias;
44 final String columnName;
45 final TypeToken<Object> javaType;
46 final TypeCodec<Object> customCodec;
47
48 private final Field field;
49 private final Method getter;
50 private final Method setter;
51 private final int position;
52 private final Map<Class<? extends Annotation>, Annotation> annotations;
53
54 PropertyMapper(String propertyName, String alias, Field field, PropertyDescriptor property) {
55 this.propertyName = propertyName;
56 this.alias = alias;
57 this.field = field;
58 getter = ReflectionUtils.findGetter(property);
59 setter = ReflectionUtils.findSetter(property);
60 annotations = ReflectionUtils.scanPropertyAnnotations(field, property);
61 if (!isTransient()) {
62 checkState(field != null || getter != null, "Property '%s' is not readable", propertyName);
63 checkState(field != null || setter != null, "Property '%s' is not writable", propertyName);
64 }
65 columnName = inferColumnName();
66 position = inferPosition();
67 javaType = inferJavaType();
68 customCodec = createCustomCodec();
69 }
70
71 Object getValue(Object entity) {
72 try {
73 // try getter first, if available, otherwise direct field access
74 if (getter != null)
75 return getter.invoke(entity);
76 else
77 return field.get(entity);
78 } catch (Exception e) {
79 throw new IllegalStateException("Unable to read property '" + propertyName + "' in " + entity.getClass().getName(), e);
80 }
81 }
82
83 void setValue(Object entity, Object value) {
84 try {
85 // try setter first, if available, otherwise direct field access
86 if (setter != null)
87 setter.invoke(entity, value);
88 else
89 field.set(entity, value);
90 } catch (Exception e) {
91 throw new IllegalStateException("Unable to write property '" + propertyName + "' in " + entity.getClass().getName(), e);
92 }
93 }
94
95 boolean hasAnnotation(Class<? extends Annotation> annotationClass) {
96 return annotations.containsKey(annotationClass);
97 }
98
99 Collection<Annotation> getAnnotations() {
100 return annotations.values();
101 }
102
103 @SuppressWarnings("unchecked")
104 <A extends Annotation> A annotation(Class<A> annotationClass) {
105 return (A) annotations.get(annotationClass);
106 }
107
108 boolean isComputed() {
109 return hasAnnotation(Computed.class);
110 }
111
112 boolean isTransient() {
113 return hasAnnotation(Transient.class);
114 }
115
116 boolean isPartitionKey() {
117 return hasAnnotation(PartitionKey.class);
118 }
119
120 boolean isClusteringColumn() {
121 return hasAnnotation(ClusteringColumn.class);
122 }
123
124 int getPosition() {
125 return position;
126 }
127
128 private String inferColumnName() {
129 Column column = annotation(Column.class);
130 if (column != null && !column.name().isEmpty()) {
131 return Metadata.quote(column.caseSensitive() ? column.name() : column.name().toLowerCase());
132 }
133 com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
134 if (udtField != null && !udtField.name().isEmpty()) {
135 return Metadata.quote(udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase());
136 }
137 if (isComputed()) {
138 return annotation(Computed.class).value();
139 }
140 return Metadata.quote(propertyName.toLowerCase());
141 }
142
143 @SuppressWarnings("unchecked")
144 private TypeToken<Object> inferJavaType() {
145 Type type;
146 if (getter != null)
147 type = getter.getGenericReturnType();
148 else
149 type = field.getGenericType();
150 return (TypeToken<Object>) TypeToken.of(type);
151 }
152
153 private int inferPosition() {
154 if (isPartitionKey()) {
155 return annotation(PartitionKey.class).value();
156 }
157 if (isClusteringColumn()) {
158 return annotation(ClusteringColumn.class).value();
159 }
160 return -1;
161 }
162
163 private TypeCodec<Object> createCustomCodec() {
164 Class<? extends TypeCodec<?>> codecClass = getCustomCodecClass();
165 if (codecClass.equals(Defaults.NoCodec.class))
166 return null;
167 @SuppressWarnings("unchecked")
168 TypeCodec<Object> instance = (TypeCodec<Object>) ReflectionUtils.newInstance(codecClass);
169 return instance;
170 }
171
172 private Class<? extends TypeCodec<?>> getCustomCodecClass() {
173 Column column = annotation(Column.class);
174 if (column != null)
175 return column.codec();
176 com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
177 if (udtField != null)
178 return udtField.codec();
179 return Defaults.NoCodec.class;
180 }
181
182 @Override
183 public String toString() {
184 return propertyName;
185 }
186
187}