| 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.ConsistencyLevel; |
| 19 | |
| 20 | import java.beans.IntrospectionException; |
| 21 | import java.beans.PropertyDescriptor; |
| 22 | import java.lang.reflect.Field; |
| 23 | import java.lang.reflect.Method; |
| 24 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | |
| 26 | /** |
| 27 | * An {@link EntityMapper} implementation that use reflection to read and write fields |
| 28 | * of an entity. |
| 29 | */ |
| 30 | class ReflectionMapper<T> extends EntityMapper<T> { |
| 31 | |
| 32 | private static ReflectionFactory factory = new ReflectionFactory(); |
| 33 | |
| 34 | private ReflectionMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 35 | super(entityClass, keyspace, table, writeConsistency, readConsistency); |
| 36 | } |
| 37 | |
| 38 | public static Factory factory() { |
| 39 | return factory; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public T newEntity() { |
| 44 | try { |
| 45 | return entityClass.newInstance(); |
| 46 | } catch (Exception e) { |
| 47 | throw new RuntimeException("Can't create an instance of " + entityClass.getName()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private static class LiteralMapper<T> extends ColumnMapper<T> { |
| 52 | |
| 53 | private final Method readMethod; |
| 54 | private final Method writeMethod; |
| 55 | |
| 56 | private LiteralMapper(Field field, int position, PropertyDescriptor pd, AtomicInteger columnCounter) { |
| 57 | super(field, position, columnCounter); |
| 58 | this.readMethod = pd.getReadMethod(); |
| 59 | this.writeMethod = pd.getWriteMethod(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | Object getValue(T entity) { |
| 64 | try { |
| 65 | return readMethod.invoke(entity); |
| 66 | } catch (IllegalArgumentException e) { |
| 67 | throw new IllegalArgumentException("Could not get field '" + fieldName + "'"); |
| 68 | } catch (Exception e) { |
| 69 | throw new IllegalStateException("Unable to access getter for '" + fieldName + "' in " + entity.getClass().getName(), e); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | void setValue(Object entity, Object value) { |
| 75 | try { |
| 76 | writeMethod.invoke(entity, value); |
| 77 | } catch (IllegalArgumentException e) { |
| 78 | throw new IllegalArgumentException("Could not set field '" + fieldName + "' to value '" + value + "'"); |
| 79 | } catch (Exception e) { |
| 80 | throw new IllegalStateException("Unable to access setter for '" + fieldName + "' in " + entity.getClass().getName(), e); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static class ReflectionFactory implements Factory { |
| 86 | |
| 87 | @Override |
| 88 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 89 | return new ReflectionMapper<T>(entityClass, keyspace, table, writeConsistency, readConsistency); |
| 90 | } |
| 91 | |
| 92 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 93 | @Override |
| 94 | public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter) { |
| 95 | String fieldName = field.getName(); |
| 96 | try { |
| 97 | PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass()); |
| 98 | |
| 99 | for (Class<?> udt : TypeMappings.findUDTs(field.getGenericType())) |
| 100 | mappingManager.getUDTCodec(udt); |
| 101 | |
| 102 | return new LiteralMapper<T>(field, position, pd, columnCounter); |
| 103 | |
| 104 | } catch (IntrospectionException e) { |
| 105 | throw new IllegalArgumentException("Cannot find matching getter and setter for field '" + fieldName + "'"); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |