| 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.Constructor; |
| 26 | import java.lang.reflect.Field; |
| 27 | import java.lang.reflect.Method; |
| 28 | import java.lang.reflect.Modifier; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | /** |
| 33 | * Utility methods related to reflection. |
| 34 | */ |
| 35 | class ReflectionUtils { |
| 36 | |
| 37 | static <T> T newInstance(Class<T> clazz) { |
| 38 | try { |
| 39 | return clazz.newInstance(); |
| 40 | } catch (Exception e) { |
| 41 | try { |
| 42 | // try private constructor |
| 43 | Constructor<T> constructor = clazz.getDeclaredConstructor(); |
| 44 | constructor.setAccessible(true); |
| 45 | return constructor.newInstance(); |
| 46 | } catch (Exception e1) { |
| 47 | throw new IllegalArgumentException("Can't create an instance of " + clazz.getName()); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static <T> Map<String, Object[]> scanFieldsAndProperties(Class<T> baseClass) { |
| 53 | Map<String, Object[]> fieldsAndProperties = new HashMap<String, Object[]>(); |
| 54 | Map<String, Field> fields = scanFields(baseClass); |
| 55 | for (Map.Entry<String, Field> entry : fields.entrySet()) { |
| 56 | fieldsAndProperties.put(entry.getKey(), new Object[]{entry.getValue(), null}); |
| 57 | } |
| 58 | Map<String, PropertyDescriptor> properties = scanProperties(baseClass); |
| 59 | for (Map.Entry<String, PropertyDescriptor> entry : properties.entrySet()) { |
| 60 | Object[] value = fieldsAndProperties.get(entry.getKey()); |
| 61 | if (value == null) |
| 62 | fieldsAndProperties.put(entry.getKey(), new Object[]{null, entry.getValue()}); |
| 63 | else value[1] = entry.getValue(); |
| 64 | } |
| 65 | return fieldsAndProperties; |
| 66 | } |
| 67 | |
| 68 | private static <T> Map<String, Field> scanFields(Class<T> baseClass) { |
| 69 | HashMap<String, Field> fields = new HashMap<String, Field>(); |
| 70 | for (Class<?> clazz = baseClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 71 | for (Field field : clazz.getDeclaredFields()) { |
| 72 | if (field.getName().equals("class") || field.isSynthetic() || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) |
| 73 | continue; |
| 74 | field.setAccessible(true); |
| 75 | // never override a more specific field masking another one declared in a superclass |
| 76 | if (!fields.containsKey(field.getName())) |
| 77 | fields.put(field.getName(), field); |
| 78 | } |
| 79 | } |
| 80 | return fields; |
| 81 | } |
| 82 | |
| 83 | private static <T> Map<String, PropertyDescriptor> scanProperties(Class<T> baseClass) { |
| 84 | BeanInfo beanInfo; |
| 85 | try { |
| 86 | beanInfo = Introspector.getBeanInfo(baseClass); |
| 87 | } catch (IntrospectionException e) { |
| 88 | throw Throwables.propagate(e); |
| 89 | } |
| 90 | Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); |
| 91 | for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { |
| 92 | if (property.getName().equals("class")) |
| 93 | continue; |
| 94 | properties.put(property.getName(), property); |
| 95 | } |
| 96 | return properties; |
| 97 | } |
| 98 | |
| 99 | static Map<Class<? extends Annotation>, Annotation> scanPropertyAnnotations(Field field, PropertyDescriptor property) { |
| 100 | Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(); |
| 101 | // annotations on getters should have precedence over annotations on fields |
| 102 | scanFieldAnnotations(field, annotations); |
| 103 | if (property != null) { |
| 104 | Method getter = property.getReadMethod(); |
| 105 | scanMethodAnnotations(getter, annotations); |
| 106 | } |
| 107 | return annotations; |
| 108 | } |
| 109 | |
| 110 | private static Map<Class<? extends Annotation>, Annotation> scanFieldAnnotations(Field field, Map<Class<? extends Annotation>, Annotation> annotations) { |
| 111 | if (field != null) { |
| 112 | for (Annotation annotation : field.getAnnotations()) { |
| 113 | annotations.put(annotation.annotationType(), annotation); |
| 114 | } |
| 115 | } |
| 116 | return annotations; |
| 117 | } |
| 118 | |
| 119 | private static Map<Class<? extends Annotation>, Annotation> scanMethodAnnotations(Method method, Map<Class<? extends Annotation>, Annotation> annotations) { |
| 120 | if (method != null) { |
| 121 | // 1. direct method annotations |
| 122 | for (Annotation annotation : method.getAnnotations()) { |
| 123 | annotations.put(annotation.annotationType(), annotation); |
| 124 | } |
| 125 | // 2. Class hierarchy: check for annotations in overridden methods in superclasses |
| 126 | Class<?> getterClass = method.getDeclaringClass(); |
| 127 | for (Class<?> clazz = getterClass.getSuperclass(); !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 128 | maybeAddOverriddenMethodAnnotations(annotations, method, clazz); |
| 129 | } |
| 130 | // 3. Interfaces: check for annotations in implemented interfaces |
| 131 | for (Class<?> clazz = getterClass; !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { |
| 132 | for (Class<?> itf : clazz.getInterfaces()) { |
| 133 | maybeAddOverriddenMethodAnnotations(annotations, method, itf); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | return annotations; |
| 138 | } |
| 139 | |
| 140 | private static void maybeAddOverriddenMethodAnnotations(Map<Class<? extends Annotation>, Annotation> annotations, Method getter, Class<?> clazz) { |
| 141 | try { |
| 142 | Method overriddenGetter = clazz.getDeclaredMethod(getter.getName(), getter.getParameterTypes()); |
| 143 | for (Annotation annotation : overriddenGetter.getAnnotations()) { |
| 144 | // do not override a more specific version of the annotation type being scanned |
| 145 | if (!annotations.containsKey(annotation.annotationType())) |
| 146 | annotations.put(annotation.annotationType(), annotation); |
| 147 | } |
| 148 | } catch (NoSuchMethodException e) { |
| 149 | //ok |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static Method findGetter(PropertyDescriptor property) { |
| 154 | if (property == null) |
| 155 | return null; |
| 156 | Method getter = property.getReadMethod(); |
| 157 | if (getter == null) |
| 158 | return null; |
| 159 | getter.setAccessible(true); |
| 160 | return getter; |
| 161 | } |
| 162 | |
| 163 | static Method findSetter(PropertyDescriptor property) { |
| 164 | if (property == null) |
| 165 | return null; |
| 166 | Method setter = property.getWriteMethod(); |
| 167 | if (setter == null) |
| 168 | return null; |
| 169 | setter.setAccessible(true); |
| 170 | return setter; |
| 171 | } |
| 172 | |
| 173 | } |