| 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 java.beans.IntrospectionException; |
| 19 | import java.beans.PropertyDescriptor; |
| 20 | import java.lang.reflect.Field; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | |
| 26 | import com.google.common.reflect.TypeToken; |
| 27 | |
| 28 | import com.datastax.driver.core.ConsistencyLevel; |
| 29 | |
| 30 | /** |
| 31 | * An {@link EntityMapper} implementation that use reflection to read and write fields |
| 32 | * of an entity. |
| 33 | */ |
| 34 | class ReflectionMapper<T> extends EntityMapper<T> { |
| 35 | |
| 36 | private static ReflectionFactory factory = new ReflectionFactory(); |
| 37 | |
| 38 | private ReflectionMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 39 | super(entityClass, keyspace, table, writeConsistency, readConsistency); |
| 40 | } |
| 41 | |
| 42 | public static Factory factory() { |
| 43 | return factory; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public T newEntity() { |
| 48 | try { |
| 49 | return entityClass.newInstance(); |
| 50 | } catch (Exception e) { |
| 51 | throw new RuntimeException("Can't create an instance of " + entityClass.getName()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private static class LiteralMapper<T> extends ColumnMapper<T> { |
| 56 | |
| 57 | private final Method readMethod; |
| 58 | private final Method writeMethod; |
| 59 | |
| 60 | private LiteralMapper(Field field, int position, PropertyDescriptor pd, AtomicInteger columnCounter) { |
| 61 | super(field, position, columnCounter); |
| 62 | this.readMethod = pd.getReadMethod(); |
| 63 | this.writeMethod = pd.getWriteMethod(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Object getValue(T entity) { |
| 68 | try { |
| 69 | return readMethod.invoke(entity); |
| 70 | } catch (IllegalArgumentException e) { |
| 71 | throw new IllegalArgumentException("Could not get field '" + fieldName + "'"); |
| 72 | } catch (Exception e) { |
| 73 | throw new IllegalStateException("Unable to access getter for '" + fieldName + "' in " + entity.getClass().getName(), e); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void setValue(Object entity, Object value) { |
| 79 | try { |
| 80 | writeMethod.invoke(entity, value); |
| 81 | } catch (IllegalArgumentException e) { |
| 82 | throw new IllegalArgumentException("Could not set field '" + fieldName + "' to value '" + value + "'"); |
| 83 | } catch (Exception e) { |
| 84 | throw new IllegalStateException("Unable to access setter for '" + fieldName + "' in " + entity.getClass().getName(), e); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private static class EnumMapper<T> extends LiteralMapper<T> { |
| 90 | |
| 91 | private final EnumType enumType; |
| 92 | private final Map<String, Object> fromString; |
| 93 | |
| 94 | private EnumMapper(Field field, int position, PropertyDescriptor pd, EnumType enumType, AtomicInteger columnCounter) { |
| 95 | super(field, position, pd, columnCounter); |
| 96 | this.enumType = enumType; |
| 97 | |
| 98 | if (enumType == EnumType.STRING) { |
| 99 | fromString = new HashMap<String, Object>(fieldType.getRawType().getEnumConstants().length); |
| 100 | for (Object constant : fieldType.getRawType().getEnumConstants()) |
| 101 | fromString.put(constant.toString().toLowerCase(), constant); |
| 102 | |
| 103 | } else { |
| 104 | fromString = null; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @SuppressWarnings("rawtypes") |
| 109 | @Override |
| 110 | public Object getValue(T entity) { |
| 111 | Object value = super.getValue(entity); |
| 112 | switch (enumType) { |
| 113 | case STRING: |
| 114 | return (value == null) ? null : value.toString(); |
| 115 | case ORDINAL: |
| 116 | return (value == null) ? null : ((Enum)value).ordinal(); |
| 117 | } |
| 118 | throw new AssertionError(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void setValue(Object entity, Object value) { |
| 123 | Object converted = null; |
| 124 | switch (enumType) { |
| 125 | case STRING: |
| 126 | converted = fromString.get(value.toString().toLowerCase()); |
| 127 | break; |
| 128 | case ORDINAL: |
| 129 | converted = fieldType.getRawType().getEnumConstants()[(Integer)value]; |
| 130 | break; |
| 131 | } |
| 132 | super.setValue(entity, converted); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public TypeToken<Object> getPivotType() { |
| 137 | return enumType.pivotType; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private static class ReflectionFactory implements Factory { |
| 142 | |
| 143 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 144 | return new ReflectionMapper<T>(entityClass, keyspace, table, writeConsistency, readConsistency); |
| 145 | } |
| 146 | |
| 147 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 148 | public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter) { |
| 149 | String fieldName = field.getName(); |
| 150 | try { |
| 151 | PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass()); |
| 152 | |
| 153 | if (field.getType().isEnum()) { |
| 154 | return new EnumMapper<T>(field, position, pd, AnnotationParser.enumType(field), columnCounter); |
| 155 | } |
| 156 | |
| 157 | for (Class<?> udt : TypeMappings.findUDTs(field.getGenericType())) |
| 158 | mappingManager.getUDTCodec(udt); |
| 159 | |
| 160 | return new LiteralMapper<T>(field, position, pd, columnCounter); |
| 161 | |
| 162 | } catch (IntrospectionException e) { |
| 163 | throw new IllegalArgumentException("Cannot find matching getter and setter for field '" + fieldName + "'"); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |