| 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 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.Metadata; |
| 19 | import com.datastax.driver.core.TypeCodec; |
| 20 | import com.datastax.driver.mapping.annotations.*; |
| 21 | import com.google.common.reflect.TypeToken; |
| 22 | |
| 23 | import java.beans.PropertyDescriptor; |
| 24 | import java.lang.annotation.Annotation; |
| 25 | import java.lang.reflect.Field; |
| 26 | import java.lang.reflect.Method; |
| 27 | import java.lang.reflect.Type; |
| 28 | import java.util.Collection; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 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 | */ |
| 40 | class 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(Class<?> baseClass, 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(baseClass, 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 | checkArgument((field != null && field.isAccessible()) || (getter != null && getter.isAccessible()), |
| 69 | "Property '%s' is not readable", propertyName); |
| 70 | checkArgument((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 IllegalArgumentException("Unable to read property '" + propertyName + "' in " + entity.getClass(), 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 IllegalArgumentException("Unable to write property '" + propertyName + "' in " + entity.getClass(), 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 | if (isComputed()) { |
| 134 | return annotation(Computed.class).value(); |
| 135 | } |
| 136 | boolean caseSensitive = false; |
| 137 | String columnName = propertyName; |
| 138 | if (hasAnnotation(Column.class)) { |
| 139 | Column column = annotation(Column.class); |
| 140 | caseSensitive = column.caseSensitive(); |
| 141 | if (!column.name().isEmpty()) |
| 142 | columnName = column.name(); |
| 143 | } else if (hasAnnotation(com.datastax.driver.mapping.annotations.Field.class)) { |
| 144 | com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); |
| 145 | caseSensitive = udtField.caseSensitive(); |
| 146 | if (!udtField.name().isEmpty()) |
| 147 | columnName = udtField.name(); |
| 148 | } |
| 149 | return caseSensitive ? Metadata.quote(columnName) : columnName.toLowerCase(); |
| 150 | } |
| 151 | |
| 152 | @SuppressWarnings("unchecked") |
| 153 | private TypeToken<Object> inferJavaType() { |
| 154 | Type type; |
| 155 | if (getter != null) |
| 156 | type = getter.getGenericReturnType(); |
| 157 | else |
| 158 | type = field.getGenericType(); |
| 159 | return (TypeToken<Object>) TypeToken.of(type); |
| 160 | } |
| 161 | |
| 162 | private int inferPosition() { |
| 163 | if (isPartitionKey()) { |
| 164 | return annotation(PartitionKey.class).value(); |
| 165 | } |
| 166 | if (isClusteringColumn()) { |
| 167 | return annotation(ClusteringColumn.class).value(); |
| 168 | } |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | private TypeCodec<Object> createCustomCodec() { |
| 173 | Class<? extends TypeCodec<?>> codecClass = getCustomCodecClass(); |
| 174 | if (codecClass.equals(Defaults.NoCodec.class)) |
| 175 | return null; |
| 176 | @SuppressWarnings("unchecked") |
| 177 | TypeCodec<Object> instance = (TypeCodec<Object>) ReflectionUtils.newInstance(codecClass); |
| 178 | return instance; |
| 179 | } |
| 180 | |
| 181 | private Class<? extends TypeCodec<?>> getCustomCodecClass() { |
| 182 | Column column = annotation(Column.class); |
| 183 | if (column != null) |
| 184 | return column.codec(); |
| 185 | com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); |
| 186 | if (udtField != null) |
| 187 | return udtField.codec(); |
| 188 | return Defaults.NoCodec.class; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | return propertyName; |
| 194 | } |
| 195 | |
| 196 | } |