Elffuss BraidHub

kikocis/java-driver public

a3674bcf4e77 · 4.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 java.beans.IntrospectionException;
19import java.beans.PropertyDescriptor;
20import java.lang.reflect.Field;
21import java.lang.reflect.Method;
22import java.util.concurrent.atomic.AtomicInteger;
23
24import com.datastax.driver.core.ConsistencyLevel;
25
26/**
27 * An {@link EntityMapper} implementation that use reflection to read and write fields
28 * of an entity.
29 */
30class 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 public 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 public 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 public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) {
88 return new ReflectionMapper<T>(entityClass, keyspace, table, writeConsistency, readConsistency);
89 }
90
91 @SuppressWarnings({ "unchecked", "rawtypes" })
92 public <T> ColumnMapper<T> createColumnMapper(Class<T> entityClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter) {
93 String fieldName = field.getName();
94 try {
95 PropertyDescriptor pd = new PropertyDescriptor(fieldName, field.getDeclaringClass());
96
97 for (Class<?> udt : TypeMappings.findUDTs(field.getGenericType()))
98 mappingManager.getUDTCodec(udt);
99
100 return new LiteralMapper<T>(field, position, pd, columnCounter);
101
102 } catch (IntrospectionException e) {
103 throw new IllegalArgumentException("Cannot find matching getter and setter for field '" + fieldName + "'");
104 }
105 }
106 }
107}