Elffuss BraidHub

kikocis/java-driver public

845bfcad3488 · 3.5 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 com.datastax.driver.core.ConsistencyLevel;
19
20import java.util.concurrent.atomic.AtomicInteger;
21
22/**
23 * An {@link EntityMapper} implementation that use reflection to read and write fields
24 * of an entity.
25 */
26class ReflectionMapper<T> extends EntityMapper<T> {
27
28 private static final ReflectionFactory factory = new ReflectionFactory();
29
30 private ReflectionMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) {
31 super(entityClass, keyspace, table, writeConsistency, readConsistency);
32 }
33
34 public static Factory factory() {
35 return factory;
36 }
37
38 @Override
39 public T newEntity() {
40 try {
41 return entityClass.newInstance();
42 } catch (Exception e) {
43 throw new RuntimeException("Can't create an instance of " + entityClass.getName());
44 }
45 }
46
47 private static class LiteralMapper<T> extends ColumnMapper<T> {
48
49 private LiteralMapper(MappedProperty<T> property, int position, AtomicInteger columnCounter) {
50 super(property, position, columnCounter);
51 }
52
53 @Override
54 Object getValue(T entity) {
55 try {
56 return property.getter().invoke(entity);
57 } catch (IllegalArgumentException e) {
58 throw new IllegalArgumentException("Could not get field '" + property.name() + "'");
59 } catch (Exception e) {
60 throw new IllegalStateException("Unable to access getter for '" + property.name() + "' in " + entity.getClass().getName(), e);
61 }
62 }
63
64 @Override
65 void setValue(Object entity, Object value) {
66 try {
67 property.setter().invoke(entity, value);
68 } catch (IllegalArgumentException e) {
69 throw new IllegalArgumentException("Could not set field '" + property.name() + "' to value '" + value + "'");
70 } catch (Exception e) {
71 throw new IllegalStateException("Unable to access setter for '" + property.name() + "' in " + entity.getClass().getName(), e);
72 }
73 }
74 }
75
76 private static class ReflectionFactory implements Factory {
77
78 @Override
79 public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) {
80 return new ReflectionMapper<T>(entityClass, keyspace, table, writeConsistency, readConsistency);
81 }
82
83 @Override
84 public <T> ColumnMapper<T> createColumnMapper(MappedProperty<T> property, int position, MappingManager mappingManager, AtomicInteger columnCounter) {
85 for (Class<?> udt : TypeMappings.findUDTs(property.type().getType()))
86 mappingManager.getUDTCodec(udt);
87 return new LiteralMapper<T>(property, position, columnCounter);
88 }
89 }
90}