Elffuss BraidHub

kikocis/java-driver public

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