Elffuss BraidHub

kikocis/java-driver public

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