| 1 | /* |
| 2 | * Copyright (C) 2012-2014 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.lang.reflect.InvocationHandler; |
| 19 | import java.lang.reflect.Method; |
| 20 | import java.util.*; |
| 21 | |
| 22 | class AccessorInvocationHandler<T> implements InvocationHandler { |
| 23 | |
| 24 | private static final Object[] NO_ARGS = new Object[0]; |
| 25 | |
| 26 | private final AccessorMapper<T> mapper; |
| 27 | |
| 28 | private final Map<Method, MethodMapper> methodMap = new HashMap<Method, MethodMapper>(); |
| 29 | |
| 30 | AccessorInvocationHandler(AccessorMapper<T> mapper) { |
| 31 | this.mapper = mapper; |
| 32 | |
| 33 | for (MethodMapper method : mapper.methods) |
| 34 | methodMap.put(method.method, method); |
| 35 | } |
| 36 | |
| 37 | public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { |
| 38 | |
| 39 | MethodMapper method = methodMap.get(m); |
| 40 | if (mapper == null) |
| 41 | throw new UnsupportedOperationException(); |
| 42 | |
| 43 | return method.invoke(args == null ? NO_ARGS : args); |
| 44 | } |
| 45 | } |
| 46 | |