| 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.datastax.driver.mapping.annotations.*; |
| 19 | |
| 20 | import java.lang.annotation.Annotation; |
| 21 | import java.lang.reflect.Field; |
| 22 | import java.lang.reflect.ParameterizedType; |
| 23 | import java.lang.reflect.Type; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * Various checks on mapping annotations. |
| 28 | */ |
| 29 | class AnnotationChecks { |
| 30 | |
| 31 | // The package containing the mapping annotations |
| 32 | private static final Package MAPPING_PACKAGE = Table.class.getPackage(); |
| 33 | |
| 34 | /** |
| 35 | * Checks that a class is decorated with the given annotation, and return the annotation instance. |
| 36 | * Also validates that no other mapping annotation is present. |
| 37 | */ |
| 38 | static <T extends Annotation> T getTypeAnnotation(Class<T> annotation, Class<?> annotatedClass) { |
| 39 | T instance = annotatedClass.getAnnotation(annotation); |
| 40 | if (instance == null) |
| 41 | throw new IllegalArgumentException(String.format("@%s annotation was not found on type %s", |
| 42 | annotation.getSimpleName(), annotatedClass.getName())); |
| 43 | |
| 44 | // Check that no other mapping annotations are present |
| 45 | validateAnnotations(annotatedClass, annotation); |
| 46 | |
| 47 | return instance; |
| 48 | } |
| 49 | |
| 50 | private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) { |
| 51 | @SuppressWarnings("unchecked") |
| 52 | Class<? extends Annotation> invalid = validateAnnotations(clazz.getAnnotations(), allowed); |
| 53 | if (invalid != null) |
| 54 | throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on type %s", |
| 55 | allowed.getSimpleName(), invalid.getSimpleName(), |
| 56 | clazz.getName())); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid. |
| 61 | */ |
| 62 | static void validateAnnotations(Field field, String classDescription, Class<? extends Annotation>... allowed) { |
| 63 | Class<? extends Annotation> invalid = validateAnnotations(field.getAnnotations(), allowed); |
| 64 | if (invalid != null) |
| 65 | throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on field %s of %s %s", |
| 66 | invalid.getSimpleName(), |
| 67 | field.getName(), classDescription, |
| 68 | field.getDeclaringClass().getName())); |
| 69 | |
| 70 | if (field.getAnnotation(Transient.class) == null) { |
| 71 | try { |
| 72 | checkFrozenTypes(field); |
| 73 | } catch (IllegalArgumentException e) { |
| 74 | throw new IllegalArgumentException(String.format("Error while checking frozen types on field %s of %s %s: %s", |
| 75 | field.getName(), classDescription, |
| 76 | field.getDeclaringClass().getName(), e.getMessage())); |
| 77 | } |
| 78 | } |
| 79 | checkValidComputed(field); |
| 80 | } |
| 81 | |
| 82 | // Returns the offending annotation if there is one |
| 83 | private static Class<? extends Annotation> validateAnnotations(Annotation[] annotations, Class<? extends Annotation>... allowed) { |
| 84 | for (Annotation annotation : annotations) { |
| 85 | Class<? extends Annotation> actual = annotation.annotationType(); |
| 86 | if (actual.getPackage().equals(MAPPING_PACKAGE) && !contains(allowed, actual)) |
| 87 | return actual; |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | private static boolean contains(Object[] array, Object target) { |
| 93 | for (Object element : array) |
| 94 | if (element.equals(target)) |
| 95 | return true; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | static void checkFrozenTypes(Field field) { |
| 100 | Type javaType = field.getGenericType(); |
| 101 | DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field); |
| 102 | checkFrozenTypes(javaType, declaredFrozenType); |
| 103 | } |
| 104 | |
| 105 | static void checkValidComputed(Field field) { |
| 106 | Computed computed = field.getAnnotation(Computed.class); |
| 107 | if (computed != null && computed.value().isEmpty()) { |
| 108 | throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName())); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field. |
| 113 | private static DeclaredFrozenType getDeclaredFrozenType(Field field) { |
| 114 | Frozen frozen = field.getAnnotation(Frozen.class); |
| 115 | if (frozen != null) |
| 116 | return DeclaredFrozenType.parse(frozen.value()); |
| 117 | |
| 118 | boolean frozenKey = field.getAnnotation(FrozenKey.class) != null; |
| 119 | boolean frozenValue = field.getAnnotation(FrozenValue.class) != null; |
| 120 | if (frozenKey && frozenValue) |
| 121 | return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE; |
| 122 | else if (frozenKey) |
| 123 | return DeclaredFrozenType.FROZEN_MAP_KEY; |
| 124 | else if (frozenValue && field.getType().equals(Map.class)) |
| 125 | return DeclaredFrozenType.FROZEN_MAP_VALUE; |
| 126 | else if (frozenValue) |
| 127 | return DeclaredFrozenType.FROZEN_ELEMENT; |
| 128 | else |
| 129 | return DeclaredFrozenType.UNFROZEN_SIMPLE; |
| 130 | } |
| 131 | |
| 132 | // Traverses the Java type and CQLType hierarchies in parallel, to ensure that all |
| 133 | // Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType. |
| 134 | // We accept that parts of the CQLType be null, in which case the matching parts |
| 135 | // in the Java type will be considered as not frozen. |
| 136 | private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) { |
| 137 | checkFrozenTypes(javaType, declaredFrozenType, true); |
| 138 | } |
| 139 | |
| 140 | private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) { |
| 141 | Class<?> javaClass; |
| 142 | Type[] childrenJavaTypes; |
| 143 | if (javaType instanceof Class<?>) { |
| 144 | javaClass = (Class<?>) javaType; |
| 145 | childrenJavaTypes = null; |
| 146 | } else if (javaType instanceof ParameterizedType) { |
| 147 | ParameterizedType pt = (ParameterizedType) javaType; |
| 148 | javaClass = (Class<?>) pt.getRawType(); |
| 149 | childrenJavaTypes = pt.getActualTypeArguments(); |
| 150 | } else |
| 151 | throw new IllegalArgumentException("unexpected type: " + javaType); |
| 152 | |
| 153 | boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen); |
| 154 | checkValidFrozen(javaClass, isRoot, frozen); |
| 155 | |
| 156 | if (childrenJavaTypes != null) { |
| 157 | for (int i = 0; i < childrenJavaTypes.length; i++) { |
| 158 | Type childJavaType = childrenJavaTypes[i]; |
| 159 | DeclaredFrozenType childDeclaredFrozenType = null; |
| 160 | if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i) |
| 161 | childDeclaredFrozenType = declaredFrozenType.subTypes.get(i); |
| 162 | checkFrozenTypes(childJavaType, childDeclaredFrozenType, false); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) { |
| 168 | boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot) |
| 169 | || TypeMappings.mapsToUserTypeOrTuple(clazz); |
| 170 | if (shouldBeFrozen != isDeclaredFrozen) |
| 171 | throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen", |
| 172 | clazz.getSimpleName(), |
| 173 | shouldBeFrozen ? "" : "not ", |
| 174 | isDeclaredFrozen ? "" : "not ")); |
| 175 | } |
| 176 | } |