| 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.collect.ComparisonChain; |
| 22 | import com.google.common.reflect.TypeToken; |
| 23 | |
| 24 | import java.beans.PropertyDescriptor; |
| 25 | import java.lang.annotation.Annotation; |
| 26 | import java.lang.reflect.Field; |
| 27 | import java.lang.reflect.Method; |
| 28 | import java.lang.reflect.Type; |
| 29 | import java.util.Collection; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | /** |
| 33 | * A bean property mapped to a table column or a UDT field. |
| 34 | * |
| 35 | * @param <T> The component classe where this property belongs (either |
| 36 | * a {@link com.datastax.driver.mapping.annotations.Table @Table} |
| 37 | * or {@link com.datastax.driver.mapping.annotations.UDT @UDT} |
| 38 | * annotated class). |
| 39 | */ |
| 40 | class MappedProperty<T> implements Comparable<MappedProperty<T>> { |
| 41 | |
| 42 | enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED} |
| 43 | |
| 44 | private final PropertyDescriptor property; |
| 45 | private final Field field; |
| 46 | private final Method getter; |
| 47 | private final Method setter; |
| 48 | private final Map<Class<? extends Annotation>, Annotation> annotations; |
| 49 | private final String columnName; |
| 50 | private final TypeToken<Object> type; |
| 51 | private final TypeCodec<Object> customCodec; |
| 52 | |
| 53 | MappedProperty(PropertyDescriptor property, Class<T> componentClass) { |
| 54 | this.property = property; |
| 55 | field = ReflectionUtils.findField(property.getName(), componentClass); |
| 56 | getter = ReflectionUtils.findGetter(property); |
| 57 | setter = ReflectionUtils.findSetter(property); |
| 58 | annotations = ReflectionUtils.findAnnotations(property, componentClass); |
| 59 | columnName = createColumnName(); |
| 60 | type = inferGenericType(); |
| 61 | customCodec = createCustomCodec(); |
| 62 | } |
| 63 | |
| 64 | boolean hasAnnotation(Class<? extends Annotation> annotationClass) { |
| 65 | return annotations.containsKey(annotationClass); |
| 66 | } |
| 67 | |
| 68 | Collection<Annotation> annotations() { |
| 69 | return annotations.values(); |
| 70 | } |
| 71 | |
| 72 | @SuppressWarnings("unchecked") |
| 73 | <A extends Annotation> A annotation(Class<A> annotationClass) { |
| 74 | return (A) annotations.get(annotationClass); |
| 75 | } |
| 76 | |
| 77 | boolean isComputed() { |
| 78 | return hasAnnotation(Computed.class); |
| 79 | } |
| 80 | |
| 81 | boolean isTransient() { |
| 82 | return hasAnnotation(Transient.class); |
| 83 | } |
| 84 | |
| 85 | boolean isPartitionKey() { |
| 86 | return hasAnnotation(PartitionKey.class); |
| 87 | } |
| 88 | |
| 89 | boolean isClusteringColumn() { |
| 90 | return hasAnnotation(ClusteringColumn.class); |
| 91 | } |
| 92 | |
| 93 | Method getter() { |
| 94 | return getter; |
| 95 | } |
| 96 | |
| 97 | Method setter() { |
| 98 | return setter; |
| 99 | } |
| 100 | |
| 101 | String name() { |
| 102 | return property.getName(); |
| 103 | } |
| 104 | |
| 105 | TypeToken<?> type() { |
| 106 | return type; |
| 107 | } |
| 108 | |
| 109 | String columnName() { |
| 110 | return columnName; |
| 111 | } |
| 112 | |
| 113 | TypeCodec<Object> customCodec() { |
| 114 | return customCodec; |
| 115 | } |
| 116 | |
| 117 | int position() { |
| 118 | if (isPartitionKey()) { |
| 119 | return annotation(PartitionKey.class).value(); |
| 120 | } |
| 121 | if (isClusteringColumn()) { |
| 122 | return annotation(ClusteringColumn.class).value(); |
| 123 | } |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | Kind kind() { |
| 128 | if (isPartitionKey()) { |
| 129 | return Kind.PARTITION_KEY; |
| 130 | } |
| 131 | if (isClusteringColumn()) { |
| 132 | return Kind.CLUSTERING_COLUMN; |
| 133 | } |
| 134 | if (isComputed()) { |
| 135 | return Kind.COMPUTED; |
| 136 | } |
| 137 | return Kind.REGULAR; |
| 138 | } |
| 139 | |
| 140 | private String createColumnName() { |
| 141 | Column column = annotation(Column.class); |
| 142 | if (column != null && !column.name().isEmpty()) { |
| 143 | return Metadata.quote(column.caseSensitive() ? column.name() : column.name().toLowerCase()); |
| 144 | } |
| 145 | com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); |
| 146 | if (udtField != null && !udtField.name().isEmpty()) { |
| 147 | return Metadata.quote(udtField.caseSensitive() ? udtField.name() : udtField.name().toLowerCase()); |
| 148 | } |
| 149 | if (isComputed()) { |
| 150 | return annotation(Computed.class).value(); |
| 151 | } |
| 152 | return Metadata.quote(name().toLowerCase()); |
| 153 | } |
| 154 | |
| 155 | @SuppressWarnings("unchecked") |
| 156 | private TypeToken<Object> inferGenericType() { |
| 157 | Type type; |
| 158 | if (field != null) |
| 159 | type = field.getGenericType(); |
| 160 | else if (getter != null) |
| 161 | type = getter.getGenericReturnType(); |
| 162 | else |
| 163 | // this will not work for generic types |
| 164 | type = property.getPropertyType(); |
| 165 | return (TypeToken<Object>) TypeToken.of(type); |
| 166 | } |
| 167 | |
| 168 | private TypeCodec<Object> createCustomCodec() { |
| 169 | Class<? extends TypeCodec<?>> codecClass = getCustomCodecClass(); |
| 170 | if (codecClass.equals(Defaults.NoCodec.class)) |
| 171 | return null; |
| 172 | try { |
| 173 | @SuppressWarnings("unchecked") |
| 174 | TypeCodec<Object> instance = (TypeCodec<Object>) codecClass.newInstance(); |
| 175 | return instance; |
| 176 | } catch (Exception e) { |
| 177 | throw new IllegalArgumentException(String.format( |
| 178 | "Cannot create an instance of custom codec %s for property %s", |
| 179 | codecClass, property |
| 180 | ), e); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private Class<? extends TypeCodec<?>> getCustomCodecClass() { |
| 185 | Column column = annotation(Column.class); |
| 186 | if (column != null) |
| 187 | return column.codec(); |
| 188 | com.datastax.driver.mapping.annotations.Field udtField = annotation(com.datastax.driver.mapping.annotations.Field.class); |
| 189 | if (udtField != null) |
| 190 | return udtField.codec(); |
| 191 | return Defaults.NoCodec.class; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int compareTo(MappedProperty<T> that) { |
| 196 | return ComparisonChain.start() |
| 197 | .compare(this.kind(), that.kind()) |
| 198 | .compare(this.position(), that.position()) |
| 199 | .compare(this.name(), that.name()) |
| 200 | .result(); |
| 201 | } |
| 202 | |
| 203 | } |