| 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.google.common.base.Throwables; |
| 19 | |
| 20 | import java.beans.BeanInfo; |
| 21 | import java.beans.IntrospectionException; |
| 22 | import java.beans.Introspector; |
| 23 | import java.beans.PropertyDescriptor; |
| 24 | import java.lang.annotation.Annotation; |
| 25 | import java.lang.reflect.Field; |
| 26 | import java.lang.reflect.Method; |
| 27 | import java.lang.reflect.Modifier; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | /** |
| 32 | * Utility methods related to reflection. |
| 33 | */ |
| 34 | class ReflectionUtils { |
| 35 | |
| 36 | static <T> BeanInfo getBeanInfo(Class<T> udtClass) { |
| 37 | BeanInfo beanInfo; |
| 38 | try { |
| 39 | beanInfo = Introspector.getBeanInfo(udtClass); |
| 40 | } catch (IntrospectionException e) { |
| 41 | throw Throwables.propagate(e); |
| 42 | } |
| 43 | return beanInfo; |
| 44 | } |
| 45 | |
| 46 | static Map<Class<? extends Annotation>, Annotation> findAnnotations(PropertyDescriptor property, Class<?> baseClass) { |
| 47 | // try field |
| 48 | Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(); |
| 49 | Field field = findField(property.getName(), baseClass); |
| 50 | if (field != null) { |
| 51 | for (Annotation annotation : field.getAnnotations()) { |
| 52 | annotations.put(annotation.annotationType(), annotation); |
| 53 | } |
| 54 | } |
| 55 | // try getter |
| 56 | Method getter = findGetter(property); |
| 57 | if (getter != null) { |
| 58 | for (Annotation annotation : getter.getAnnotations()) { |
| 59 | annotations.put(annotation.annotationType(), annotation); |
| 60 | } |
| 61 | } |
| 62 | return annotations; |
| 63 | } |
| 64 | |
| 65 | static Method findGetter(PropertyDescriptor property) { |
| 66 | Method getter = property.getReadMethod(); |
| 67 | if (getter == null) |
| 68 | return null; |
| 69 | getter.setAccessible(true); |
| 70 | return getter; |
| 71 | } |
| 72 | |
| 73 | static Method findSetter(PropertyDescriptor property) { |
| 74 | Method setter = property.getWriteMethod(); |
| 75 | if (setter == null) |
| 76 | return null; |
| 77 | setter.setAccessible(true); |
| 78 | return setter; |
| 79 | } |
| 80 | |
| 81 | static Field findField(String name, Class<?> baseClass) { |
| 82 | for (Class<?> clazz = baseClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 83 | try { |
| 84 | Field field = clazz.getDeclaredField(name); |
| 85 | if (field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) |
| 86 | continue; |
| 87 | field.setAccessible(true); |
| 88 | return field; |
| 89 | } catch (NoSuchFieldException e) { |
| 90 | //ok |
| 91 | } |
| 92 | } |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | } |