Elffuss BraidHub

kikocis/java-driver public

f6efaebfde68 · 1.9 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.lang.reflect.Proxy;
19import java.util.List;
20
21class AccessorReflectionMapper<T> extends AccessorMapper<T> {
22
23 private static AccessorReflectionFactory factory = new AccessorReflectionFactory();
24
25 private final Class<T>[] proxyClasses;
26 private final AccessorInvocationHandler<T> handler;
27
28 @SuppressWarnings({"unchecked", "rawtypes"})
29 private AccessorReflectionMapper(Class<T> daoClass, List<MethodMapper> methods) {
30 super(daoClass, methods);
31 this.proxyClasses = (Class<T>[]) new Class[]{daoClass};
32 this.handler = new AccessorInvocationHandler<T>(this);
33 }
34
35 public static Factory factory() {
36 return factory;
37 }
38
39 @SuppressWarnings("unchecked")
40 @Override
41 public T createProxy() {
42 try {
43 return (T) Proxy.newProxyInstance(daoClass.getClassLoader(), proxyClasses, handler);
44 } catch (Exception e) {
45 throw new RuntimeException("Cannot create instance for Accessor interface " + daoClass.getName());
46 }
47 }
48
49 private static class AccessorReflectionFactory implements Factory {
50 @Override
51 public <T> AccessorMapper<T> create(Class<T> daoClass, List<MethodMapper> methods) {
52 return new AccessorReflectionMapper<T>(daoClass, methods);
53 }
54 }
55}