Elffuss BraidHub

kikocis/java-driver public

dd37f80f3022 · 6.7 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 final int position;
48
49 private final Field field;
50 private final Method getter;
51 private final Method setter;
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 (field != null)
62 ReflectionUtils.tryMakeAccessible(field);
63 if (getter != null)
64 ReflectionUtils.tryMakeAccessible(getter);
65 if (setter != null)
66 ReflectionUtils.tryMakeAccessible(setter);
67 if (!isTransient()) {
68 checkState((field != null && field.isAccessible()) || (getter != null && getter.isAccessible()),
69 "Property '%s' is not readable", propertyName);
70 checkState((field != null && field.isAccessible()) || (setter != null && setter.isAccessible()),
71 "Property '%s' is not writable", propertyName);
72 }
73 columnName = inferColumnName();
74 position = inferPosition();
75 javaType = inferJavaType();
76 customCodec = createCustomCodec();
77 }
78
79 Object getValue(Object entity) {
80 try {
81 // try getter first, if available, otherwise direct field access
82 if (getter != null && getter.isAccessible())
83 return getter.invoke(entity);
84 else
85 return field.get(entity);
86 } catch (Exception e) {
87 throw new IllegalStateException("Unable to read property '" + propertyName + "' in " + entity.getClass().getName(), e);
88 }
89 }
90
91 void setValue(Object entity, Object value) {
92 try {
93 // try setter first, if available, otherwise direct field access
94 if (setter != null && setter.isAccessible())
95 setter.invoke(entity, value);
96 else
97 field.set(entity, value);
98 } catch (Exception e) {
99 throw new IllegalStateException("Unable to write property '" + propertyName + "' in " + entity.getClass().getName(), e);
100 }
101 }
102
103 boolean hasAnnotation(Class<? extends Annotation> annotationClass) {
104 return annotations.containsKey(annotationClass);
105 }
106
107 Collection<Annotation> getAnnotations() {
108 return annotations.values();
109 }
110
111 @SuppressWarnings("unchecked")
112 <A extends Annotation> A annotation(Class<A> annotationClass) {
113 return (A) annotations.get(annotationClass);
114 }
115
116 boolean isComputed() {
117 return hasAnnotation(Computed.class);
118 }
119
120 boolean isTransient() {
121 return hasAnnotation(Transient.class);
122 }
123
124 boolean isPartitionKey() {
125 return hasAnnotation(PartitionKey.class);
126 }
127
128 boolean isClusteringColumn() {
129 return hasAnnotation(ClusteringColumn.class);
130 }
131
132 private String inferColumnName() {
133 Column column = annotation(Column.class);
134 if (column != null && !column.name().isEmpty()) {
135 return Metadata.quote(column.caseSensitive() ? column.name() : column.name().toLowerCase());
136 }
137 com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
138 if (udtField != null && !udtField.name().isEmpty()) {
139 return Metadata.quote(udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase());
140 }
141 if (isComputed()) {
142 return annotation(Computed.class).value();
143 }
144 return Metadata.quote(propertyName.toLowerCase());
145 }
146
147 @SuppressWarnings("unchecked")
148 private TypeToken<Object> inferJavaType() {
149 Type type;
150 if (getter != null)
151 type = getter.getGenericReturnType();
152 else
153 type = field.getGenericType();
154 return (TypeToken<Object>) TypeToken.of(type);
155 }
156
157 private int inferPosition() {
158 if (isPartitionKey()) {
159 return annotation(PartitionKey.class).value();
160 }
161 if (isClusteringColumn()) {
162 return annotation(ClusteringColumn.class).value();
163 }
164 return -1;
165 }
166
167 private TypeCodec<Object> createCustomCodec() {
168 Class<? extends TypeCodec<?>> codecClass = getCustomCodecClass();
169 if (codecClass.equals(Defaults.NoCodec.class))
170 return null;
171 @SuppressWarnings("unchecked")
172 TypeCodec<Object> instance = (TypeCodec<Object>) ReflectionUtils.newInstance(codecClass);
173 return instance;
174 }
175
176 private Class<? extends TypeCodec<?>> getCustomCodecClass() {
177 Column column = annotation(Column.class);
178 if (column != null)
179 return column.codec();
180 com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class);
181 if (udtField != null)
182 return udtField.codec();
183 return Defaults.NoCodec.class;
184 }
185
186 @Override
187 public String toString() {
188 return propertyName;
189 }
190
191}