| 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.Column; |
| 19 | import com.datastax.driver.mapping.annotations.Computed; |
| 20 | import com.datastax.driver.mapping.annotations.Table; |
| 21 | |
| 22 | import java.lang.annotation.Annotation; |
| 23 | import java.util.Collection; |
| 24 | import java.util.Collections; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Various checks on mapping annotations. |
| 30 | */ |
| 31 | class AnnotationChecks { |
| 32 | |
| 33 | // The package containing the mapping annotations |
| 34 | private static final Package MAPPING_PACKAGE = Table.class.getPackage(); |
| 35 | |
| 36 | /** |
| 37 | * Checks that a class is decorated with the given annotation, and return the annotation instance. |
| 38 | * Also validates that no other mapping annotation is present. |
| 39 | */ |
| 40 | static <T extends Annotation> T getTypeAnnotation(Class<T> annotation, Class<?> annotatedClass) { |
| 41 | T instance = annotatedClass.getAnnotation(annotation); |
| 42 | if (instance == null) |
| 43 | throw new IllegalArgumentException(String.format("@%s annotation was not found on %s", |
| 44 | annotation.getSimpleName(), annotatedClass)); |
| 45 | |
| 46 | // Check that no other mapping annotations are present |
| 47 | validateAnnotations(annotatedClass, annotation); |
| 48 | |
| 49 | return instance; |
| 50 | } |
| 51 | |
| 52 | private static void validateAnnotations(Class<?> clazz, Class<? extends Annotation> allowed) { |
| 53 | Collection<Annotation> classAnnotations = new HashSet<Annotation>(); |
| 54 | Collections.addAll(classAnnotations, clazz.getAnnotations()); |
| 55 | Class<? extends Annotation> invalid = validateAnnotations(classAnnotations, Collections.singleton(allowed)); |
| 56 | if (invalid != null) |
| 57 | throw new IllegalArgumentException(String.format("Cannot have both @%s and @%s on %s", |
| 58 | allowed.getSimpleName(), invalid.getSimpleName(), |
| 59 | clazz)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Checks that a field is only annotated with the given mapping annotations, and that its "frozen" annotations are valid. |
| 64 | */ |
| 65 | static void validateAnnotations(PropertyMapper property, Collection<? extends Class<? extends Annotation>> allowed) { |
| 66 | Class<? extends Annotation> invalid = validateAnnotations(property.getAnnotations(), allowed); |
| 67 | if (invalid != null) { |
| 68 | throw new IllegalArgumentException(String.format("Annotation @%s is not allowed on property '%s'", |
| 69 | invalid.getSimpleName(), |
| 70 | property)); |
| 71 | } |
| 72 | checkValidPrimaryKey(property); |
| 73 | checkValidComputed(property); |
| 74 | } |
| 75 | |
| 76 | // Returns the offending annotation if there is one |
| 77 | private static Class<? extends Annotation> validateAnnotations(Collection<Annotation> annotations, Collection<? extends Class<? extends Annotation>> allowed) { |
| 78 | for (Annotation annotation : annotations) { |
| 79 | Class<? extends Annotation> actual = annotation.annotationType(); |
| 80 | if (actual.getPackage().equals(MAPPING_PACKAGE) && !allowed.contains(actual)) |
| 81 | return actual; |
| 82 | } |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | private static void checkValidPrimaryKey(PropertyMapper property) { |
| 87 | if (property.isPartitionKey() && property.isClusteringColumn()) |
| 88 | throw new IllegalArgumentException(String.format("Property '%s' cannot be annotated with both @PartitionKey and @ClusteringColumn", property)); |
| 89 | } |
| 90 | |
| 91 | private static void checkValidComputed(PropertyMapper property) { |
| 92 | if (property.isComputed()) { |
| 93 | Computed computed = property.annotation(Computed.class); |
| 94 | if (computed.value().isEmpty()) { |
| 95 | throw new IllegalArgumentException(String.format("Property '%s': attribute 'value' of annotation @Computed is mandatory for computed properties", property)); |
| 96 | } |
| 97 | if (property.hasAnnotation(Column.class)) { |
| 98 | throw new IllegalArgumentException(String.format("Property '%s' cannot be annotated with both @Column and @Computed", property)); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void validateOrder(List<PropertyMapper> properties, String annotation) { |
| 104 | for (int i = 0; i < properties.size(); i++) { |
| 105 | PropertyMapper property = properties.get(i); |
| 106 | int pos = property.position; |
| 107 | if (pos != i) |
| 108 | throw new IllegalArgumentException(String.format("Invalid ordering value %d for annotation %s of property '%s', was expecting %d", |
| 109 | pos, annotation, property, i)); |
| 110 | } |
| 111 | } |
| 112 | } |